mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 12:52:07 +08:00
1、新增网关支持固定接口不传token调用,新增ignoreToken配置;2、新增小程序专用单板查询、单板查询订单信息接口;
This commit is contained in:
@@ -26,4 +26,9 @@ public class CfGatewayProperties {
|
||||
* 需要忽略打印requestBody和responseBody的请求地址列表
|
||||
*/
|
||||
private Set<String> accessLogIgnoreUrls = Collections.emptySet();
|
||||
|
||||
/**
|
||||
* 需要忽略token的请求地址列表
|
||||
*/
|
||||
private Set<String> ignoreTokenUrls = Collections.emptySet();
|
||||
}
|
||||
|
||||
+50
@@ -1,21 +1,25 @@
|
||||
package com.cf.imes.gateway.filter.security;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.cf.imes.framework.common.exception.ErrorCode;
|
||||
import com.cf.imes.framework.common.exception.ServiceException;
|
||||
import com.cf.imes.framework.common.pojo.CommonResult;
|
||||
import com.cf.imes.framework.common.util.json.JsonUtils;
|
||||
import com.cf.imes.gateway.config.CfGatewayProperties;
|
||||
import com.cf.imes.gateway.util.SecurityFrameworkUtils;
|
||||
import com.cf.imes.gateway.util.WebFrameworkUtils;
|
||||
import com.cf.imes.module.system.api.oauth2.OAuth2TokenApi;
|
||||
import com.cf.imes.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -45,6 +49,13 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
|
||||
private static final TypeReference<CommonResult> TOKEN_RESULT
|
||||
= new TypeReference<CommonResult>() {};
|
||||
|
||||
private static final TypeReference<CommonResult<String>> IGNORE_TOKEN_SOURCE_RESULT
|
||||
= new TypeReference<>() {
|
||||
};
|
||||
|
||||
@Resource
|
||||
private CfGatewayProperties cfGatewayProperties;
|
||||
|
||||
/**
|
||||
* 空的 LoginUser 的结果
|
||||
*
|
||||
@@ -72,6 +83,24 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
|
||||
// 情况一,如果没有 Token 令牌,则直接继续 filter
|
||||
String token = SecurityFrameworkUtils.obtainAuthorization(exchange);
|
||||
if (CharSequenceUtil.isEmpty(token)) {
|
||||
// 是否忽略token的外部请求,如小程序用
|
||||
AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
String url = exchange.getRequest().getURI().getPath();
|
||||
for (String ignoreUrl : cfGatewayProperties.getIgnoreTokenUrls()) {
|
||||
if (pathMatcher.match(ignoreUrl, url)) {
|
||||
return getIgnoreTokenSource()
|
||||
.defaultIfEmpty("")
|
||||
.flatMap(result -> {
|
||||
String dataCode = buildSource(result);
|
||||
LoginUser user = new LoginUser().setDataCode(dataCode);
|
||||
ServerWebExchange newExchange = exchange.mutate()
|
||||
.request(builder -> SecurityFrameworkUtils.setLoginUserHeader1(builder, user))
|
||||
.build();
|
||||
return chain.filter(newExchange);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
@@ -111,6 +140,12 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
|
||||
.retrieve().bodyToMono(String.class);
|
||||
}
|
||||
|
||||
private Mono<String> getIgnoreTokenSource() {
|
||||
return webClient.get()
|
||||
.uri(OAuth2TokenApi.IGNORE_TOKEN_SOURCE_GET)
|
||||
.retrieve().bodyToMono(String.class);
|
||||
}
|
||||
|
||||
private LoginUser buildUser(String body) {
|
||||
|
||||
CommonResult commonResult = JsonUtils.parseObject(body, TOKEN_RESULT);
|
||||
@@ -155,6 +190,21 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
|
||||
.setProductId(tokenInfo.getProductId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建忽略令牌的数据源请求结果
|
||||
*
|
||||
* @param body
|
||||
* @return
|
||||
*/
|
||||
private String buildSource(String body) {
|
||||
CommonResult<String> commonResult = JsonUtils.parseObject(body, IGNORE_TOKEN_SOURCE_RESULT);
|
||||
if (ObjectUtil.isNotNull(commonResult)) {
|
||||
return commonResult.getData();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return -100; // 和 Spring Security Filter 的顺序对齐
|
||||
|
||||
@@ -124,4 +124,23 @@ public class SecurityFrameworkUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略令牌将 data-code 并设置到 请求头
|
||||
*
|
||||
* @param builder 请求
|
||||
* @param user 用户
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static void setLoginUserHeader1(ServerHttpRequest.Builder builder, LoginUser user) {
|
||||
try {
|
||||
String userStr = JsonUtils.toJsonString(user);
|
||||
userStr = URLEncoder.encode(userStr, StandardCharsets.UTF_8.name()); // 编码,避免中文乱码
|
||||
builder.header(LOGIN_USER_HEADER, userStr);
|
||||
} catch (Exception ex) {
|
||||
log.error("[setLoginUserHeader][序列化 user({}) 发生异常]", user, ex);
|
||||
throw ex;
|
||||
}
|
||||
builder.header(DATA_CODE, user.getDataCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,9 @@ logging:
|
||||
|
||||
chenfeng:
|
||||
gateway:
|
||||
ignore-token-urls:
|
||||
- /admin-api/executor/plate/wechat/miniprogram/cadview/info/*
|
||||
- /admin-api/executor/plate/wechat/miniprogram/cadview/order/info/*
|
||||
accessLogEnable: false
|
||||
access-log-ignore-urls:
|
||||
- /report/template
|
||||
|
||||
Reference in New Issue
Block a user