用springboot写一个登录
时间: 2024-04-29 13:23:36 浏览: 14
这里提供一个简单的登录示例,使用Spring Boot、Thymeleaf和Spring Security实现。
1. 创建Spring Boot项目
首先,我们需要创建一个Spring Boot项目。可以使用Spring Initializr快速创建,选择Web、Thymeleaf和Spring Security依赖。
2. 配置Spring Security
在pom.xml中添加Spring Security依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
在application.properties中添加一些基本的Spring Security配置:
```
spring.security.user.name=user
spring.security.user.password=password
spring.security.user.roles=USER
```
这将创建一个名为"user"、密码为"password"、角色为"USER"的用户,可以用于测试。
3. 创建登录页面
在src/main/resources/templates中创建login.html:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<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="Login"/>
</div>
</form>
</body>
</html>
```
这是一个简单的表单,用户可以输入用户名和密码进行登录。
4. 创建登录控制器
创建一个控制器类,将处理登录请求:
```
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
}
```
5. 配置Spring Security
创建一个配置类,配置Spring Security:
```
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/css/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/home")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("{noop}password").roles("USER");
}
}
```
这里配置了登录页面、成功页面、退出URL等。我们还创建了一个用户,用户名为"user",密码为"password",角色为"USER"。
6. 创建主页
在src/main/resources/templates中创建home.html:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Home</title>
</head>
<body>
<h1>Welcome</h1>
<p>You are logged in.</p>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Logout"/>
</form>
</body>
</html>
```
这是一个简单的欢迎页面,用户可以在此处退出登录。
7. 运行应用
现在,我们可以运行应用并测试登录功能了。访问http://localhost:8080/login,输入用户名和密码,登录成功后将重定向到http://localhost:8080/home。在主页上,用户可以看到欢迎消息,并在表单中退出。
这是一个简单的登录示例,可以在此基础上进行扩展,添加更多的功能。
相关推荐
![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)
![](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)