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