| | |
| | | package com.ruoyi.gateway.filter;
|
| | |
|
| | | import com.ruoyi.common.core.utils.web.WebUtils;
|
| | | import com.ruoyi.gateway.config.properties.IgnoreClientProperties;
|
| | | 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.server.reactive.ServerHttpRequest;
|
| | | import org.springframework.http.server.reactive.ServerHttpResponse;
|
| | | import org.springframework.stereotype.Component;
|
| | | import com.alibaba.fastjson.JSON;
|
| | | import com.alibaba.fastjson.JSONObject;
|
| | | import com.ruoyi.common.core.utils.StringUtils;
|
| | | import com.ruoyi.common.core.web.domain.AjaxResult;
|
| | | import com.ruoyi.gateway.service.ValidateCodeService;
|
| | | import reactor.core.publisher.Flux;
|
| | | import reactor.core.publisher.Mono;
|
| | |
|
| | | /**
|
| | |
| | | @Component
|
| | | public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
|
| | | {
|
| | | private final static String AUTH_URL = "/oauth/token";
|
| | | private final static String AUTH_URL = "/auth/login";
|
| | |
|
| | | @Autowired
|
| | | private ValidateCodeService validateCodeService;
|
| | |
|
| | | @Autowired
|
| | | private IgnoreClientProperties ignoreClient;
|
| | | private static final String CODE = "code";
|
| | |
|
| | | private static final String UUID = "uuid";
|
| | |
|
| | | @Override
|
| | | public GatewayFilter apply(Object config)
|
| | |
| | | {
|
| | | return chain.filter(exchange);
|
| | | }
|
| | |
|
| | | try
|
| | | {
|
| | | // swagger的oauth2.0验证码放行操作
|
| | | String[] clientInfos = WebUtils.getClientId(request);
|
| | | if (ignoreClient.getClients().contains(clientInfos[0]))
|
| | | {
|
| | | return chain.filter(exchange);
|
| | | }
|
| | |
|
| | | validateCodeService.checkCapcha(request.getQueryParams().getFirst("code"),
|
| | | request.getQueryParams().getFirst("uuid"));
|
| | | String rspStr = resolveBodyFromRequest(request);
|
| | | JSONObject obj = JSONObject.parseObject(rspStr);
|
| | | validateCodeService.checkCapcha(obj.getString(CODE), obj.getString(UUID));
|
| | | }
|
| | | catch (Exception e)
|
| | | {
|
| | | ServerHttpResponse response = exchange.getResponse();
|
| | | response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
|
| | | return exchange.getResponse().writeWith(
|
| | | Mono.just(response.bufferFactory().wrap(JSON.toJSONBytes(AjaxResult.error(e.getMessage())))));
|
| | | }
|
| | | return chain.filter(exchange);
|
| | | };
|
| | | }
|
| | |
|
| | | private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest)
|
| | | {
|
| | | // 获取请求体
|
| | | Flux<DataBuffer> body = serverHttpRequest.getBody();
|
| | | AtomicReference<String> bodyRef = new AtomicReference<>();
|
| | | body.subscribe(buffer -> {
|
| | | CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
|
| | | DataBufferUtils.release(buffer);
|
| | | bodyRef.set(charBuffer.toString());
|
| | | });
|
| | | return bodyRef.get();
|
| | | }
|
| | | }
|