master
parent
cb9f4f3a68
commit
5111319d21
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.boot.security.server.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.task.TaskExecutor;
|
||||||
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程池配置、启用异步
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
* 2017年8月19日
|
||||||
|
*/
|
||||||
|
@EnableAsync(proxyTargetClass = true)
|
||||||
|
@Configuration
|
||||||
|
public class AsycTaskExecutorConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TaskExecutor taskExecutor() {
|
||||||
|
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||||
|
taskExecutor.setCorePoolSize(50);
|
||||||
|
taskExecutor.setMaxPoolSize(100);
|
||||||
|
|
||||||
|
return taskExecutor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,116 @@
|
||||||
|
package com.boot.security.server.config;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.alibaba.druid.support.http.StatViewServlet;
|
||||||
|
import com.alibaba.druid.support.http.WebStatFilter;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Druid数据源配置
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Slf4j(topic = "adminLogger")
|
||||||
|
@Configuration
|
||||||
|
public class DruidConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServletRegistrationBean druidServlet() {
|
||||||
|
log.info("init Druid Servlet Configuration ");
|
||||||
|
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
|
||||||
|
"/druid/*");
|
||||||
|
// IP白名单
|
||||||
|
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
|
||||||
|
// IP黑名单(共同存在时,deny优先于allow)
|
||||||
|
// servletRegistrationBean.addInitParameter("deny", "192.168.27.26");
|
||||||
|
// // 控制台管理用户
|
||||||
|
// servletRegistrationBean.addInitParameter("loginUsername", "admin");
|
||||||
|
// servletRegistrationBean.addInitParameter("loginPassword", "admin");
|
||||||
|
// // 是否能够重置数据 禁用HTML页面上的“Reset All”功能
|
||||||
|
// servletRegistrationBean.addInitParameter("resetEnable", "false");
|
||||||
|
return servletRegistrationBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean filterRegistrationBean() {
|
||||||
|
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
|
||||||
|
filterRegistrationBean.addUrlPatterns("/*");
|
||||||
|
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
|
||||||
|
return filterRegistrationBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据源配置
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
class DataSourceProperties {
|
||||||
|
private String url;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private String driverClassName;
|
||||||
|
private int initialSize;
|
||||||
|
private int minIdle;
|
||||||
|
private int maxActive;
|
||||||
|
private int maxWait;
|
||||||
|
private int timeBetweenEvictionRunsMillis;
|
||||||
|
private int minEvictableIdleTimeMillis;
|
||||||
|
private String validationQuery;
|
||||||
|
private boolean testWhileIdle;
|
||||||
|
private boolean testOnBorrow;
|
||||||
|
private boolean testOnReturn;
|
||||||
|
private boolean poolPreparedStatements;
|
||||||
|
private int maxPoolPreparedStatementPerConnectionSize;
|
||||||
|
private String filters;
|
||||||
|
private String connectionProperties;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public DataSource dataSource() {
|
||||||
|
DruidDataSource datasource = new DruidDataSource();
|
||||||
|
datasource.setUrl(url);
|
||||||
|
datasource.setUsername(username);
|
||||||
|
datasource.setPassword(password);
|
||||||
|
datasource.setDriverClassName(driverClassName);
|
||||||
|
|
||||||
|
datasource.setInitialSize(initialSize);
|
||||||
|
datasource.setMinIdle(minIdle);
|
||||||
|
datasource.setMaxActive(maxActive);
|
||||||
|
datasource.setMaxWait(maxWait);
|
||||||
|
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
||||||
|
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
||||||
|
datasource.setValidationQuery(validationQuery);
|
||||||
|
datasource.setTestWhileIdle(testWhileIdle);
|
||||||
|
datasource.setTestOnBorrow(testOnBorrow);
|
||||||
|
datasource.setTestOnReturn(testOnReturn);
|
||||||
|
datasource.setPoolPreparedStatements(poolPreparedStatements);
|
||||||
|
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
|
||||||
|
try {
|
||||||
|
datasource.setFilters(filters);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("异常", e);
|
||||||
|
}
|
||||||
|
datasource.setConnectionProperties(connectionProperties);
|
||||||
|
return datasource;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,8 +20,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http.authorizeRequests().antMatchers("/css/**", "/fonts/**", "/img/**", "/js/**", "/layui/**").permitAll()
|
http.authorizeRequests().antMatchers("/css/**", "/fonts/**", "/img/**", "/js/**", "/layui/**", "/files/*")
|
||||||
.anyRequest().authenticated().and().formLogin().loginProcessingUrl("/login")
|
.permitAll().anyRequest().authenticated().and().formLogin().loginProcessingUrl("/login")
|
||||||
.successHandler(authenticationSuccessHandler).failureHandler(authenticationFailureHandler).and()
|
.successHandler(authenticationSuccessHandler).failureHandler(authenticationFailureHandler).and()
|
||||||
.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
|
.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.boot.security.server.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import springfox.documentation.builders.ApiInfoBuilder;
|
||||||
|
import springfox.documentation.builders.PathSelectors;
|
||||||
|
import springfox.documentation.service.Contact;
|
||||||
|
import springfox.documentation.spi.DocumentationType;
|
||||||
|
import springfox.documentation.spring.web.plugins.Docket;
|
||||||
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* swagger文档
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
* 2017年7月21日
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableSwagger2
|
||||||
|
public class SwaggerConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Docket docket() {
|
||||||
|
return new Docket(DocumentationType.SWAGGER_2).groupName("swagger接口文档")
|
||||||
|
.apiInfo(new ApiInfoBuilder().title("swagger接口文档")
|
||||||
|
.contact(new Contact("小威老师", "", "xiaoweijiagou@163.com")).version("1.0").build())
|
||||||
|
.select().paths(PathSelectors.any()).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.boot.security.server.config;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.util.ResourceUtils;
|
||||||
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
|
||||||
|
import com.boot.security.server.page.table.PageTableArgumentResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebMvcConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跨域支持
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public WebMvcConfigurer corsConfigurer() {
|
||||||
|
return new WebMvcConfigurerAdapter() {
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
registry.addMapping("/**").allowedMethods("*");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* datatable分页解析
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public PageTableArgumentResolver tableHandlerMethodArgumentResolver() {
|
||||||
|
return new PageTableArgumentResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||||
|
argumentResolvers.add(tableHandlerMethodArgumentResolver());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件根路径
|
||||||
|
*/
|
||||||
|
@Value("${files.path}")
|
||||||
|
private String filesPath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 外部文件访问
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
registry.addResourceHandler("/files/**")
|
||||||
|
.addResourceLocations(ResourceUtils.FILE_URL_PREFIX + filesPath + File.separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,115 @@
|
||||||
|
package com.boot.security.server.page.table;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||||
|
import org.springframework.web.context.request.NativeWebRequest;
|
||||||
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||||
|
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页、查询参数解析
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class PageTableArgumentResolver implements HandlerMethodArgumentResolver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportsParameter(MethodParameter parameter) {
|
||||||
|
Class<?> cla = parameter.getParameterType();
|
||||||
|
|
||||||
|
return cla.isAssignableFrom(PageTableRequest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
|
||||||
|
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
||||||
|
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||||
|
|
||||||
|
PageTableRequest tableRequest = new PageTableRequest();
|
||||||
|
Map<String, String[]> param = request.getParameterMap();
|
||||||
|
if (param.containsKey("start")) {
|
||||||
|
tableRequest.setOffset(Integer.parseInt(request.getParameter("start")));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (param.containsKey("length")) {
|
||||||
|
tableRequest.setLimit(Integer.parseInt(request.getParameter("length")));
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> map = Maps.newHashMap();
|
||||||
|
tableRequest.setParams(map);
|
||||||
|
|
||||||
|
param.forEach((k, v) -> {
|
||||||
|
if (v.length == 1) {
|
||||||
|
map.put(k, v[0]);
|
||||||
|
} else {
|
||||||
|
map.put(k, Arrays.asList(v));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setOrderBy(tableRequest, map);
|
||||||
|
removeParam(tableRequest);
|
||||||
|
|
||||||
|
return tableRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去除datatables分页带的一些复杂参数
|
||||||
|
*
|
||||||
|
* @param tableRequest
|
||||||
|
*/
|
||||||
|
private void removeParam(PageTableRequest tableRequest) {
|
||||||
|
Map<String, Object> map = tableRequest.getParams();
|
||||||
|
|
||||||
|
if (!CollectionUtils.isEmpty(map)) {
|
||||||
|
Map<String, Object> param = new HashMap<>();
|
||||||
|
map.forEach((k, v) -> {
|
||||||
|
if (k.indexOf("[") < 0 && k.indexOf("]") < 0 && !"_".equals(k)) {
|
||||||
|
param.put(k, v);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
tableRequest.setParams(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从datatables分页请求数据中解析排序
|
||||||
|
*
|
||||||
|
* @param tableRequest
|
||||||
|
* @param map
|
||||||
|
*/
|
||||||
|
private void setOrderBy(PageTableRequest tableRequest, Map<String, Object> map) {
|
||||||
|
StringBuilder orderBy = new StringBuilder();
|
||||||
|
int size = map.size();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
String index = (String) map.get("order[" + i + "][column]");
|
||||||
|
if (StringUtils.isEmpty(index)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
String column = (String) map.get("columns[" + index + "][data]");
|
||||||
|
if (StringUtils.isBlank(column)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String sort = (String) map.get("order[" + i + "][dir]");
|
||||||
|
|
||||||
|
orderBy.append(column).append(" ").append(sort).append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (orderBy.length() > 0) {
|
||||||
|
tableRequest.getParams().put("orderBy",
|
||||||
|
" order by " + StringUtils.substringBeforeLast(orderBy.toString(), ","));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.boot.security.server.page.table;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询处理器
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Builder
|
||||||
|
public class PageTableHandler<T> {
|
||||||
|
|
||||||
|
private CountHandler countHandler;
|
||||||
|
private ListHandler<T> listHandler;
|
||||||
|
|
||||||
|
public PageTableResponse<T> handle(PageTableRequest dtRequest) {
|
||||||
|
int count = 0;
|
||||||
|
List<T> list = null;
|
||||||
|
|
||||||
|
count = this.countHandler.count(dtRequest);
|
||||||
|
if (count > 0) {
|
||||||
|
list = this.listHandler.list(dtRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (list == null) {
|
||||||
|
list = new ArrayList<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return PageTableResponse.<T> builder().recordsTotal(count).recordsFiltered(count).data(list).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ListHandler<T> {
|
||||||
|
List<T> list(PageTableRequest request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface CountHandler {
|
||||||
|
int count(PageTableRequest request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.boot.security.server.page.table;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询参数
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class PageTableRequest implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 7328071045193618467L;
|
||||||
|
|
||||||
|
private Integer offset;
|
||||||
|
private Integer limit;
|
||||||
|
private Map<String, Object> params;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.boot.security.server.page.table;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询返回
|
||||||
|
*
|
||||||
|
* @author 小威老师
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Builder
|
||||||
|
public class PageTableResponse<T> implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 620421858510718076L;
|
||||||
|
|
||||||
|
private Integer recordsTotal;
|
||||||
|
private Integer recordsFiltered;
|
||||||
|
private List<T> data;
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue