fix:【framework 框架】确保在 Bean 创建前映射应用请求前缀

master
YunaiV 5 months ago
parent fb47ed6c14
commit ec3a391981

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

Loading…
Cancel
Save