Pre Merge pull request !209 from egd/fix-PathMatcher

pull/209/MERGE
egd 2025-09-09 01:52:33 +00:00 committed by Gitee
commit 081beec593
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 35 additions and 18 deletions

View File

@ -7,11 +7,13 @@ 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 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;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
@ -23,40 +25,55 @@ import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter; import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import jakarta.annotation.Resource; import java.util.LinkedHashMap;
import jakarta.servlet.Filter; import java.util.Map;
import java.util.function.Predicate;
@AutoConfiguration @AutoConfiguration
@EnableConfigurationProperties(WebProperties.class) @EnableConfigurationProperties(WebProperties.class)
public class YudaoWebAutoConfiguration implements WebMvcConfigurer { public class YudaoWebAutoConfiguration {
@Resource
private WebProperties webProperties;
/** /**
* *
*/ */
@Value("${spring.application.name}") @Value("${spring.application.name}")
private String applicationName; private String applicationName;
@Bean
public WebMvcRegistrations webMvcRegistrations(WebProperties webProperties) {
return new WebMvcRegistrations() {
@Override @Override
public void configurePathMatch(PathMatchConfigurer configurer) { public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
configurePathMatch(configurer, webProperties.getAdminApi()); var mapping = new RequestMappingHandlerMapping();
configurePathMatch(configurer, webProperties.getAppApi()); mapping.setPathPrefixes(buildPrefixRules(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;
} }
/** /**
* API controller * API controller
*
* @param configurer
* @param api API
*/ */
private void configurePathMatch(PathMatchConfigurer configurer, WebProperties.Api api) { private void putRule(Map<String, Predicate<Class<?>>> rules, WebProperties.Api api, AntPathMatcher matcher) {
AntPathMatcher antPathMatcher = new AntPathMatcher("."); if (api == null || api.getPrefix() == null) {
configurer.addPathPrefix(api.getPrefix(), clazz -> clazz.isAnnotationPresent(RestController.class) return;
&& antPathMatcher.match(api.getController(), clazz.getPackage().getName())); // 仅仅匹配 controller 包 }
rules.put(api.getPrefix(), // api前缀
clazz -> clazz.isAnnotationPresent(RestController.class)
&& matcher.match(api.getController(), clazz.getPackage().getName()));
} }
@Bean @Bean