Spring Boot整合Spring Securit自定义登录
时间: 2024-02-22 16:51:44 浏览: 34
以下是Spring Boot整合Spring Security自定义登录的步骤:
1. 引入依赖:在pom.xml文件中引入Spring Security和Thymeleaf的依赖。
2. 配置Spring Security:在Spring Boot的配置类中添加@EnableWebSecurity注解,并继承WebSecurityConfigurerAdapter类,重写configure(HttpSecurity http)方法,配置登录页面、登录请求、登录成功后的跳转等信息。
3. 自定义登录页面:在resources/templates目录下创建login.html文件,使用Thymeleaf模板引擎实现自定义登录页面。
4. 自定义登录表单:在login.html文件中添加表单,表单中包含用户名和密码两个输入框。
5. 自定义登录请求:在Spring Boot的控制器类中添加/login请求的处理方法,该方法接收用户名和密码参数,并使用Spring Security提供的AuthenticationManager进行认证。
6. 自定义认证逻辑:在Spring Security的配置类中重写configure(AuthenticationManagerBuilder auth)方法,实现自定义的认证逻辑。
7. 自定义登录成功后的跳转:在Spring Security的配置类中重写configure(HttpSecurity http)方法,配置登录成功后的跳转页面。
下面是示例代码:
1. 引入依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
```
2. 配置Spring Security:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/home")
.permitAll()
.and()
.logout()
.permitAll();
}
}
```
3. 自定义登录页面:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form th:action="@{/login}" method="post">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required autofocus>
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit">Login</button>
</form>
</body>
</html>
```
4. 自定义登录表单:在login.html文件中添加表单,表单中包含用户名和密码两个输入框。
5. 自定义登录请求:
```java
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
@PostMapping("/login")
public String doLogin(@RequestParam String username, @RequestParam String password) {
// 使用AuthenticationManager进行认证
return "redirect:/home";
}
}
```
6. 自定义认证逻辑:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
}
```
7. 自定义登录成功后的跳转:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/home")
.permitAll()
.and()
.logout()
.permitAll();
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)