|
|
@ -7,19 +7,15 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
|
|
|
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.data.redis.core.ValueOperations; |
|
|
|
import org.springframework.data.redis.core.ValueOperations; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
|
|
|
import org.springframework.http.server.reactive.ServerHttpResponse; |
|
|
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.ruoyi.common.core.constant.CacheConstants; |
|
|
|
import com.ruoyi.common.core.constant.CacheConstants; |
|
|
|
import com.ruoyi.common.core.constant.Constants; |
|
|
|
import com.ruoyi.common.core.constant.Constants; |
|
|
|
import com.ruoyi.common.core.domain.R; |
|
|
|
import com.ruoyi.common.core.constant.HttpStatus; |
|
|
|
|
|
|
|
import com.ruoyi.common.core.utils.SecurityUtils; |
|
|
|
import com.ruoyi.common.core.utils.ServletUtils; |
|
|
|
import com.ruoyi.common.core.utils.ServletUtils; |
|
|
|
import com.ruoyi.common.core.utils.StringUtils; |
|
|
|
import com.ruoyi.common.core.utils.StringUtils; |
|
|
|
import com.ruoyi.common.redis.service.RedisService; |
|
|
|
import com.ruoyi.common.redis.service.RedisService; |
|
|
@ -35,7 +31,7 @@ import reactor.core.publisher.Mono; |
|
|
|
public class AuthFilter implements GlobalFilter, Ordered |
|
|
|
public class AuthFilter implements GlobalFilter, Ordered |
|
|
|
{ |
|
|
|
{ |
|
|
|
private static final Logger log = LoggerFactory.getLogger(AuthFilter.class); |
|
|
|
private static final Logger log = LoggerFactory.getLogger(AuthFilter.class); |
|
|
|
|
|
|
|
|
|
|
|
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60; |
|
|
|
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60; |
|
|
|
|
|
|
|
|
|
|
|
// 排除过滤的 uri 地址,nacos自行添加
|
|
|
|
// 排除过滤的 uri 地址,nacos自行添加
|
|
|
@ -44,61 +40,68 @@ public class AuthFilter implements GlobalFilter, Ordered |
|
|
|
|
|
|
|
|
|
|
|
@Resource(name = "stringRedisTemplate") |
|
|
|
@Resource(name = "stringRedisTemplate") |
|
|
|
private ValueOperations<String, String> sops; |
|
|
|
private ValueOperations<String, String> sops; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private RedisService redisService; |
|
|
|
private RedisService redisService; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) |
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) |
|
|
|
{ |
|
|
|
{ |
|
|
|
String url = exchange.getRequest().getURI().getPath(); |
|
|
|
ServerHttpRequest request = exchange.getRequest(); |
|
|
|
|
|
|
|
ServerHttpRequest.Builder mutate = request.mutate(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String url = request.getURI().getPath(); |
|
|
|
// 跳过不需要验证的路径
|
|
|
|
// 跳过不需要验证的路径
|
|
|
|
if (StringUtils.matches(url, ignoreWhite.getWhites())) |
|
|
|
if (StringUtils.matches(url, ignoreWhite.getWhites())) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return chain.filter(exchange); |
|
|
|
return chain.filter(exchange); |
|
|
|
} |
|
|
|
} |
|
|
|
String token = getToken(exchange.getRequest()); |
|
|
|
String token = getToken(request); |
|
|
|
if (StringUtils.isBlank(token)) |
|
|
|
if (StringUtils.isEmpty(token)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return setUnauthorizedResponse(exchange, "令牌不能为空"); |
|
|
|
return unauthorizedResponse(exchange, "令牌不能为空"); |
|
|
|
} |
|
|
|
} |
|
|
|
String userStr = sops.get(getTokenKey(token)); |
|
|
|
String userStr = sops.get(getTokenKey(token)); |
|
|
|
if (StringUtils.isNull(userStr)) |
|
|
|
if (StringUtils.isEmpty(userStr)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return setUnauthorizedResponse(exchange, "登录状态已过期"); |
|
|
|
return unauthorizedResponse(exchange, "登录状态已过期"); |
|
|
|
} |
|
|
|
} |
|
|
|
JSONObject obj = JSONObject.parseObject(userStr); |
|
|
|
JSONObject cacheObj = JSONObject.parseObject(userStr); |
|
|
|
String userid = obj.getString("userid"); |
|
|
|
String userid = cacheObj.getString("userid"); |
|
|
|
String username = obj.getString("username"); |
|
|
|
String username = cacheObj.getString("username"); |
|
|
|
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)) |
|
|
|
if (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return setUnauthorizedResponse(exchange, "令牌验证失败"); |
|
|
|
return unauthorizedResponse(exchange, "令牌验证失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 设置过期时间
|
|
|
|
// 设置过期时间
|
|
|
|
redisService.expire(getTokenKey(token), EXPIRE_TIME); |
|
|
|
redisService.expire(getTokenKey(token), EXPIRE_TIME); |
|
|
|
// 设置用户信息到请求
|
|
|
|
// 设置用户信息到请求
|
|
|
|
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid) |
|
|
|
addHeader(mutate, CacheConstants.DETAILS_USER_ID, userid); |
|
|
|
.header(CacheConstants.DETAILS_USERNAME, ServletUtils.urlEncode(username)).build(); |
|
|
|
addHeader(mutate, CacheConstants.DETAILS_USERNAME, username); |
|
|
|
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build(); |
|
|
|
return chain.filter(exchange.mutate().request(mutate.build()).build()); |
|
|
|
|
|
|
|
|
|
|
|
return chain.filter(mutableExchange); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg) |
|
|
|
private void addHeader(ServerHttpRequest.Builder mutate, String name, Object value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ServerHttpResponse response = exchange.getResponse(); |
|
|
|
if (value == null) |
|
|
|
response.getHeaders().setContentType(MediaType.APPLICATION_JSON); |
|
|
|
{ |
|
|
|
response.setStatusCode(HttpStatus.OK); |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String valueStr = value.toString(); |
|
|
|
|
|
|
|
String valueEncode = ServletUtils.urlEncode(valueStr); |
|
|
|
|
|
|
|
mutate.header(name, valueEncode); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String msg) |
|
|
|
|
|
|
|
{ |
|
|
|
log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath()); |
|
|
|
log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath()); |
|
|
|
|
|
|
|
return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED); |
|
|
|
return response.writeWith(Mono.fromSupplier(() -> { |
|
|
|
|
|
|
|
DataBufferFactory bufferFactory = response.bufferFactory(); |
|
|
|
|
|
|
|
return bufferFactory.wrap(JSON.toJSONBytes(R.fail(HttpStatus.UNAUTHORIZED.value(), msg))); |
|
|
|
|
|
|
|
})); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 获取缓存key |
|
|
|
|
|
|
|
*/ |
|
|
|
private String getTokenKey(String token) |
|
|
|
private String getTokenKey(String token) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return CacheConstants.LOGIN_TOKEN_KEY + token; |
|
|
|
return CacheConstants.LOGIN_TOKEN_KEY + token; |
|
|
@ -109,12 +112,8 @@ public class AuthFilter implements GlobalFilter, Ordered |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private String getToken(ServerHttpRequest request) |
|
|
|
private String getToken(ServerHttpRequest request) |
|
|
|
{ |
|
|
|
{ |
|
|
|
String token = request.getHeaders().getFirst(CacheConstants.HEADER); |
|
|
|
String token = request.getHeaders().getFirst(CacheConstants.TOKEN_AUTHENTICATION); |
|
|
|
if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX)) |
|
|
|
return SecurityUtils.replaceTokenPrefix(token); |
|
|
|
{ |
|
|
|
|
|
|
|
token = token.replace(CacheConstants.TOKEN_PREFIX, ""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return token; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|