jwt验证兼容redis
This commit is contained in:
parent
5d8f4ae4fd
commit
27cbcd9f33
|
|
@ -3,6 +3,8 @@ package com.tacit.admin.config;
|
|||
import com.tacit.common.constant.CommonConstant;
|
||||
import com.tacit.common.utils.AesPasswordEncoder;
|
||||
import com.tacit.common.utils.JwtUtils;
|
||||
import com.tacit.common.utils.RedisUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
|
@ -31,6 +33,9 @@ import java.util.Collections;
|
|||
@EnableMethodSecurity(prePostEnabled = true)
|
||||
@Slf4j
|
||||
public class SecurityConfig {
|
||||
|
||||
@Resource
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
|
|
@ -58,8 +63,8 @@ public class SecurityConfig {
|
|||
if (authorization != null && authorization.startsWith(CommonConstant.JWT_PREFIX)) {
|
||||
String token = authorization.substring(CommonConstant.JWT_PREFIX.length());
|
||||
try {
|
||||
// 验证JWT令牌
|
||||
if (JwtUtils.validateToken(token)) {
|
||||
// 验证JWT令牌和Redis中的令牌是否存在
|
||||
if (JwtUtils.validateToken(token) && redisUtils.hasKey(token)) {
|
||||
// 从令牌中获取用户信息
|
||||
String username = JwtUtils.getUsernameFromToken(token);
|
||||
String role = JwtUtils.getRoleFromToken(token);
|
||||
|
|
|
|||
|
|
@ -29,7 +29,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||
|
||||
@Resource
|
||||
private PasswordEncoder passwordEncoder;
|
||||
@Autowired
|
||||
|
||||
@Resource
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package com.tacit.app.config;
|
|||
import com.tacit.common.constant.CommonConstant;
|
||||
import com.tacit.common.utils.AesPasswordEncoder;
|
||||
import com.tacit.common.utils.JwtUtils;
|
||||
import com.tacit.common.utils.RedisUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
|
|
@ -31,6 +33,9 @@ import java.util.Collections;
|
|||
@EnableMethodSecurity(prePostEnabled = true)
|
||||
@Slf4j
|
||||
public class AppSecurityConfig {
|
||||
|
||||
@Autowired
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
|
|
@ -57,8 +62,8 @@ public class AppSecurityConfig {
|
|||
if (authorization != null && authorization.startsWith(CommonConstant.JWT_PREFIX)) {
|
||||
String token = authorization.substring(CommonConstant.JWT_PREFIX.length());
|
||||
try {
|
||||
// 验证JWT令牌
|
||||
if (JwtUtils.validateToken(token)) {
|
||||
// 验证JWT令牌和Redis中的令牌是否存在
|
||||
if (JwtUtils.validateToken(token) && redisUtils.hasKey(token)) {
|
||||
// 从令牌中获取用户信息
|
||||
Long userId = JwtUtils.getUserIdFromToken(token);
|
||||
String username = JwtUtils.getUsernameFromToken(token);
|
||||
|
|
|
|||
|
|
@ -72,8 +72,8 @@ public class JwtAuthenticationFilter extends AbstractGatewayFilterFactory<JwtAut
|
|||
|
||||
// 验证JWT令牌
|
||||
try {
|
||||
Boolean isBlacklisted = redisTemplate.hasKey(token);
|
||||
if (!Boolean.TRUE.equals(isBlacklisted)) {
|
||||
Boolean isTokenValid = redisTemplate.hasKey(token);
|
||||
if (!isTokenValid) {
|
||||
return unauthorizedResponse(exchange, "Token已被注销");
|
||||
}
|
||||
JwtUtils.validateToken(token);
|
||||
|
|
|
|||
Loading…
Reference in New Issue