add 退出登录
This commit is contained in:
parent
29d101e605
commit
399efe49ff
|
|
@ -56,6 +56,15 @@
|
|||
<artifactId>junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- 缓存及客户端 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
package com.tacit.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.tacit.common.exception.BusinessException;
|
||||
|
||||
public class JSONUtils {
|
||||
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
|
||||
.registerModule(new JavaTimeModule());
|
||||
/**
|
||||
*
|
||||
* @param obj
|
||||
* @return
|
||||
*/
|
||||
public static <T> String obj2String(T obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return obj instanceof String ? (String)obj : OBJECT_MAPPER.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new BusinessException("object 序列化错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package com.tacit.common.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(factory);
|
||||
|
||||
// key 使用 String 序列化
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
|
||||
// value 使用 JSON 序列化
|
||||
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
// 设置字段可见性
|
||||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
|
||||
// 新版 Jackson 安全的多态类型配置
|
||||
mapper.activateDefaultTyping(
|
||||
LaissezFaireSubTypeValidator.instance,
|
||||
ObjectMapper.DefaultTyping.NON_FINAL
|
||||
);
|
||||
|
||||
template.setValueSerializer(serializer);
|
||||
template.setHashValueSerializer(serializer);
|
||||
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package com.tacit.common.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Description: redis工具类
|
||||
* @Author: lidongjin
|
||||
* @CreateTime: 2026-01-07 09:27
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RedisUtils {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
public void setObject(String key, Object value, long expireTime, TimeUnit timeUnit) {
|
||||
redisTemplate.opsForValue().set(key, value, expireTime, timeUnit);
|
||||
}
|
||||
|
||||
public Boolean hasKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
public void delete(String token) {
|
||||
redisTemplate.delete(token);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package com.tacit.common.utils;
|
||||
|
||||
/**
|
||||
* @Author: ldj
|
||||
* @Date: 2026/1/7 10:54
|
||||
*/
|
||||
public enum ResCode {
|
||||
|
||||
SAVE_SUCCESS(200, "保存成功"),
|
||||
|
||||
QUERY_SUCCESS(200, ""),
|
||||
|
||||
DELETE_SUCCESS(200, "删除成功"),
|
||||
// 处理成功的
|
||||
SUCCESS_REQUEST(200, "成功"),
|
||||
|
||||
NO_LOGIN(201, "登录超时请重新登录"),
|
||||
|
||||
NO_PARAM(202, "参数为空"),
|
||||
|
||||
PARAM_ERROR(202, "参数序列化错误"),
|
||||
|
||||
NO_PERMISSION(401, "用户没有该权限"),
|
||||
// 查询的数据或者前端传入的数据不存在
|
||||
DATA_NOT_EXIST(9999, "不存在"),
|
||||
|
||||
SYSTEM_ERROR(9999, "系统内部错误"),
|
||||
|
||||
MULTIPART_TOO_LARGE(203, "上传文件过大,文件不能超过10MB"),
|
||||
|
||||
LOGOUT(202, "退出成功")
|
||||
;
|
||||
|
||||
|
||||
/** 错误码 */
|
||||
private Integer resultCode;
|
||||
|
||||
/** 错误描述 */
|
||||
private String resultMsg;
|
||||
|
||||
ResCode(Integer resultCode, String resultMsg) {
|
||||
this.resultCode = resultCode;
|
||||
this.resultMsg = resultMsg;
|
||||
}
|
||||
|
||||
public Integer getResultCode() {
|
||||
return resultCode;
|
||||
}
|
||||
|
||||
public String getResultMsg() {
|
||||
return resultMsg;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,10 +5,11 @@ import com.baomidou.mybatisplus.annotation.IdType;
|
|||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class Base {
|
||||
public class Base implements Serializable {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
private LocalDateTime createTime;
|
||||
|
|
|
|||
|
|
@ -28,6 +28,9 @@ public class ResponseResult<T> implements Serializable {
|
|||
public static <T> ResponseResult<T> success(T data) {
|
||||
return new ResponseResult<>(200, "操作成功", data);
|
||||
}
|
||||
public static <T> ResponseResult<T> success(Integer code, String message,T data) {
|
||||
return new ResponseResult<>(code, message,data);
|
||||
}
|
||||
|
||||
public static <T> ResponseResult<T> success(String message, T data) {
|
||||
return new ResponseResult<>(200, message, data);
|
||||
|
|
|
|||
14
pom.xml
14
pom.xml
|
|
@ -34,6 +34,8 @@
|
|||
<commons-lang3.version>3.14.0</commons-lang3.version>
|
||||
<junit-jupiter.version>5.9.3</junit-jupiter.version>
|
||||
<loadbalancer.version>4.0.4</loadbalancer.version>
|
||||
<redis.version>3.2.0</redis.version>
|
||||
<data-redis.version>2.2.1.RELEASE</data-redis.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
@ -218,6 +220,17 @@
|
|||
<artifactId>resilience4j-timelimiter</artifactId>
|
||||
<version>${resilience4j.version}</version>
|
||||
</dependency>
|
||||
<!--缓存及客户端-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<version>${data-redis.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>${redis.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
@ -256,6 +269,7 @@
|
|||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
<version>${loadbalancer.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
FROM java:21
|
||||
FROM openjdk:21-rc
|
||||
COPY *.jar /app.jar
|
||||
|
||||
CMD ["--server.port=8082"]
|
||||
|
|
|
|||
|
|
@ -94,7 +94,15 @@
|
|||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 缓存及客户端 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -4,10 +4,15 @@ import com.tacit.admin.entity.dto.LoginRequest;
|
|||
import com.tacit.admin.entity.dto.LoginResponse;
|
||||
import com.tacit.admin.entity.dto.RegisterRequest;
|
||||
import com.tacit.admin.service.UserService;
|
||||
import com.tacit.common.constant.CommonConstant;
|
||||
import com.tacit.common.entity.ResponseResult;
|
||||
import com.tacit.common.utils.ResCode;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
|
@ -32,4 +37,12 @@ public class AuthController {
|
|||
userService.register(registerRequest);
|
||||
return ResponseResult.success();
|
||||
}
|
||||
@Operation(summary = "用户退出登录", description = "用户退出登录")
|
||||
@PostMapping("/logout")
|
||||
public ResponseResult<Void> logout(HttpServletRequest request) {
|
||||
String authorization = request.getHeader("Authorization");
|
||||
String token = authorization.substring(CommonConstant.JWT_PREFIX.length());
|
||||
userService.logout(token);
|
||||
return ResponseResult.<Void>success(ResCode.LOGOUT.getResultCode(), ResCode.LOGOUT.getResultMsg(), null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,4 +62,8 @@ public interface UserService extends IService<User> {
|
|||
* @param registerRequest 注册请求参数
|
||||
*/
|
||||
void register(RegisterRequest registerRequest);
|
||||
/**
|
||||
* 用户退出登录
|
||||
*/
|
||||
void logout(String token);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,13 +9,17 @@ import com.tacit.admin.entity.dto.RegisterRequest;
|
|||
import com.tacit.admin.mapper.UserMapper;
|
||||
import com.tacit.admin.service.UserService;
|
||||
import com.tacit.common.utils.JwtUtils;
|
||||
import com.tacit.common.utils.RedisUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
|
||||
|
|
@ -25,6 +29,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||
|
||||
@Resource
|
||||
private PasswordEncoder passwordEncoder;
|
||||
@Autowired
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Override
|
||||
public User getUserByUsername(String username) {
|
||||
|
|
@ -71,6 +77,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||
|
||||
@Override
|
||||
public LoginResponse login(LoginRequest loginRequest) {
|
||||
// 验证用户名、密码 不是空也不是null
|
||||
if (StringUtils.isBlank(loginRequest.getUsername()) || StringUtils.isBlank(loginRequest.getPassword())) {
|
||||
throw new RuntimeException("用户名或密码不能为空");
|
||||
}
|
||||
|
||||
// 根据用户名查询用户
|
||||
User user = getUserByUsername(loginRequest.getUsername());
|
||||
if (user == null) {
|
||||
|
|
@ -87,7 +98,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||
claims.put("username", user.getUsername());
|
||||
claims.put("role", user.getRole());
|
||||
String token = JwtUtils.generateToken(claims);
|
||||
|
||||
// 将生成的 JWT 令牌存储到 Redis 缓存中,设置过期时间为 7 天(7 * 24 * 60 * 60 秒)
|
||||
redisUtils.setObject(token, user.getId(),7 * 24 * 60 * 60, TimeUnit.SECONDS);
|
||||
// 构建登录响应
|
||||
LoginResponse loginResponse = new LoginResponse();
|
||||
loginResponse.setToken(token);
|
||||
|
|
@ -95,6 +107,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||
|
||||
return loginResponse;
|
||||
}
|
||||
/**
|
||||
* 用户退出登录
|
||||
*/
|
||||
@Override
|
||||
public void logout(String token) {
|
||||
redisUtils.delete(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(RegisterRequest registerRequest) {
|
||||
|
|
|
|||
|
|
@ -1,36 +1,3 @@
|
|||
server:
|
||||
port: 8082
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.oceanbase.jdbc.Driver
|
||||
url: jdbc:oceanbase://localhost:2881/tacit?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
data:
|
||||
redis:
|
||||
host: 117.72.94.232
|
||||
port: 56379
|
||||
password: redis
|
||||
database: 1
|
||||
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
mapper-locations: classpath*:mapper/**/*.xml
|
||||
type-aliases-package: com.tacit.admin.entity
|
||||
|
||||
# Swagger Configuration
|
||||
springdoc:
|
||||
api-docs:
|
||||
enabled: true
|
||||
path: /v3/api-docs
|
||||
swagger-ui:
|
||||
enabled: true
|
||||
path: /swagger-ui.html
|
||||
|
||||
# Logging Configuration
|
||||
logging:
|
||||
level:
|
||||
com.tacit.admin: debug
|
||||
|
|
|
|||
|
|
@ -1,36 +1,3 @@
|
|||
server:
|
||||
port: 8082
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.oceanbase.jdbc.Driver
|
||||
url: jdbc:oceanbase://117.72.94.232:53306/tacit?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||
username: root
|
||||
password: fU44GFH5
|
||||
data:
|
||||
redis:
|
||||
host: 117.72.94.232
|
||||
port: 56379
|
||||
password: redis
|
||||
database: 2
|
||||
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
mapper-locations: classpath*:mapper/**/*.xml
|
||||
type-aliases-package: com.tacit.admin.entity
|
||||
|
||||
# Swagger Configuration
|
||||
springdoc:
|
||||
api-docs:
|
||||
enabled: true
|
||||
path: /v3/api-docs
|
||||
swagger-ui:
|
||||
enabled: true
|
||||
path: /swagger-ui.html
|
||||
|
||||
# Logging Configuration
|
||||
logging:
|
||||
level:
|
||||
com.tacit.admin: debug
|
||||
|
|
|
|||
|
|
@ -7,12 +7,14 @@ spring:
|
|||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
server-addr: http://117.72.94.232:9091
|
||||
username: nacos
|
||||
password: nacos
|
||||
namespace: ldj
|
||||
config:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
server-addr: http://117.72.94.232:9091
|
||||
file-extension: yaml
|
||||
username: nacos
|
||||
password: nacos
|
||||
password: nacos
|
||||
namespace: ldj
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
FROM java:21
|
||||
FROM openjdk:21-rc
|
||||
COPY *.jar /app.jar
|
||||
|
||||
CMD ["--server.port=8082"]
|
||||
|
|
|
|||
|
|
@ -1,46 +1,3 @@
|
|||
server:
|
||||
port: 8083
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: tacit-app-api
|
||||
config:
|
||||
import: optional:nacos:localhost:8848?namespace=public&group=DEFAULT_GROUP&file-extension=yml
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
namespace: public
|
||||
datasource:
|
||||
driver-class-name: com.oceanbase.jdbc.Driver
|
||||
url: jdbc:oceanbase://localhost:3306/tacit?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
data:
|
||||
redis:
|
||||
host: 117.72.94.232
|
||||
port: 56379
|
||||
password: redis
|
||||
database: 1
|
||||
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
mapper-locations: classpath*:mapper/**/*.xml
|
||||
type-aliases-package: com.tacit.app.entity
|
||||
|
||||
# Swagger Configuration
|
||||
springdoc:
|
||||
api-docs:
|
||||
enabled: true
|
||||
path: /v3/api-docs
|
||||
swagger-ui:
|
||||
enabled: true
|
||||
path: /swagger-ui.html
|
||||
|
||||
# Logging Configuration
|
||||
logging:
|
||||
level:
|
||||
com.tacit.app: debug
|
||||
|
|
|
|||
|
|
@ -1,46 +1,3 @@
|
|||
server:
|
||||
port: 8083
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: tacit-app-api
|
||||
config:
|
||||
import: optional:nacos:localhost:8848?namespace=public&group=DEFAULT_GROUP&file-extension=yml
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
namespace: public
|
||||
datasource:
|
||||
driver-class-name: com.oceanbase.jdbc.Driver
|
||||
url: jdbc:oceanbase://117.72.94.232:53306/tacit?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||
username: root
|
||||
password: fU44GFH5
|
||||
data:
|
||||
redis:
|
||||
host: 117.72.94.232
|
||||
port: 56379
|
||||
password: redis
|
||||
database: 2
|
||||
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
mapper-locations: classpath*:mapper/**/*.xml
|
||||
type-aliases-package: com.tacit.app.entity
|
||||
|
||||
# Swagger Configuration
|
||||
springdoc:
|
||||
api-docs:
|
||||
enabled: true
|
||||
path: /v3/api-docs
|
||||
swagger-ui:
|
||||
enabled: true
|
||||
path: /swagger-ui.html
|
||||
|
||||
# Logging Configuration
|
||||
logging:
|
||||
level:
|
||||
com.tacit.app: debug
|
||||
|
|
|
|||
|
|
@ -7,12 +7,15 @@ spring:
|
|||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
server-addr: http://117.72.94.232:9091
|
||||
username: nacos
|
||||
password: nacos
|
||||
namespace: ldj
|
||||
config:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
server-addr: http://117.72.94.232:9091
|
||||
# server-addr: localhost:8848
|
||||
file-extension: yaml
|
||||
username: nacos
|
||||
password: nacos
|
||||
namespace: ldj
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
FROM java:21
|
||||
FROM openjdk:21-rc
|
||||
COPY *.jar /app.jar
|
||||
|
||||
CMD ["--server.port=8082"]
|
||||
|
|
|
|||
|
|
@ -62,7 +62,15 @@
|
|||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 缓存及客户端 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
package com.tacit.gateway.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(factory);
|
||||
|
||||
// key 使用 String 序列化
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
|
||||
// value 使用 JSON 序列化
|
||||
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
// 设置字段可见性
|
||||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
|
||||
// 新版 Jackson 安全的多态类型配置
|
||||
mapper.activateDefaultTyping(
|
||||
LaissezFaireSubTypeValidator.instance,
|
||||
ObjectMapper.DefaultTyping.NON_FINAL
|
||||
);
|
||||
|
||||
template.setValueSerializer(serializer);
|
||||
template.setHashValueSerializer(serializer);
|
||||
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,9 +3,11 @@ package com.tacit.gateway.filter;
|
|||
import com.tacit.common.constant.CommonConstant;
|
||||
import com.tacit.common.utils.JwtUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
|
|
@ -22,13 +24,14 @@ import java.util.List;
|
|||
@Component("JwtAuthenticationFilter")
|
||||
@Slf4j
|
||||
public class JwtAuthenticationFilter extends AbstractGatewayFilterFactory<JwtAuthenticationFilter.Config> {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
// 不需要认证的路径
|
||||
private static final List<String> WHITE_LIST = List.of(
|
||||
"/api/auth/login",
|
||||
"/api/auth/register",
|
||||
"/admin/auth/login",
|
||||
"/admin/auth/register",
|
||||
"/auth/login",
|
||||
"/auth/register",
|
||||
"/auth/login",
|
||||
"/auth/register",
|
||||
"/swagger-ui",
|
||||
"/v3/api-docs"
|
||||
);
|
||||
|
|
@ -70,7 +73,10 @@ public class JwtAuthenticationFilter extends AbstractGatewayFilterFactory<JwtAut
|
|||
// 验证JWT令牌
|
||||
try {
|
||||
JwtUtils.validateToken(token);
|
||||
|
||||
Boolean isBlacklisted = redisTemplate.hasKey(token);
|
||||
if (!Boolean.TRUE.equals(isBlacklisted)) {
|
||||
return unauthorizedResponse(exchange, "Token已被注销");
|
||||
}
|
||||
// 从令牌中获取用户信息并添加到请求头
|
||||
Long userId = JwtUtils.getUserIdFromToken(token);
|
||||
String username = JwtUtils.getUsernameFromToken(token);
|
||||
|
|
|
|||
|
|
@ -9,13 +9,16 @@ spring:
|
|||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
server-addr: http://117.72.94.232:9091
|
||||
username: nacos
|
||||
password: nacos
|
||||
namespace: ldj
|
||||
config:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
server-addr: http://117.72.94.232:9091
|
||||
# server-addr: localhost:8848
|
||||
file-extension: yaml
|
||||
username: nacos
|
||||
password: nacos
|
||||
namespace: ldj
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue