2
0

fix: 修复omada接口feign问题

This commit is contained in:
caiyuchao
2025-02-15 15:20:14 +08:00
parent e258254e2f
commit 65e49ffdc2
4 changed files with 24 additions and 32 deletions

View File

@@ -13,6 +13,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import org.wfc.common.core.constant.SecurityConstants;
import org.wfc.common.redis.service.RedisService; import org.wfc.common.redis.service.RedisService;
import org.wfc.omada.config.domain.vo.AuthorizeTokenVO; import org.wfc.omada.config.domain.vo.AuthorizeTokenVO;
import org.wfc.omada.config.domain.vo.OmadaResult; import org.wfc.omada.config.domain.vo.OmadaResult;
@@ -42,7 +43,6 @@ public class FeignConfig implements RequestInterceptor {
private static final String CLIENT_ID = "client_id"; private static final String CLIENT_ID = "client_id";
private static final String CLIENT_SECRET = "client_secret"; private static final String CLIENT_SECRET = "client_secret";
private static final String GRANT_TYPE = "grant_type"; private static final String GRANT_TYPE = "grant_type";
private static final String PRE_ACCESS_TOKEN = "AccessToken=";
private static final String REFRESH_TOKEN = "refresh_token"; private static final String REFRESH_TOKEN = "refresh_token";
private static final String AUTHORIZATION = "Authorization"; private static final String AUTHORIZATION = "Authorization";
private static final String CLIENT_CREDENTIALS = "client_credentials"; private static final String CLIENT_CREDENTIALS = "client_credentials";
@@ -66,12 +66,12 @@ public class FeignConfig implements RequestInterceptor {
} }
String accessToken = omadaResult.getResult().getAccessToken(); String accessToken = omadaResult.getResult().getAccessToken();
String refreshToken = omadaResult.getResult().getRefreshToken(); String refreshToken = omadaResult.getResult().getRefreshToken();
authorization = PRE_ACCESS_TOKEN + accessToken; authorization = SecurityConstants.PRE_ACCESS_TOKEN + accessToken;
// 保存访问令牌和刷新令牌到redis中 // 保存访问令牌和刷新令牌到redis中
redisService.setCacheObject(REDIS_ACCESS_TOKEN, accessToken, 7000L, TimeUnit.SECONDS); redisService.setCacheObject(REDIS_ACCESS_TOKEN, accessToken, 7000L, TimeUnit.SECONDS);
redisService.setCacheObject(REDIS_REFRESH_TOKEN, refreshToken, 13L, TimeUnit.DAYS); redisService.setCacheObject(REDIS_REFRESH_TOKEN, refreshToken, 13L, TimeUnit.DAYS);
} else { } else {
authorization = PRE_ACCESS_TOKEN + cacheAccessToken; authorization = SecurityConstants.PRE_ACCESS_TOKEN + cacheAccessToken;
} }
if (StrUtil.isNotBlank(authorization)) { if (StrUtil.isNotBlank(authorization)) {
// 更新最新的omadaUrl和omadacId // 更新最新的omadaUrl和omadacId

View File

@@ -27,6 +27,11 @@ public class SecurityConstants
*/ */
public static final String AUTHORIZATION_HEADER = "Authorization"; public static final String AUTHORIZATION_HEADER = "Authorization";
/**
* 授权信息值
*/
public static final String PRE_ACCESS_TOKEN = "AccessToken=";
/** /**
* 请求来源 * 请求来源
*/ */

View File

@@ -9,6 +9,7 @@ import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.ip.IpUtils; import org.wfc.common.core.utils.ip.IpUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map; import java.util.Map;
/** /**
@@ -17,39 +18,35 @@ import java.util.Map;
* @author wfc * @author wfc
*/ */
@Component @Component
public class FeignRequestInterceptor implements RequestInterceptor public class FeignRequestInterceptor implements RequestInterceptor {
{
@Override @Override
public void apply(RequestTemplate requestTemplate) public void apply(RequestTemplate requestTemplate) {
{
HttpServletRequest httpServletRequest = ServletUtils.getRequest(); HttpServletRequest httpServletRequest = ServletUtils.getRequest();
if (StringUtils.isNotNull(httpServletRequest)) if (StringUtils.isNotNull(httpServletRequest)) {
{
Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest); Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
// 传递用户信息请求头,防止丢失 // 传递用户信息请求头,防止丢失
String userId = headers.get(SecurityConstants.DETAILS_USER_ID); String userId = headers.get(SecurityConstants.DETAILS_USER_ID);
if (StringUtils.isNotEmpty(userId)) if (StringUtils.isNotEmpty(userId)) {
{
requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId); requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId);
} }
String userKey = headers.get(SecurityConstants.USER_KEY); String userKey = headers.get(SecurityConstants.USER_KEY);
if (StringUtils.isNotEmpty(userKey)) if (StringUtils.isNotEmpty(userKey)) {
{
requestTemplate.header(SecurityConstants.USER_KEY, userKey); requestTemplate.header(SecurityConstants.USER_KEY, userKey);
} }
String userName = headers.get(SecurityConstants.DETAILS_USERNAME); String userName = headers.get(SecurityConstants.DETAILS_USERNAME);
if (StringUtils.isNotEmpty(userName)) if (StringUtils.isNotEmpty(userName)) {
{
requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName); requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName);
} }
String authentication = headers.get(SecurityConstants.AUTHORIZATION_HEADER); String authentication = headers.get(SecurityConstants.AUTHORIZATION_HEADER);
if (StringUtils.isNotEmpty(authentication)) if (StringUtils.isNotEmpty(authentication)) {
{ Collection<String> headerValues = requestTemplate.headers().get(SecurityConstants.AUTHORIZATION_HEADER);
boolean hasToken = headerValues.stream().anyMatch(c -> c.startsWith(SecurityConstants.PRE_ACCESS_TOKEN));
if (!hasToken) {
requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication); requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication);
} }
}
String language = headers.get(SecurityConstants.CONTENT_LANGUAGE); String language = headers.get(SecurityConstants.CONTENT_LANGUAGE);
if (StringUtils.isNotEmpty(language)) if (StringUtils.isNotEmpty(language)) {
{
requestTemplate.header(SecurityConstants.CONTENT_LANGUAGE, language); requestTemplate.header(SecurityConstants.CONTENT_LANGUAGE, language);
} }
String platform = headers.get(SecurityConstants.DETAILS_PLATFORM); String platform = headers.get(SecurityConstants.DETAILS_PLATFORM);

View File

@@ -1,5 +1,6 @@
package org.wfc.gateway.filter; package org.wfc.gateway.filter;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -19,11 +20,8 @@ import org.wfc.common.core.utils.ServletUtils;
import org.wfc.common.core.utils.StringUtils; import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.redis.service.RedisService; import org.wfc.common.redis.service.RedisService;
import org.wfc.gateway.config.properties.IgnoreWhiteProperties; import org.wfc.gateway.config.properties.IgnoreWhiteProperties;
import io.jsonwebtoken.Claims;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Arrays;
/** /**
* 网关鉴权 * 网关鉴权
* *
@@ -41,10 +39,6 @@ public class AuthFilter implements GlobalFilter, Ordered
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
private static final String[] feignOmadaUrls = {"system/dashboard/overview", "system/dashboard/page", "schedule/job/run",
"system/client/list", "u/order", "u/order/paySuccess", "/payment/aliPay/callback", "/payment/wxPay/callback"};
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
{ {
@@ -89,10 +83,6 @@ public class AuthFilter implements GlobalFilter, Ordered
addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid); addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid);
addHeader(mutate, SecurityConstants.DETAILS_USERNAME, username); addHeader(mutate, SecurityConstants.DETAILS_USERNAME, username);
addHeader(mutate, SecurityConstants.DETAILS_PLATFORM, platform); addHeader(mutate, SecurityConstants.DETAILS_PLATFORM, platform);
// feign omada api 调用处理
if (Arrays.stream(feignOmadaUrls).anyMatch(url::contains)) {
removeHeader(mutate, SecurityConstants.AUTHORIZATION_HEADER);
}
// 内部请求来源参数清除 // 内部请求来源参数清除
removeHeader(mutate, SecurityConstants.FROM_SOURCE); removeHeader(mutate, SecurityConstants.FROM_SOURCE);
return chain.filter(exchange.mutate().request(mutate.build()).build()); return chain.filter(exchange.mutate().request(mutate.build()).build());