|
@@ -1,20 +1,32 @@
|
|
package com.huimv.gateway.filter;
|
|
package com.huimv.gateway.filter;
|
|
|
|
|
|
|
|
|
|
|
|
+import com.huimv.common.utils.TokenUtil;
|
|
import com.huimv.gateway.utils.JwtUtils;
|
|
import com.huimv.gateway.utils.JwtUtils;
|
|
import io.jsonwebtoken.Claims;
|
|
import io.jsonwebtoken.Claims;
|
|
|
|
+import lombok.SneakyThrows;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
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.DataBuffer;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
|
+import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
+import org.apache.commons.codec.binary.Hex;
|
|
|
|
+
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.security.MessageDigest;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @Author yinhao
|
|
* @Author yinhao
|
|
@@ -28,43 +40,77 @@ public class TokenSignFilter implements GlobalFilter, Ordered {
|
|
@Autowired
|
|
@Autowired
|
|
private JwtUtils jwtUtils;
|
|
private JwtUtils jwtUtils;
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @SneakyThrows
|
|
@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();
|
|
ServerHttpResponse response = exchange.getResponse();
|
|
ServerHttpResponse response = exchange.getResponse();
|
|
|
|
|
|
- //登录接口不参与过滤
|
|
|
|
- if (request.getURI().getPath().contains("/login")) {
|
|
|
|
|
|
+
|
|
|
|
+ if (request.getURI().getPath().contains("/getToken")) {
|
|
return chain.filter(exchange);
|
|
return chain.filter(exchange);
|
|
}
|
|
}
|
|
|
|
|
|
- String token = request.getHeaders().getFirst(jwtUtils.getHeader());
|
|
|
|
- if (StringUtils.isEmpty(token)) {
|
|
|
|
|
|
+
|
|
|
|
+ Map<String,String> requestBody = exchange.getAttribute("cachedRequestBodyObject");
|
|
|
|
+ String token = requestBody.get("token");
|
|
|
|
+ if (!TokenUtil.verify(token)){
|
|
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
return response.setComplete();
|
|
return response.setComplete();
|
|
}
|
|
}
|
|
|
|
|
|
- try {
|
|
|
|
-
|
|
|
|
- Claims claims = jwtUtils.getClaimByToken(token);
|
|
|
|
- if (claims == null || jwtUtils.isTokenExpired(claims.getExpiration())) {
|
|
|
|
- response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
|
- return response.setComplete();
|
|
|
|
- //throw new RRException(jwtUtils.getHeader() + "失效,请重新登录", HttpStatus.UNAUTHORIZED.value());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Long id = Long.parseLong(claims.getSubject());
|
|
|
|
- log.info("find userId: {} from uri: {}", id, request.getURI());
|
|
|
|
- ServerHttpRequest serverHttpRequest = request.mutate().headers(httpHeaders -> httpHeaders.add("userId", id + "")).build();
|
|
|
|
- exchange.mutate().request(serverHttpRequest).build();
|
|
|
|
|
|
+ String random = requestBody.get("random");
|
|
|
|
+ String timestamp = requestBody.get("timestamp");
|
|
|
|
+ String sign = requestBody.get("sign");
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- //向客户端返回错误提示信息
|
|
|
|
|
|
+ MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
|
+ String data = random +";" +timestamp;
|
|
|
|
+ String oneSign = Hex.encodeHexString(md.digest(data.getBytes(StandardCharsets.UTF_8)));
|
|
|
|
+ oneSign =oneSign +"HuiMv";
|
|
|
|
+ String twoSign = Hex.encodeHexString(md.digest(oneSign.getBytes(StandardCharsets.UTF_8)));
|
|
|
|
+ if (!sign.equals(twoSign)){
|
|
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
return response.setComplete();
|
|
return response.setComplete();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ //登录接口不参与过滤
|
|
|
|
+// if (request.getURI().getPath().contains("/login")) {
|
|
|
|
+// return chain.filter(exchange);
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// String token = request.getHeaders().getFirst(jwtUtils.getHeader());
|
|
|
|
+// if (StringUtils.isEmpty(token)) {
|
|
|
|
+// response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
|
+// return response.setComplete();
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// try {
|
|
|
|
+//
|
|
|
|
+// Claims claims = jwtUtils.getClaimByToken(token);
|
|
|
|
+// if (claims == null || jwtUtils.isTokenExpired(claims.getExpiration())) {
|
|
|
|
+// response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
|
+// return response.setComplete();
|
|
|
|
+// //throw new RRException(jwtUtils.getHeader() + "失效,请重新登录", HttpStatus.UNAUTHORIZED.value());
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// Long id = Long.parseLong(claims.getSubject());
|
|
|
|
+// log.info("find userId: {} from uri: {}", id, request.getURI());
|
|
|
|
+// ServerHttpRequest serverHttpRequest = request.mutate().headers(httpHeaders -> httpHeaders.add("userId", id + "")).build();
|
|
|
|
+// exchange.mutate().request(serverHttpRequest).build();
|
|
|
|
+//
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+// //向客户端返回错误提示信息
|
|
|
|
+// response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
|
+// return response.setComplete();
|
|
|
|
+// }
|
|
|
|
+
|
|
return chain.filter(exchange);
|
|
return chain.filter(exchange);
|
|
}
|
|
}
|
|
|
|
|