|
@@ -3,12 +3,15 @@ package com.onemap.gateway.filter;
|
|
|
import java.nio.CharBuffer;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
|
|
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
@@ -24,9 +27,8 @@ import reactor.core.publisher.Flux;
|
|
|
* @author onemap
|
|
|
*/
|
|
|
@Component
|
|
|
-public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
|
|
|
-{
|
|
|
- private final static String[] VALIDATE_URL = new String[] { "/auth/login", "/auth/register" };
|
|
|
+public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object> {
|
|
|
+ private final static String[] VALIDATE_URL = new String[]{"/auth/login", "/auth/register"};
|
|
|
|
|
|
@Autowired
|
|
|
private ValidateCodeService validateCodeService;
|
|
@@ -38,34 +40,59 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
|
|
|
|
|
|
private static final String UUID = "uuid";
|
|
|
|
|
|
+ private static final String USERNAME = "username";
|
|
|
+
|
|
|
+ private static final String PASSWORD = "password";
|
|
|
+
|
|
|
@Override
|
|
|
- public GatewayFilter apply(Object config)
|
|
|
- {
|
|
|
+ public GatewayFilter apply(Object config) {
|
|
|
return (exchange, chain) -> {
|
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
|
|
|
|
// 非登录/注册请求或验证码关闭,不处理
|
|
|
- if (!StringUtils.containsAnyIgnoreCase(request.getURI().getPath(), VALIDATE_URL) || !captchaProperties.getEnabled())
|
|
|
- {
|
|
|
+ if (!StringUtils.containsAnyIgnoreCase(request.getURI().getPath(), VALIDATE_URL) || !captchaProperties.getEnabled()) {
|
|
|
return chain.filter(exchange);
|
|
|
}
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
String rspStr = resolveBodyFromRequest(request);
|
|
|
JSONObject obj = JSON.parseObject(rspStr);
|
|
|
validateCodeService.checkCaptcha(obj.getString(CODE), obj.getString(UUID));
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+
|
|
|
+
|
|
|
+ obj.put(PASSWORD, validateCodeService.decryptString(obj.getString(UUID), obj.getString(PASSWORD)));
|
|
|
+ obj.put(USERNAME, validateCodeService.decryptString(obj.getString(UUID), obj.getString(USERNAME)));
|
|
|
+ String modifiedBody = obj.toString();
|
|
|
+ validateCodeService.deletePrivateKey(obj.getString(UUID));
|
|
|
+ byte[] bodyBytes = modifiedBody.getBytes(StandardCharsets.UTF_8);
|
|
|
+ DataBuffer bodyDataBuffer = exchange.getResponse().bufferFactory().wrap(bodyBytes);
|
|
|
+ Flux<DataBuffer> modifiedBodyFlux = Flux.just(bodyDataBuffer);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.putAll(request.getHeaders());
|
|
|
+ headers.setContentLength(bodyBytes.length);
|
|
|
+
|
|
|
+ ServerHttpRequest decoratedRequest = new ServerHttpRequestDecorator(request) {
|
|
|
+ @Override
|
|
|
+ public HttpHeaders getHeaders() {
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Flux<DataBuffer> getBody() {
|
|
|
+ return modifiedBodyFlux;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return chain.filter(exchange.mutate().request(decoratedRequest).build());
|
|
|
+ } catch (Exception e) {
|
|
|
return ServletUtils.webFluxResponseWriter(exchange.getResponse(), e.getMessage());
|
|
|
}
|
|
|
- return chain.filter(exchange);
|
|
|
+// return chain.filter(exchange);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest)
|
|
|
- {
|
|
|
+ private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) {
|
|
|
// 获取请求体
|
|
|
Flux<DataBuffer> body = serverHttpRequest.getBody();
|
|
|
AtomicReference<String> bodyRef = new AtomicReference<>();
|