|
@@ -19,6 +19,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
import org.springframework.core.Ordered;
|
|
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -34,6 +35,7 @@ import org.springframework.web.server.ServerWebExchange;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
@Component
|
|
@@ -49,6 +51,9 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
@Autowired
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
@Value("${jwt.tokenHeader}")
|
|
|
private String tokenHeader;
|
|
|
@Value("${redis.database}")
|
|
@@ -56,6 +61,9 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
@Value("${redis.key.token}")
|
|
|
private String REDIS_KEY_TOKEN;
|
|
|
|
|
|
+ @Value("${jwt.expiration}")
|
|
|
+ private long EXPIRE_TIME;
|
|
|
+
|
|
|
@Override
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
@@ -76,7 +84,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
log.error("token = {}",token);
|
|
|
throw new ApiException(ResultCode.UNAUTHORIZED);
|
|
|
}
|
|
|
- String username = jwtTokenUtil.getUserNameFromToken(token);
|
|
|
+ String username = jwtTokenUtil.getUserNameFromToken(token.trim());
|
|
|
// 待抽离
|
|
|
if (StringUtils.isEmpty(username)){
|
|
|
|
|
@@ -88,14 +96,17 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
log.error("resultToken = {}",resultToken);
|
|
|
throw new ApiException(ResultCode.UNAUTHORIZED);
|
|
|
}
|
|
|
+ redisTemplate.expire(key,EXPIRE_TIME, TimeUnit.SECONDS);
|
|
|
|
|
|
String s = stringRedisTemplate.opsForValue().get(CacheConstants.LOGIN_USER_INFO + username);
|
|
|
if (StringUtils.isNotEmpty(s)){
|
|
|
+
|
|
|
JSONArray array = (JSONArray)JSONArray.parse(s);
|
|
|
log.info("获取到了登录信息"+array);
|
|
|
JSONObject o = (JSONObject)array.get(1);
|
|
|
if (o.containsKey("userId")){
|
|
|
String userId = o.get("userId").toString();
|
|
|
+ redisTemplate.expire(CacheConstants.LOGIN_USER_INFO + username,EXPIRE_TIME, TimeUnit.SECONDS);
|
|
|
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userId)
|
|
|
.header(CacheConstants.DETAILS_USERNAME, username).build();
|
|
|
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
|
|
@@ -116,7 +127,6 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
return Ordered.HIGHEST_PRECEDENCE;
|
|
|
}
|
|
|
|
|
|
- private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
|
|
|
|
|
|
|
|
|
|
|
@@ -129,12 +139,14 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
String token = getToken(exchange.getRequest());
|
|
|
if (StringUtils.isBlank(token))
|
|
|
{
|
|
|
- return setUnauthorizedResponse(exchange, "令牌不能为空");
|
|
|
+// return setUnauthorizedResponse(exchange, "令牌不能为空");
|
|
|
+ throw new ApiException(ResultCode.UNAUTHORIZED);
|
|
|
}
|
|
|
String userStr = stringRedisTemplate.opsForValue().get(getTokenKey(token));
|
|
|
if (userStr == null)
|
|
|
{
|
|
|
- return setUnauthorizedResponse(exchange, "登录状态已过期");
|
|
|
+// return setUnauthorizedResponse(exchange, "登录状态已过期");
|
|
|
+ throw new ApiException(ResultCode.UNAUTHORIZED);
|
|
|
}
|
|
|
log.info("获取的用户信息{}",userStr);
|
|
|
// JSONObject obj = JSONObject.parseObject(userStr);
|
|
@@ -143,7 +155,8 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
|
|
String username = obj.getString("username");
|
|
|
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username))
|
|
|
{
|
|
|
- return setUnauthorizedResponse(exchange, "令牌验证失败");
|
|
|
+// return setUnauthorizedResponse(exchange, "令牌验证失败");
|
|
|
+ throw new ApiException(ResultCode.UNAUTHORIZED);
|
|
|
}
|
|
|
|
|
|
// 设置过期时间
|