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

Spring Boot 3表单登录异常:提交凭证后返回404错误

表单登录提交后404且未触发指定控制器的排查方向

1. SecurityFilterChain配置偏差

  • 表单登录的loginProcessingUrl未指向/v1/auth/login
    检查配置代码,确保formLogin()块内设置了正确的处理地址:
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .formLogin(form -> form
                .loginPage("/v1/view/login") // 自定义登录页面路径
                .loginProcessingUrl("/v1/auth/login") // 必须与表单提交地址完全一致
                .permitAll()
            );
        return http.build();
    }
    
  • 未放行/v1/auth/login的POST请求
    Spring Security默认拦截所有请求,需确保该地址被允许匿名访问,避免因权限拦截返回404。

2. Thymeleaf登录表单提交地址错误

  • 表单action属性未正确设置为/v1/auth/login
    检查登录页面的表单代码:
    <form th:action="@{/v1/auth/login}" method="post">
        <!-- 用户名、密码输入框及提交按钮 -->
    </form>
    
    注意:表单必须使用POST方法,Spring Security的表单登录逻辑默认只接收POST请求。

3. 登录处理控制器映射配置错误

  • 控制器的@PostMapping未匹配/v1/auth/login
    检查控制器代码:
    @Controller
    @RequestMapping("/v1/auth")
    public class LoginAuthController {
        @PostMapping("/login") // 确保映射路径与loginProcessingUrl完全一致
        public String handleLogin(HttpServletRequest request) {
            // 登录逻辑处理
            return "redirect:/home";
        }
    }
    
    重要提醒:Spring Security的loginProcessingUrl会接管该地址的POST请求,无需自定义控制器处理——这是常见误区。如果强行自定义控制器但路径不匹配,就会出现404。

4. 路径匹配规则冲突

  • Spring Boot 3.x默认使用PATH_PATTERN_PARSER路径匹配策略,若控制器路径用了Ant风格但未兼容,可能导致匹配失败。可在application.properties中修改策略:
    spring.mvc.pathmatch.matching-strategy=ant-path-matcher
    

5. 自定义拦截器/过滤器干扰

  • 自定义的拦截器、过滤器可能拦截了/v1/auth/login的POST请求,导致请求无法到达目标逻辑。检查拦截器配置,确保该地址被排除在拦截范围外。

内容的提问来源于stack exchange,提问作者A M

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 10:03:12