|
|
|
@ -18,7 +18,6 @@ import org.springframework.web.server.ServerWebExchange; |
|
|
|
|
import reactor.core.publisher.Mono; |
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.util.Enumeration; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
@Component |
|
|
|
@ -40,17 +39,16 @@ public class ApiGlobalFilter implements GlobalFilter, Ordered { |
|
|
|
|
@Override |
|
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
|
|
|
|
String requestUrl = exchange.getRequest().getPath().toString(); |
|
|
|
|
boolean status = CollectionUtils.contains((Enumeration<?>) ignoreUrl, requestUrl); |
|
|
|
|
|
|
|
|
|
boolean status = CollectionUtils.contains(ignoreUrl.iterator(), requestUrl); |
|
|
|
|
if (!status){ |
|
|
|
|
String token = exchange.getRequest().getHeaders().getFirst("token"); |
|
|
|
|
//type用于区分不同的端,在做校验token时需要
|
|
|
|
|
String type= exchange.getRequest().getHeaders().getFirst("type"); |
|
|
|
|
ServerHttpResponse response = exchange.getResponse(); |
|
|
|
|
//没有数据
|
|
|
|
|
if (StringUtils.isBlank(token) || StringUtils.isBlank(type)) { |
|
|
|
|
if (StringUtils.isBlank(token)) { |
|
|
|
|
JSONObject message = new JSONObject(); |
|
|
|
|
message.put("code", ""); |
|
|
|
|
message.put("message", "鉴权失败,无token或类型"); |
|
|
|
|
message.put("code", "401"); |
|
|
|
|
message.put("message", "请先登录"); |
|
|
|
|
byte[] bits = message.toString().getBytes(StandardCharsets.UTF_8); |
|
|
|
|
DataBuffer buffer = response.bufferFactory().wrap(bits); |
|
|
|
|
response.setStatusCode(HttpStatus.UNAUTHORIZED); |
|
|
|
@ -58,13 +56,12 @@ public class ApiGlobalFilter implements GlobalFilter, Ordered { |
|
|
|
|
return response.writeWith(Mono.just(buffer)); |
|
|
|
|
//有数据
|
|
|
|
|
}else { |
|
|
|
|
String prefix = this.getPrefix(type); |
|
|
|
|
//校验token
|
|
|
|
|
Long userId = verifyJWT(token ,prefix); |
|
|
|
|
Long userId = verifyJWT(token); |
|
|
|
|
if (userId == null){ |
|
|
|
|
JSONObject message = new JSONObject(); |
|
|
|
|
message.put("message", "token错误"); |
|
|
|
|
message.put("code", ""); |
|
|
|
|
message.put("message", "请重新登录"); |
|
|
|
|
message.put("code", "401"); |
|
|
|
|
byte[] bits = message.toString().getBytes(StandardCharsets.UTF_8); |
|
|
|
|
DataBuffer buffer = response.bufferFactory().wrap(bits); |
|
|
|
|
response.setStatusCode(HttpStatus.UNAUTHORIZED); |
|
|
|
@ -85,27 +82,10 @@ public class ApiGlobalFilter implements GlobalFilter, Ordered { |
|
|
|
|
* @param token |
|
|
|
|
* @return userPhone |
|
|
|
|
*/ |
|
|
|
|
private Long verifyJWT(String token, String prefix){ |
|
|
|
|
private Long verifyJWT(String token){ |
|
|
|
|
return JwtUtil.verifyToken(token); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 根据type获取前缀 |
|
|
|
|
* @param type |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private String getPrefix(String type){ |
|
|
|
|
String prefix = null; |
|
|
|
|
if ("1".equals(type)){ |
|
|
|
|
prefix = "OPERATE"; |
|
|
|
|
}else if ("2".equals(type)){ |
|
|
|
|
prefix = "USER"; |
|
|
|
|
}else if ("3".equals(type)){ |
|
|
|
|
prefix = "WX"; |
|
|
|
|
} |
|
|
|
|
return prefix; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public int getOrder() { |
|
|
|
|
return -200; |
|
|
|
|