From 4cb9af22a2d78585c7dcc50a6613be64a3d82cc7 Mon Sep 17 00:00:00 2001 From: egd <7769519+egberd@user.noreply.gitee.com> Date: Tue, 9 Sep 2025 09:39:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(web):=20=E7=A1=AE=E4=BF=9D=E5=9C=A8=20Bean?= =?UTF-8?q?=20=E5=88=9B=E5=BB=BA=E5=89=8D=E6=98=A0=E5=B0=84=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E8=AF=B7=E6=B1=82=E5=89=8D=E7=BC=80=20=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=EF=BC=9A=20=E5=BD=93=20app=20=E5=92=8C=20admin=20?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E6=8E=A5=E5=8F=A3=E5=9C=B0=E5=9D=80=E4=B8=80?= =?UTF-8?q?=E8=87=B4=E6=97=B6=EF=BC=88=E6=AF=94=E5=A6=82=EF=BC=9A/system/u?= =?UTF-8?q?ser/get=EF=BC=89=EF=BC=8C=E9=9C=80=E8=A6=81=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E5=89=8D=E7=BC=80=EF=BC=88admin-api=20=EF=BD=9C=20app-api?= =?UTF-8?q?=EF=BC=89=E6=9D=A5=E5=8C=BA=E5=88=86=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=20URI=20=E5=86=B2=E7=AA=81=E3=80=82=20=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9A=20-=20=E5=BD=93=20xss.enable=3Dtrue=20=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=BC=9A=E8=A7=A6=E5=8F=91=20TechXssAutoConfiguration?= =?UTF-8?q?=20=E4=B8=AD=20xssJacksonCustomizer=20=E7=9A=84=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=EF=BC=8C=E9=97=B4=E6=8E=A5=E8=A7=A6=E5=8F=91=20PathMa?= =?UTF-8?q?tcher=20=E6=B3=A8=E5=85=A5=EF=BC=8C=E8=B0=83=E7=94=A8=20Delegat?= =?UTF-8?q?ingWebMvcConfiguration.configurePathMatch=E3=80=82=20-=20?= =?UTF-8?q?=E6=AD=A4=E6=97=B6=20RequestMappingHandlerMapping=20=E7=9A=84?= =?UTF-8?q?=20mapping=20=E8=BF=98=E6=9C=AA=E5=8A=A0=E4=B8=8A=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E3=80=82=20-=20=E5=BD=93=20api-encrypt.enable=3Dtrue?= =?UTF-8?q?=20=E6=97=B6=EF=BC=8C=E6=8F=90=E5=89=8D=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E7=9A=84=20RequestMappingHandlerMapping=20=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=89=8D=E7=BC=80=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E9=87=8D=E5=A4=8D=E6=8A=A5=E9=94=99=E3=80=82?= =?UTF-8?q?=20=E8=A7=A3=E5=86=B3=EF=BC=9A=20-=20=E4=B8=8D=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=20DelegatingWebMvcConfiguration=20=E7=9A=84=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E9=A1=BA=E5=BA=8F=E3=80=82=20-=20=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E5=8D=B3=E4=BD=BF=E5=85=B6=E4=BB=96=20Bean=20=E6=8F=90?= =?UTF-8?q?=E5=89=8D=E8=A7=A6=E5=8F=91=20Mapping=20=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E4=B9=9F=E8=83=BD=E6=AD=A3=E7=A1=AE=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A=E5=89=8D=E7=BC=80=EF=BC=8C=E9=81=BF=E5=85=8D=20URI=20?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/config/YudaoWebAutoConfiguration.java | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java index 5e033b124..aff4ff173 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java @@ -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.GlobalResponseBodyHandler; import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; +import jakarta.servlet.Filter; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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.web.client.RestTemplateBuilder; 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.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; -import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; -import jakarta.annotation.Resource; -import jakarta.servlet.Filter; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.Predicate; @AutoConfiguration @EnableConfigurationProperties(WebProperties.class) -public class YudaoWebAutoConfiguration implements WebMvcConfigurer { +public class YudaoWebAutoConfiguration { - @Resource - private WebProperties webProperties; /** * 应用名 */ @Value("${spring.application.name}") private String applicationName; - @Override - public void configurePathMatch(PathMatchConfigurer configurer) { - configurePathMatch(configurer, webProperties.getAdminApi()); - configurePathMatch(configurer, webProperties.getAppApi()); + @Bean + public WebMvcRegistrations webMvcRegistrations(WebProperties webProperties) { + return new WebMvcRegistrations() { + @Override + public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { + var mapping = new RequestMappingHandlerMapping(); + mapping.setPathPrefixes(buildPrefixRules(webProperties)); // 实例化时就带上前缀 + return mapping; + } + }; + } + + /** + * 构建 prefix → 匹配条件 的映射 + */ + private Map>> buildPrefixRules(WebProperties webProperties) { + AntPathMatcher antPathMatcher = new AntPathMatcher("."); + Map>> rules = new LinkedHashMap<>(); + putRule(rules, webProperties.getAdminApi(), antPathMatcher); + putRule(rules, webProperties.getAppApi(), antPathMatcher); + return rules; } /** * 设置 API 前缀,仅仅匹配 controller 包下的 - * - * @param configurer 配置 - * @param api API 配置 */ - private void configurePathMatch(PathMatchConfigurer configurer, WebProperties.Api api) { - AntPathMatcher antPathMatcher = new AntPathMatcher("."); - configurer.addPathPrefix(api.getPrefix(), clazz -> clazz.isAnnotationPresent(RestController.class) - && antPathMatcher.match(api.getController(), clazz.getPackage().getName())); // 仅仅匹配 controller 包 + private void putRule(Map>> 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())); } @Bean