|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package cn.iocoder.yudao.framework.web.config;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.filter.CacheRequestBodyFilter;
|
|
|
|
|
@ -7,6 +8,7 @@ import cn.iocoder.yudao.framework.web.core.filter.DemoFilter;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.handler.GlobalResponseBodyHandler;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
import jakarta.servlet.Filter;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
|
|
|
@ -27,7 +29,6 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
|
import org.springframework.web.filter.CorsFilter;
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
|
|
|
|
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
|
|
|
|
@ -44,36 +45,39 @@ public class YudaoWebAutoConfiguration {
|
|
|
|
|
@Bean
|
|
|
|
|
public WebMvcRegistrations webMvcRegistrations(WebProperties webProperties) {
|
|
|
|
|
return new WebMvcRegistrations() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
|
|
|
|
|
var mapping = new RequestMappingHandlerMapping();
|
|
|
|
|
mapping.setPathPrefixes(buildPrefixRules(webProperties)); // 实例化时就带上前缀
|
|
|
|
|
// 实例化时就带上前缀
|
|
|
|
|
mapping.setPathPrefixes(buildPathPrefixes(webProperties));
|
|
|
|
|
return mapping;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构建 prefix → 匹配条件 的映射
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, Predicate<Class<?>>> buildPrefixRules(WebProperties webProperties) {
|
|
|
|
|
AntPathMatcher antPathMatcher = new AntPathMatcher(".");
|
|
|
|
|
Map<String, Predicate<Class<?>>> rules = new LinkedHashMap<>();
|
|
|
|
|
putRule(rules, webProperties.getAdminApi(), antPathMatcher);
|
|
|
|
|
putRule(rules, webProperties.getAppApi(), antPathMatcher);
|
|
|
|
|
return rules;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 构建 prefix → 匹配条件的映射
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, Predicate<Class<?>>> buildPathPrefixes(WebProperties webProperties) {
|
|
|
|
|
AntPathMatcher antPathMatcher = new AntPathMatcher(".");
|
|
|
|
|
Map<String, Predicate<Class<?>>> pathPrefixes = Maps.newLinkedHashMapWithExpectedSize(2);
|
|
|
|
|
putPathPrefix(pathPrefixes, webProperties.getAdminApi(), antPathMatcher);
|
|
|
|
|
putPathPrefix(pathPrefixes, webProperties.getAppApi(), antPathMatcher);
|
|
|
|
|
return pathPrefixes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置 API 前缀,仅仅匹配 controller 包下的
|
|
|
|
|
*/
|
|
|
|
|
private void putRule(Map<String, Predicate<Class<?>>> rules, WebProperties.Api api, AntPathMatcher matcher) {
|
|
|
|
|
if (api == null || api.getPrefix() == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rules.put(api.getPrefix(), // api前缀
|
|
|
|
|
clazz -> clazz.isAnnotationPresent(RestController.class)
|
|
|
|
|
&& matcher.match(api.getController(), clazz.getPackage().getName()));
|
|
|
|
|
/**
|
|
|
|
|
* 设置 API 前缀,仅仅匹配 controller 包下的
|
|
|
|
|
*/
|
|
|
|
|
private void putPathPrefix(Map<String, Predicate<Class<?>>> pathPrefixes, WebProperties.Api api, AntPathMatcher matcher) {
|
|
|
|
|
if (api == null || StrUtil.isEmpty(api.getPrefix())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pathPrefixes.put(api.getPrefix(), // api 前缀
|
|
|
|
|
clazz -> clazz.isAnnotationPresent(RestController.class)
|
|
|
|
|
&& matcher.match(api.getController(), clazz.getPackage().getName()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|