You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Spring Boot 2.6后配置permitAll的端点无法访问跳转登录页

问题根因
  • 路径匹配规则写法错误:你当前配置的mvcMatchers("presentations")没有以斜杠/开头,Spring MVC的路径匹配默认基于context-path后的绝对路径,少了开头的斜杠会导致匹配不到实际访问的地址
  • Spring Boot 2.6.0 版本默认关闭了尾部斜杠匹配特性:升级前/presentations和/presentations/会被识别为同一个路径,升级后二者是完全独立的路径,如果你访问的地址带尾部斜杠,就无法命中你配置的放行规则,会触发全局认证逻辑跳转到登录页

注意:如果项目中配置了server.servlet.context-path,mvcMatchers里的路径不需要带context-path前缀,该注解会自动适配上下文路径后的路径。

解决方案

方案1:直接修正安全配置的匹配规则

直接在放行规则里覆盖两种路径格式即可,修改后的配置如下:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .mvcMatchers("/presentations", "/presentations/")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin();
    }

    // ...
}

方案2:全局恢复尾部斜杠匹配逻辑

如果你希望整个项目保持升级前的路径匹配逻辑,不需要逐个修改安全配置的匹配规则,可以通过Spring MVC配置开启尾部斜杠匹配:

  1. 配置文件方式(application.properties):
spring.mvc.pathmatch.use-trailing-slash-match=true
  1. 代码配置方式:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseTrailingSlashMatch(true);
    }
}

内容的提问来源于stack exchange,提问作者Gerardo Roza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 17:27:05