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

Spring Security登录页面无法找到,出现HTTP 404资源未找到错误

解决Spring Boot中/admin/login 404问题

问题分析

从控制台日志可以看出,请求/admin/login被路由到静态资源处理器,但未找到对应资源。你已将login.html放置在resources、static和templates目录,但路径结构或配置缺失导致无法访问。

可行解决方案

1. 调整静态文件结构(纯静态HTML场景)

将login.html移动到src/main/resources/static/admin/目录下,此时可通过/admin/login.html直接访问。若要保持/admin/login的访问路径,有两种处理方式:

  • 修改Spring Security配置中的loginPage参数为"/admin/login.html"
  • 添加控制器映射,将/admin/login指向静态页面:
@Controller
public class LoginController {
    @GetMapping("/admin/login")
    public String loginPage() {
        return "admin/login.html";
    }
}

2. 基于模板引擎配置(如Thymeleaf)

若使用templates目录,需依赖模板引擎并通过控制器返回视图名:

  • 添加Thymeleaf依赖(Maven示例):
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  • 将login.html放置在src/main/resources/templates/admin/目录下
  • 添加控制器:
@Controller
public class LoginController {
    @GetMapping("/admin/login")
    public String loginPage() {
        return "admin/login"; // 无需添加.html后缀,Thymeleaf会自动解析
    }
}

3. 验证Security配置

你的当前配置已将/admin/login加入permitAll列表,这部分无需调整。但需确保没有其他拦截规则覆盖该配置,比如全局的anyRequest().authenticated()需放在permitAll规则之后(若存在该规则)。

4. 清理缓存并重启

IDE缓存可能导致资源无法加载,清理项目缓存(如IntelliJ IDEA的File -> Invalidate Caches...)后重启项目,验证资源是否能正常访问。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 06:37:33