parent
3630200fcb
commit
9ebceeaa03
|
|
@ -2,10 +2,3 @@
|
|||
## 2018.03.05 spring boot 由1.5.10 升级到2.0.0
|
||||
## 2018.03.15上传文件的大小配置修改spring.http.multipart改为spring.servlet.multipart
|
||||
## 2018.04.14 layui升级到2.2.6
|
||||
## 2018.05.16 修改druid
|
||||
1. pom中删除druid依赖,改为druid-spring-boot-starter
|
||||
2. 作废DruidConfig类,druid支持自动注入了,前缀spring.datasource.druid
|
||||
源码com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper
|
||||
3. 修改application.yml里的数据源配置,spring.datasource改为spring.datasource.druid
|
||||
|
||||
|
||||
|
|
|
|||
275
pom.xml
275
pom.xml
|
|
@ -1,143 +1,148 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.boot</groupId>
|
||||
<artifactId>boot-security</artifactId>
|
||||
<version>1.0</version>
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.boot</groupId>
|
||||
<artifactId>boot-security</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<swagger.version>2.8.0</swagger.version>
|
||||
<google.guava>23.0</google.guava>
|
||||
<fastjson.version>1.2.47</fastjson.version>
|
||||
<druid.version>1.1.8</druid.version>
|
||||
<poi.version>3.17</poi.version>
|
||||
<jwt.version>0.9.0</jwt.version>
|
||||
<mybatis.version>1.3.2</mybatis.version>
|
||||
</properties>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<swagger.version>2.8.0</swagger.version>
|
||||
<google.guava>23.0</google.guava>
|
||||
<fastjson.version>1.2.47</fastjson.version>
|
||||
<druid.version>1.1.8</druid.version>
|
||||
<poi.version>3.17</poi.version>
|
||||
<jwt.version>0.9.0</jwt.version>
|
||||
<mybatis.version>1.3.2</mybatis.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${google.guava}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId>
|
||||
</dependency> -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>${fastjson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>${mybatis.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
<!-- druid需要此日志包 -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${google.guava}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId>
|
||||
</dependency> -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>${fastjson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>${mybatis.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!--druid改为starter的方式-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-bean-validators</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>${poi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>${jwt.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-bean-validators</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>${poi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>${jwt.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
@ -1,260 +1,258 @@
|
|||
package com.boot.security.server.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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 javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
|
||||
/**
|
||||
* Druid数据源配置<br>
|
||||
* 2018.05.16 作废该配置类,因为druid提供了druid-spring-boot-starter,看下pom.xml<br>
|
||||
* 直接在bootstrap.yml里就能配置druid数据源了<br>
|
||||
* 前缀spring.datasource.druid,具体看下druid自动注入源码 DruidDataSourceWrapper
|
||||
*
|
||||
* Druid数据源配置
|
||||
*
|
||||
* @author 小威老师
|
||||
* @see com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper
|
||||
*
|
||||
*/
|
||||
//@Configuration
|
||||
@Deprecated
|
||||
@Configuration
|
||||
public class DruidConfig {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger("adminLogger");
|
||||
private static final Logger log = LoggerFactory.getLogger("adminLogger");
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean<StatViewServlet> druidServlet() {
|
||||
log.info("init Druid Servlet Configuration ");
|
||||
ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(
|
||||
new StatViewServlet(), "/druid/*");
|
||||
// IP白名单
|
||||
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
|
||||
// IP黑名单(共同存在时,deny优先于allow)
|
||||
@Bean
|
||||
public ServletRegistrationBean<StatViewServlet> druidServlet() {
|
||||
log.info("init Druid Servlet Configuration ");
|
||||
ServletRegistrationBean<StatViewServlet> 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;
|
||||
}
|
||||
return servletRegistrationBean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
|
||||
FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(
|
||||
new WebStatFilter());
|
||||
filterRegistrationBean.addUrlPatterns("/*");
|
||||
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
@Bean
|
||||
public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
|
||||
FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(
|
||||
new WebStatFilter());
|
||||
filterRegistrationBean.addUrlPatterns("/*");
|
||||
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据源配置
|
||||
*
|
||||
* @author 小威老师
|
||||
*/
|
||||
// @ConfigurationProperties(prefix = "spring.datasource")
|
||||
@Deprecated
|
||||
public 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;
|
||||
/**
|
||||
* 数据源配置
|
||||
*
|
||||
* @author 小威老师
|
||||
*
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.datasource")
|
||||
public 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;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getDriverClassName() {
|
||||
return driverClassName;
|
||||
}
|
||||
public String getDriverClassName() {
|
||||
return driverClassName;
|
||||
}
|
||||
|
||||
public void setDriverClassName(String driverClassName) {
|
||||
this.driverClassName = driverClassName;
|
||||
}
|
||||
public void setDriverClassName(String driverClassName) {
|
||||
this.driverClassName = driverClassName;
|
||||
}
|
||||
|
||||
public int getInitialSize() {
|
||||
return initialSize;
|
||||
}
|
||||
public int getInitialSize() {
|
||||
return initialSize;
|
||||
}
|
||||
|
||||
public void setInitialSize(int initialSize) {
|
||||
this.initialSize = initialSize;
|
||||
}
|
||||
public void setInitialSize(int initialSize) {
|
||||
this.initialSize = initialSize;
|
||||
}
|
||||
|
||||
public int getMinIdle() {
|
||||
return minIdle;
|
||||
}
|
||||
public int getMinIdle() {
|
||||
return minIdle;
|
||||
}
|
||||
|
||||
public void setMinIdle(int minIdle) {
|
||||
this.minIdle = minIdle;
|
||||
}
|
||||
public void setMinIdle(int minIdle) {
|
||||
this.minIdle = minIdle;
|
||||
}
|
||||
|
||||
public int getMaxActive() {
|
||||
return maxActive;
|
||||
}
|
||||
public int getMaxActive() {
|
||||
return maxActive;
|
||||
}
|
||||
|
||||
public void setMaxActive(int maxActive) {
|
||||
this.maxActive = maxActive;
|
||||
}
|
||||
public void setMaxActive(int maxActive) {
|
||||
this.maxActive = maxActive;
|
||||
}
|
||||
|
||||
public int getMaxWait() {
|
||||
return maxWait;
|
||||
}
|
||||
public int getMaxWait() {
|
||||
return maxWait;
|
||||
}
|
||||
|
||||
public void setMaxWait(int maxWait) {
|
||||
this.maxWait = maxWait;
|
||||
}
|
||||
public void setMaxWait(int maxWait) {
|
||||
this.maxWait = maxWait;
|
||||
}
|
||||
|
||||
public int getTimeBetweenEvictionRunsMillis() {
|
||||
return timeBetweenEvictionRunsMillis;
|
||||
}
|
||||
public int getTimeBetweenEvictionRunsMillis() {
|
||||
return timeBetweenEvictionRunsMillis;
|
||||
}
|
||||
|
||||
public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
|
||||
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
|
||||
}
|
||||
public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
|
||||
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
|
||||
}
|
||||
|
||||
public int getMinEvictableIdleTimeMillis() {
|
||||
return minEvictableIdleTimeMillis;
|
||||
}
|
||||
public int getMinEvictableIdleTimeMillis() {
|
||||
return minEvictableIdleTimeMillis;
|
||||
}
|
||||
|
||||
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
|
||||
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
|
||||
}
|
||||
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
|
||||
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
|
||||
}
|
||||
|
||||
public String getValidationQuery() {
|
||||
return validationQuery;
|
||||
}
|
||||
public String getValidationQuery() {
|
||||
return validationQuery;
|
||||
}
|
||||
|
||||
public void setValidationQuery(String validationQuery) {
|
||||
this.validationQuery = validationQuery;
|
||||
}
|
||||
public void setValidationQuery(String validationQuery) {
|
||||
this.validationQuery = validationQuery;
|
||||
}
|
||||
|
||||
public boolean isTestWhileIdle() {
|
||||
return testWhileIdle;
|
||||
}
|
||||
public boolean isTestWhileIdle() {
|
||||
return testWhileIdle;
|
||||
}
|
||||
|
||||
public void setTestWhileIdle(boolean testWhileIdle) {
|
||||
this.testWhileIdle = testWhileIdle;
|
||||
}
|
||||
public void setTestWhileIdle(boolean testWhileIdle) {
|
||||
this.testWhileIdle = testWhileIdle;
|
||||
}
|
||||
|
||||
public boolean isTestOnBorrow() {
|
||||
return testOnBorrow;
|
||||
}
|
||||
public boolean isTestOnBorrow() {
|
||||
return testOnBorrow;
|
||||
}
|
||||
|
||||
public void setTestOnBorrow(boolean testOnBorrow) {
|
||||
this.testOnBorrow = testOnBorrow;
|
||||
}
|
||||
public void setTestOnBorrow(boolean testOnBorrow) {
|
||||
this.testOnBorrow = testOnBorrow;
|
||||
}
|
||||
|
||||
public boolean isTestOnReturn() {
|
||||
return testOnReturn;
|
||||
}
|
||||
public boolean isTestOnReturn() {
|
||||
return testOnReturn;
|
||||
}
|
||||
|
||||
public void setTestOnReturn(boolean testOnReturn) {
|
||||
this.testOnReturn = testOnReturn;
|
||||
}
|
||||
public void setTestOnReturn(boolean testOnReturn) {
|
||||
this.testOnReturn = testOnReturn;
|
||||
}
|
||||
|
||||
public boolean isPoolPreparedStatements() {
|
||||
return poolPreparedStatements;
|
||||
}
|
||||
public boolean isPoolPreparedStatements() {
|
||||
return poolPreparedStatements;
|
||||
}
|
||||
|
||||
public void setPoolPreparedStatements(boolean poolPreparedStatements) {
|
||||
this.poolPreparedStatements = poolPreparedStatements;
|
||||
}
|
||||
public void setPoolPreparedStatements(boolean poolPreparedStatements) {
|
||||
this.poolPreparedStatements = poolPreparedStatements;
|
||||
}
|
||||
|
||||
public int getMaxPoolPreparedStatementPerConnectionSize() {
|
||||
return maxPoolPreparedStatementPerConnectionSize;
|
||||
}
|
||||
public int getMaxPoolPreparedStatementPerConnectionSize() {
|
||||
return maxPoolPreparedStatementPerConnectionSize;
|
||||
}
|
||||
|
||||
public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
|
||||
this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
|
||||
}
|
||||
public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
|
||||
this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
|
||||
}
|
||||
|
||||
public String getFilters() {
|
||||
return filters;
|
||||
}
|
||||
public String getFilters() {
|
||||
return filters;
|
||||
}
|
||||
|
||||
public void setFilters(String filters) {
|
||||
this.filters = filters;
|
||||
}
|
||||
public void setFilters(String filters) {
|
||||
this.filters = filters;
|
||||
}
|
||||
|
||||
public String getConnectionProperties() {
|
||||
return connectionProperties;
|
||||
}
|
||||
public String getConnectionProperties() {
|
||||
return connectionProperties;
|
||||
}
|
||||
|
||||
public void setConnectionProperties(String connectionProperties) {
|
||||
this.connectionProperties = connectionProperties;
|
||||
}
|
||||
public void setConnectionProperties(String connectionProperties) {
|
||||
this.connectionProperties = connectionProperties;
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// @Primary
|
||||
@Deprecated
|
||||
public DataSource dataSource() {
|
||||
DruidDataSource datasource = new DruidDataSource();
|
||||
datasource.setUrl(url);
|
||||
datasource.setUsername(username);
|
||||
datasource.setPassword(password);
|
||||
datasource.setDriverClassName(driverClassName);
|
||||
@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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,17 @@
|
|||
package com.boot.security.server.service.impl;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.boot.security.server.dto.LoginUser;
|
||||
import com.boot.security.server.dto.Token;
|
||||
import com.boot.security.server.service.SysLogService;
|
||||
import com.boot.security.server.service.TokenService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* token存到redis的实现类<br>
|
||||
|
|
@ -19,7 +21,7 @@ import java.util.concurrent.TimeUnit;
|
|||
*
|
||||
*/
|
||||
@Deprecated
|
||||
//@Service
|
||||
@Service
|
||||
public class TokenServiceImpl implements TokenService {
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -31,135 +31,136 @@ import io.jsonwebtoken.SignatureAlgorithm;
|
|||
/**
|
||||
* token存到redis的实现类<br>
|
||||
* jwt实现的token
|
||||
*
|
||||
*
|
||||
* @author 小威老师
|
||||
*
|
||||
*/
|
||||
@Primary
|
||||
@Service
|
||||
public class TokenServiceJWTImpl implements TokenService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger("adminLogger");
|
||||
private static final Logger log = LoggerFactory.getLogger("adminLogger");
|
||||
|
||||
/**
|
||||
* token过期秒数
|
||||
*/
|
||||
@Value("${token.expire.seconds}")
|
||||
private Integer expireSeconds;
|
||||
@Autowired
|
||||
private RedisTemplate<String, LoginUser> redisTemplate;
|
||||
@Autowired
|
||||
private SysLogService logService;
|
||||
/**
|
||||
* 私钥
|
||||
*/
|
||||
@Value("${token.jwtSecret}")
|
||||
private String jwtSecret;
|
||||
/**
|
||||
* token过期秒数
|
||||
*/
|
||||
@Value("${token.expire.seconds}")
|
||||
private Integer expireSeconds;
|
||||
@Autowired
|
||||
private RedisTemplate<String, LoginUser> redisTemplate;
|
||||
@Autowired
|
||||
private SysLogService logService;
|
||||
/**
|
||||
* 私钥
|
||||
*/
|
||||
@Value("${token.jwtSecret}")
|
||||
private String jwtSecret;
|
||||
|
||||
private static Key KEY = null;
|
||||
private static final String LOGIN_USER_KEY = "LOGIN_USER_KEY";
|
||||
private static Key KEY = null;
|
||||
private static final String LOGIN_USER_KEY = "LOGIN_USER_KEY";
|
||||
|
||||
@Override
|
||||
public Token saveToken(LoginUser loginUser) {
|
||||
loginUser.setToken(UUID.randomUUID().toString());
|
||||
cacheLoginUser(loginUser);
|
||||
// 登陆日志
|
||||
logService.save(loginUser.getId(), "登陆", true, null);
|
||||
@Override
|
||||
public Token saveToken(LoginUser loginUser) {
|
||||
loginUser.setToken(UUID.randomUUID().toString());
|
||||
cacheLoginUser(loginUser);
|
||||
// 登陆日志
|
||||
logService.save(loginUser.getId(), "登陆", true, null);
|
||||
|
||||
String jwtToken = createJWTToken(loginUser);
|
||||
String jwtToken = createJWTToken(loginUser);
|
||||
|
||||
return new Token(jwtToken, loginUser.getLoginTime());
|
||||
}
|
||||
return new Token(jwtToken, loginUser.getLoginTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成jwt
|
||||
*
|
||||
* @param loginUser
|
||||
* @return
|
||||
*/
|
||||
private String createJWTToken(LoginUser loginUser) {
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
claims.put(LOGIN_USER_KEY, loginUser.getToken());// 放入一个随机字符串,通过该串可找到登陆用户
|
||||
/**
|
||||
* 生成jwt
|
||||
*
|
||||
* @param loginUser
|
||||
* @return
|
||||
*/
|
||||
private String createJWTToken(LoginUser loginUser) {
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
claims.put(LOGIN_USER_KEY, loginUser.getToken());// 放入一个随机字符串,通过该串可找到登陆用户
|
||||
|
||||
String jwtToken = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS256, getKeyInstance())
|
||||
.compact();
|
||||
String jwtToken = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS256, getKeyInstance())
|
||||
.compact();
|
||||
|
||||
return jwtToken;
|
||||
}
|
||||
return jwtToken;
|
||||
}
|
||||
|
||||
private void cacheLoginUser(LoginUser loginUser) {
|
||||
loginUser.setLoginTime(System.currentTimeMillis());
|
||||
loginUser.setExpireTime(loginUser.getLoginTime() + expireSeconds * 1000);
|
||||
// 根据uuid将loginUser缓存
|
||||
redisTemplate.boundValueOps(getTokenKey(loginUser.getToken())).set(loginUser, expireSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
private void cacheLoginUser(LoginUser loginUser) {
|
||||
loginUser.setLoginTime(System.currentTimeMillis());
|
||||
loginUser.setExpireTime(loginUser.getLoginTime() + expireSeconds * 1000);
|
||||
// 根据uuid将loginUser缓存
|
||||
redisTemplate.boundValueOps(getTokenKey(loginUser.getToken())).set(loginUser, expireSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新缓存的用户信息
|
||||
*/
|
||||
@Override
|
||||
public void refresh(LoginUser loginUser) {
|
||||
cacheLoginUser(loginUser);
|
||||
}
|
||||
/**
|
||||
* 更新缓存的用户信息
|
||||
*/
|
||||
@Override
|
||||
public void refresh(LoginUser loginUser) {
|
||||
cacheLoginUser(loginUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginUser getLoginUser(String jwtToken) {
|
||||
String uuid = getUUIDFromJWT(jwtToken);
|
||||
if (uuid != null) {
|
||||
return redisTemplate.boundValueOps(getTokenKey(uuid)).get();
|
||||
}
|
||||
@Override
|
||||
public LoginUser getLoginUser(String jwtToken) {
|
||||
String uuid = getUUIDFromJWT(jwtToken);
|
||||
if (uuid != null) {
|
||||
return redisTemplate.boundValueOps(getTokenKey(uuid)).get();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteToken(String jwtToken) {
|
||||
String uuid = getUUIDFromJWT(jwtToken);
|
||||
if (uuid != null) {
|
||||
String key = getTokenKey(uuid);
|
||||
LoginUser loginUser = redisTemplate.opsForValue().get(key);
|
||||
if (loginUser != null) {
|
||||
redisTemplate.delete(key);
|
||||
// 退出日志
|
||||
logService.save(loginUser.getId(), "退出", true, null);
|
||||
@Override
|
||||
public boolean deleteToken(String jwtToken) {
|
||||
String uuid = getUUIDFromJWT(jwtToken);
|
||||
if (uuid != null) {
|
||||
String key = getTokenKey(uuid);
|
||||
LoginUser loginUser = redisTemplate.opsForValue().get(key);
|
||||
if (loginUser != null) {
|
||||
redisTemplate.delete(key);
|
||||
// 退出日志
|
||||
logService.save(loginUser.getId(), "退出", true, null);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getTokenKey(String uuid) {
|
||||
return "tokens:" + uuid;
|
||||
}
|
||||
private String getTokenKey(String uuid) {
|
||||
return "tokens:" + uuid;
|
||||
}
|
||||
|
||||
private Key getKeyInstance() {
|
||||
if (KEY == null) {
|
||||
synchronized (TokenServiceJWTImpl.class) {
|
||||
if (KEY == null) {// 双重锁
|
||||
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(jwtSecret);
|
||||
KEY = new SecretKeySpec(apiKeySecretBytes, SignatureAlgorithm.HS256.getJcaName());
|
||||
}
|
||||
}
|
||||
}
|
||||
private Key getKeyInstance() {
|
||||
if (KEY == null) {
|
||||
synchronized (TokenServiceJWTImpl.class) {
|
||||
if (KEY == null) {// 双重锁
|
||||
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(jwtSecret);
|
||||
KEY = new SecretKeySpec(apiKeySecretBytes, SignatureAlgorithm.HS256.getJcaName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return KEY;
|
||||
}
|
||||
return KEY;
|
||||
}
|
||||
|
||||
private String getUUIDFromJWT(String jwtToken) {
|
||||
if ("null".equals(jwtToken) || StringUtils.isBlank(jwtToken)) {
|
||||
return null;
|
||||
}
|
||||
private String getUUIDFromJWT(String jwtToken) {
|
||||
if ("null".equals(jwtToken) || StringUtils.isBlank(jwtToken)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Map<String, Object> jwtClaims = Jwts.parser().setSigningKey(getKeyInstance()).parseClaimsJws(jwtToken).getBody();
|
||||
return MapUtils.getString(jwtClaims, LOGIN_USER_KEY);
|
||||
} catch (ExpiredJwtException e) {
|
||||
log.error("{}已过期", jwtToken);
|
||||
} catch (Exception e) {
|
||||
log.error("{}", e);
|
||||
}
|
||||
try {
|
||||
Map<String, Object> jwtClaims = Jwts.parser().setSigningKey(getKeyInstance()).parseClaimsJws(jwtToken).getBody();
|
||||
return MapUtils.getString(jwtClaims, LOGIN_USER_KEY);
|
||||
} catch (ExpiredJwtException e) {
|
||||
log.error("{}已过期", jwtToken);
|
||||
} catch (Exception e) {
|
||||
log.error("{}", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,23 @@
|
|||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/boot_security?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
initialSize: 5
|
||||
minIdle: 5
|
||||
maxActive: 20
|
||||
maxWait: 60000
|
||||
timeBetweenEvictionRunsMillis: 60000
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
validationQuery: SELECT 1
|
||||
testWhileIdle: true
|
||||
testOnBorrow: true
|
||||
testOnReturn: false
|
||||
poolPreparedStatements: true
|
||||
maxPoolPreparedStatementPerConnectionSize: 20
|
||||
filters: stat,wall
|
||||
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/boot_security?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
initialSize: 5
|
||||
minIdle: 5
|
||||
maxActive: 20
|
||||
maxWait: 60000
|
||||
timeBetweenEvictionRunsMillis: 60000
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
validationQuery: SELECT 1
|
||||
testWhileIdle: true
|
||||
testOnBorrow: true
|
||||
testOnReturn: false
|
||||
poolPreparedStatements: true
|
||||
maxPoolPreparedStatementPerConnectionSize: 20
|
||||
filters: stat,wall
|
||||
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
joda-date-time-format: yyyy-MM-dd HH:mm:ss
|
||||
|
|
@ -33,7 +32,6 @@ spring:
|
|||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
password:
|
||||
mail:
|
||||
default-encoding: UTF-8
|
||||
host: smtp.163.com
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue