springBoot登录注册
时间: 2023-10-11 14:05:03 浏览: 35
好的,我可以为您提供一个简单的Spring Boot登录注册示例。首先,您需要创建一个Spring Boot项目并添加所需的依赖项。在这个示例中,我们将使用Spring Security来处理身份验证和授权,以及Thymeleaf作为模板引擎。
1. 添加依赖项
在您的pom.xml文件中添加以下依赖项:
```
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
```
2. 配置Spring Security
在您的应用程序中添加一个Security配置类,用于配置Spring Security。
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/register", "/css/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
}
```
这个配置类将允许用户访问/register和/css路径,而其他所有请求都需要身份验证。
3. 创建用户服务
创建一个UserService类实现UserDetailsService接口,用于从数据库加载用户信息。
```java
@Service
public class UserService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
AuthorityUtils.createAuthorityList("ROLE_USER"));
}
}
```
在这个示例中,我们使用了一个UserRepository接口来从数据库中获取用户。
4. 创建控制器
创建一个控制器用于处理登录和注册请求。
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/register")
public String showRegistrationForm(Model model) {
model.addAttribute("user", new User());
return "registration";
}
@PostMapping("/register")
public String processRegistrationForm(@ModelAttribute("user") User user) {
userService.save(user);
return "redirect:/login";
}
@GetMapping("/login")
public String showLoginForm() {
return "login";
}
}
```
这个控制器包含三个方法,用于显示注册和登录表单,以及处理注册表单提交。
5. 创建模板
最后,我们需要创建Thymeleaf模板来呈现我们的注册和登录表单。
registration.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Registration</title>
<link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<h1>Registration</h1>
<form th:action="@{/register}" th:object="${user}" method="post">
<div>
<label for="username">Username:</label>
<input type="text" id="username" th:field="*{username}"/>
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" th:field="*{password}"/>
</div>
<div>
<button type="submit">Register</button>
</div>
</form>
</body>
</html>
```
login.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Login</title>
<link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<h1>Login</h1>
<form th:action="@{/login}" method="post">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username"/>
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password"/>
</div>
<div>
<button type="submit">Login</button>
</div>
</form>
</body>
</html>
```
6. 运行应用
启动您的应用程序并访问http://localhost:8080/register,您将看到一个注册表单。填写表单并提交后,您将被重定向到登录页面。输入您的凭证并提交,您将被重定向到您的应用程序的主页。