| | |
| | | package com.ruoyi.gateway.filter;
|
| | |
|
| | | import com.ruoyi.common.core.utils.web.WebUtils;
|
| | | import com.ruoyi.gateway.config.properties.IgnoreClientProperties;
|
| | | 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.http.HttpHeaders;
|
| | | import org.springframework.http.server.reactive.ServerHttpRequest;
|
| | | import org.springframework.http.server.reactive.ServerHttpResponse;
|
| | | import org.springframework.stereotype.Component;
|
| | |
| | | @Autowired
|
| | | private ValidateCodeService validateCodeService;
|
| | |
|
| | | @Autowired
|
| | | private IgnoreClientProperties ignoreClient;
|
| | | private static final String BASIC_ = "Basic ";
|
| | |
|
| | | private static final String CODE = "code";
|
| | |
|
| | | private static final String UUID = "uuid";
|
| | | |
| | | private static final String GRANT_TYPE = "grant_type";
|
| | | |
| | | private static final String REFRESH_TOKEN = "refresh_token";
|
| | |
|
| | | @Override
|
| | | public GatewayFilter apply(Object config)
|
| | |
| | | {
|
| | | return chain.filter(exchange);
|
| | | }
|
| | | |
| | | // 刷新token请求,不处理
|
| | | String grantType = request.getQueryParams().getFirst(GRANT_TYPE);
|
| | | if (StringUtils.containsIgnoreCase(request.getURI().getPath(), AUTH_URL) && StringUtils.containsIgnoreCase(grantType, REFRESH_TOKEN))
|
| | | {
|
| | | return chain.filter(exchange);
|
| | | }
|
| | |
|
| | | // 消息头存在内容,且不存在验证码参数,不处理
|
| | | String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
|
| | | if (StringUtils.isNotEmpty(header) && StringUtils.startsWith(header, BASIC_)
|
| | | && !request.getQueryParams().containsKey(CODE) && !request.getQueryParams().containsKey(UUID))
|
| | | {
|
| | | 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"));
|
| | | validateCodeService.checkCapcha(request.getQueryParams().getFirst(CODE),
|
| | | request.getQueryParams().getFirst(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())))));
|
| | | }
|