Spring Boot 集成 Security代码
时间: 2024-02-22 08:33:18 浏览: 33
Spring Boot 集成 Security 的代码如下:
1. 添加依赖
在 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
2. 配置 Security
在 Spring Boot 应用程序的主类中添加以下配置:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("{noop}password").roles("USER");
}
}
```
这个配置类继承了 WebSecurityConfigurerAdapter 类并重写了其中的 configure(HttpSecurity http) 和 configureGlobal(AuthenticationManagerBuilder auth) 方法。
在 configure(HttpSecurity http) 方法中,我们配置了安全策略。我们允许访问根路径和 /home 路径,并且要求认证任何其他请求。我们也配置了登录页面和注销。
在 configureGlobal(AuthenticationManagerBuilder auth) 方法中,我们配置了一个内存中的用户,这个用户的用户名为 user,密码为 password,角色为 USER。
3. 添加登录页面
在 src/main/resources/templates 目录下创建一个名为 login.html 的文件,添加以下内容:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login}" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/>
</div>
<div>
<label>Password:</label>
<input type="password" name="password"/>
</div>
<div>
<input type="submit" value="Log in"/>
</div>
</form>
</body>
</html>
```
4. 添加控制器
在 src/main/java 目录下创建一个名为 HomeController 的控制器,添加以下内容:
```java
@Controller
public class HomeController {
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/home")
public String home() {
return "home";
}
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/logout")
public String logout() {
return "logout";
}
}
```
这个控制器有四个方法,分别处理根路径、/home、/login 和 /logout 请求。我们需要创建名为 index.html 和 home.html 的 Thymeleaf 模板文件。
5. 创建模板文件
在 src/main/resources/templates 目录下创建名为 index.html 和 home.html 的 Thymeleaf 模板文件,分别添加以下内容:
index.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Boot Security Example</title>
</head>
<body>
<h1>Welcome!</h1>
<p>You can access the <a th:href="@{/home}">home page</a> without logging in.</p>
<p>You need to <a th:href="@{/login}">log in</a> to access other pages.</p>
</body>
</html>
```
home.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
<p>Welcome to the home page!</p>
<p><a th:href="@{/logout}">Log out</a></p>
</body>
</html>
```
6. 运行应用程序
现在,您可以运行应用程序并在浏览器中访问 localhost:8080。您将看到一个欢迎页面,其中包含一个指向 home 页面和登录页面的链接。单击 home 页面链接,您将被重定向到登录页面。在登录页面中输入用户名和密码(user/password),然后单击登录按钮。现在,您将被重定向到 home 页面,并看到欢迎消息和一个注销链接。单击注销链接,您将被重定向到登录页面。
相关推荐
![](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)
![](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)