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