Spring security 知识笔记【自定义登录页面】
一、引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
二、配置Spring Security的登录页面路径
在WebSecurityConfig复写configure(HttpSecurityhttp)方法,复写登录页面的路径,如下示例代码:
package Eleven.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("123456")).roles("admin"); auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("123456")).roles("normal"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() // 定义哪些URL需要被保护、哪些不需要被保护 .antMatchers("/login").permitAll()// 设置所有人都可以访问登录页面 .anyRequest().authenticated() // 任何请求,登录后可以访问 .and() .formLogin().loginPage("/login") ; } }
三、自定义登录页面login.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>My Login Page</title> </head> <body> <div th:if="${param.error}"> 用户名或密码错误!!! </div> <div th:if="${param.logout}"> 登出成功!!! </div> <form th:action="@{/login}" method="post"> <div><label> 用户名: <input type="text" name="username"/> </label></div> <div><label> 密 码: <input type="password" name="password"/> </label></div> <div><input type="submit" value="登录"/></div> </form> </body> </html>
四、自定义index.html页面
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>Spring Security</title> </head> <body> <h1>欢迎使用Spring Security!</h1> </body> </html>
五、新建controller
package Eleven.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller //这里不能写成RestController,否则return后就是String类型了,而不是跳转到login.html public class HomeController { @GetMapping("/login") public String login(){ return "/login"; } @GetMapping({"","/","/index"}) public String index() { return "/index"; } }
分类:
show me the code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探