mirror of
https://github.com/RemainderTime/spring-boot-base-demo.git
synced 2026-03-09 21:50:46 +08:00
接口拦截器实现
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -76,6 +76,14 @@
|
||||
<version>3.19.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<version>2.7.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
20
src/main/java/cn/xf/basedemo/common/model/LoginUserInfo.java
Normal file
20
src/main/java/cn/xf/basedemo/common/model/LoginUserInfo.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package cn.xf.basedemo.common.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @program: xf-boot-base
|
||||
* @ClassName LoginUserInfo
|
||||
* @description:
|
||||
* @author: xiongfeng
|
||||
* @create: 2022-06-17 14:54
|
||||
**/
|
||||
@Data
|
||||
public class LoginUserInfo {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String phone;
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package cn.xf.basedemo.config;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @program: spring-boot-base-demo
|
||||
* @ClassName TokenInterceptor
|
||||
* @description:
|
||||
* @author: xiongfeng
|
||||
* @create: 2022-06-16 14:17
|
||||
**/
|
||||
public class TokenInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
|
||||
String requestURI = request.getRequestURI();
|
||||
|
||||
//登录处理
|
||||
String authorization = request.getHeader("Authorization");
|
||||
|
||||
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.xf.basedemo.config;
|
||||
package cn.xf.basedemo.interceptor;
|
||||
|
||||
import cn.xf.basedemo.interceptor.TokenInterceptor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
43
src/main/java/cn/xf/basedemo/interceptor/SessionContext.java
Normal file
43
src/main/java/cn/xf/basedemo/interceptor/SessionContext.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package cn.xf.basedemo.interceptor;
|
||||
|
||||
import cn.xf.basedemo.common.model.LoginUserInfo;
|
||||
|
||||
/**
|
||||
* @program: xf-boot-base
|
||||
* @ClassName SessionContext
|
||||
* @description:
|
||||
* @author: xiongfeng
|
||||
* @create: 2022-06-17 15:03
|
||||
**/
|
||||
public class SessionContext {
|
||||
|
||||
private ThreadLocal<LoginUserInfo> threadLocal;
|
||||
|
||||
private SessionContext() {
|
||||
this.threadLocal = new ThreadLocal<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用静态内部类创建单例
|
||||
*/
|
||||
private static class Context {
|
||||
private static final SessionContext INSTANCE = new SessionContext();
|
||||
}
|
||||
|
||||
public static SessionContext getInstance() {
|
||||
return Context.INSTANCE;
|
||||
}
|
||||
|
||||
public void set(LoginUserInfo user) {
|
||||
this.threadLocal.set(user);
|
||||
}
|
||||
|
||||
public LoginUserInfo get() {
|
||||
return this.threadLocal.get();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.threadLocal.remove();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package cn.xf.basedemo.interceptor;
|
||||
|
||||
import cn.xf.basedemo.common.model.LoginUserInfo;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @program: spring-boot-base-demo
|
||||
* @ClassName TokenInterceptor
|
||||
* @description:
|
||||
* @author: xiongfeng
|
||||
* @create: 2022-06-16 14:17
|
||||
**/
|
||||
public class TokenInterceptor implements HandlerInterceptor {
|
||||
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
//不拦截的请求列表
|
||||
private static final List<String> EXCLUDE_PATH_LIST = Arrays.asList("");
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
|
||||
String requestURI = request.getRequestURI();
|
||||
if(EXCLUDE_PATH_LIST.contains(requestURI)){
|
||||
return true;
|
||||
}
|
||||
//登录处理
|
||||
String token = request.getHeader("Authorization");
|
||||
if(StringUtils.isEmpty(token))
|
||||
token = request.getParameter("token");
|
||||
|
||||
String value = redisTemplate.opsForValue().get(token);
|
||||
if(StringUtils.isEmpty(value)){
|
||||
return false;
|
||||
}
|
||||
LoginUserInfo loginUserInfo = objectMapper.convertValue(value, LoginUserInfo.class);
|
||||
if(loginUserInfo == null || loginUserInfo.getId() <= 0){
|
||||
return false;
|
||||
}
|
||||
redisTemplate.expire(token, 86700, TimeUnit.SECONDS);
|
||||
|
||||
//用户信息设置到上下文
|
||||
SessionContext.getInstance().set(loginUserInfo);
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
}
|
||||
@@ -1,70 +1,24 @@
|
||||
#spring:
|
||||
# datasource:
|
||||
# dynamic:
|
||||
# primary: master
|
||||
# strict: true #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
|
||||
# hikari:
|
||||
# minimum-idle: 4
|
||||
# maximum-pool-size: 4
|
||||
# connection-init-sql: SELECT 1
|
||||
# connection-test-query: SELECT 1
|
||||
# datasource:
|
||||
# master:
|
||||
# url: jdbc:mysql://rm-2vcg431j388xlt5541o.mysql.cn-chengdu.rds.aliyuncs.com:3306/nearby_travel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
|
||||
# username: aliyun_test
|
||||
# password: testRootZby!2020@
|
||||
# driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# slave:
|
||||
# url: jdbc:mysql://rm-2vcg431j388xlt5541o.mysql.cn-chengdu.rds.aliyuncs.com:3306/nearby_travel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
|
||||
# username: aliyun_test
|
||||
# password: testRootZby!2020@
|
||||
# driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# cloud:
|
||||
# config:
|
||||
# enabled: false
|
||||
# consul:
|
||||
# host: 192.168.10.111 #139.224.207.104
|
||||
# port: 8500
|
||||
# config:
|
||||
# # 是否启用配置中心,默认值 true 开启
|
||||
# enabled: false
|
||||
# discovery:
|
||||
# register: false # 是否需要注册
|
||||
#grpc:
|
||||
# client:
|
||||
# GLOBAL:
|
||||
# security:
|
||||
# enable-keep-alive: true
|
||||
# keep-alive-without-calls: true
|
||||
# negotiation-type: plaintext
|
||||
# center-merchant:
|
||||
# address: 'discovery:///center-merchant'
|
||||
# # kdm 192.168.11.116
|
||||
# # gsj 192.168.10.43
|
||||
# # tc 192.168.9.62
|
||||
# # address: 'static://192.168.11.116:9999'
|
||||
# enableKeepAlive: true
|
||||
# keepAliveWithoutCalls: true
|
||||
# center-user:
|
||||
# address: 'discovery:///center-user'
|
||||
# # address: 'static://192.168.10.43:8888'
|
||||
# enableKeepAlive: true
|
||||
# keepAliveWithoutCalls: true
|
||||
# center-ocr:
|
||||
# address: 'discovery:///center-merchant'
|
||||
# # address: 'static://192.168.10.43:6566'
|
||||
# enableKeepAlive: true
|
||||
# keepAliveWithoutCalls: true
|
||||
# center-contract:
|
||||
# address: 'discovery:///center-contract'
|
||||
# enableKeepAlive: true
|
||||
# keepAliveWithoutCalls: true
|
||||
# center-dataapi:
|
||||
# address: 'static://192.168.8.126:9999'
|
||||
# enableKeepAlive: true
|
||||
# keepAliveWithoutCalls: true
|
||||
#
|
||||
#
|
||||
spring:
|
||||
datasource:
|
||||
dynamic:
|
||||
primary: master
|
||||
strict: true #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
|
||||
hikari:
|
||||
minimum-idle: 4
|
||||
maximum-pool-size: 4
|
||||
connection-init-sql: SELECT 1
|
||||
connection-test-query: SELECT 1
|
||||
datasource:
|
||||
master:
|
||||
url: jdbc:mysql://122.112.153.128:3306/xf-boot-base?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
|
||||
username: root
|
||||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
slave:
|
||||
url: jdbc:mysql://122.112.153.128:3306/xf-boot-base?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
|
||||
username: root
|
||||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
##redis
|
||||
#redis:
|
||||
# dynamic:
|
||||
|
||||
Reference in New Issue
Block a user