|
@@ -1,122 +1,118 @@
|
|
|
-//package com.huimv.gateway.filter;
|
|
|
-//
|
|
|
-//
|
|
|
-//import com.huimv.common.utils.TokenUtil;
|
|
|
-//import com.huimv.gateway.utils.JwtUtils;
|
|
|
-//import io.jsonwebtoken.Claims;
|
|
|
-//import lombok.SneakyThrows;
|
|
|
-//import lombok.extern.slf4j.Slf4j;
|
|
|
-//import org.apache.commons.lang.StringUtils;
|
|
|
-//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-//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.DataBuffer;
|
|
|
-//import org.springframework.http.HttpHeaders;
|
|
|
-//import org.springframework.http.HttpStatus;
|
|
|
-//import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
-//import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
-//import org.springframework.stereotype.Component;
|
|
|
-//import org.springframework.util.MultiValueMap;
|
|
|
-//import org.springframework.web.server.ServerWebExchange;
|
|
|
-//import reactor.core.publisher.Flux;
|
|
|
-//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
|
|
|
-// * @Date 2021/4/30 18:37
|
|
|
-// * @Description
|
|
|
-// */
|
|
|
-//@Slf4j
|
|
|
-//@Component
|
|
|
-//public class TokenSignFilter implements GlobalFilter, Ordered {
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// private JwtUtils jwtUtils;
|
|
|
-//
|
|
|
-//
|
|
|
-//
|
|
|
-// @SneakyThrows
|
|
|
-// @Override
|
|
|
-// public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
-//
|
|
|
-//
|
|
|
-//
|
|
|
-// ServerHttpRequest request = exchange.getRequest();
|
|
|
-// ServerHttpResponse response = exchange.getResponse();
|
|
|
-//
|
|
|
-//
|
|
|
-// if (request.getURI().getPath().contains("/getToken") ||request.getURI().getPath().contains("/management") ) {
|
|
|
+package com.huimv.gateway.filter;
|
|
|
+
|
|
|
+
|
|
|
+import com.huimv.common.utils.TokenUtil;
|
|
|
+import com.huimv.gateway.utils.JwtUtils;
|
|
|
+import io.jsonwebtoken.Claims;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+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.DataBuffer;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
+import reactor.core.publisher.Flux;
|
|
|
+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
|
|
|
+ * @Date 2021/4/30 18:37
|
|
|
+ * @Description
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class TokenSignFilter implements GlobalFilter, Ordered {
|
|
|
+
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @Override
|
|
|
+ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ServerHttpRequest request = exchange.getRequest();
|
|
|
+ ServerHttpResponse response = exchange.getResponse();
|
|
|
+
|
|
|
+
|
|
|
+ if (request.getURI().getPath().contains("/getToken") ||request.getURI().getPath().contains("/management") ) {
|
|
|
+ return chain.filter(exchange);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Map<String,String> requestBody = exchange.getAttribute("cachedRequestBodyObject");
|
|
|
+ String token = requestBody.get("token");
|
|
|
+ if (!TokenUtil.verify(token)){
|
|
|
+ response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
+ return response.setComplete();
|
|
|
+ }
|
|
|
+
|
|
|
+ String random = requestBody.get("random");
|
|
|
+ String timestamp = requestBody.get("timestamp");
|
|
|
+ String sign = requestBody.get("sign");
|
|
|
+
|
|
|
+ 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);
|
|
|
+ return response.setComplete();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //登录接口不参与过滤
|
|
|
+// if (request.getURI().getPath().contains("/login")) {
|
|
|
// return chain.filter(exchange);
|
|
|
// }
|
|
|
//
|
|
|
-//
|
|
|
-// Map<String,String> requestBody = exchange.getAttribute("cachedRequestBodyObject");
|
|
|
-// String token = requestBody.get("token");
|
|
|
-// if (!TokenUtil.verify(token)){
|
|
|
+// String token = request.getHeaders().getFirst(jwtUtils.getHeader());
|
|
|
+// if (StringUtils.isEmpty(token)) {
|
|
|
// response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
// return response.setComplete();
|
|
|
// }
|
|
|
//
|
|
|
-// String random = requestBody.get("random");
|
|
|
-// String timestamp = requestBody.get("timestamp");
|
|
|
-// String sign = requestBody.get("sign");
|
|
|
+// 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();
|
|
|
//
|
|
|
-// 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)){
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// //向客户端返回错误提示信息
|
|
|
// response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
|
|
// 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);
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public int getOrder() {
|
|
|
-// return 0;
|
|
|
-// }
|
|
|
-//}
|
|
|
+
|
|
|
+ return chain.filter(exchange);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getOrder() {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|