springboot注册登录
时间: 2023-11-07 17:58:40 浏览: 19
Spring Boot 是一个快速开发框架,能够快速创建 Spring 应用程序,提供了很多开箱即用的组件,简化了开发过程。下面是一个简单的 Spring Boot 注册登录的实现步骤:
1. 创建一个 Spring Boot 项目。
2. 添加依赖。
在 pom.xml 文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
3. 创建用户实体类。
创建一个 User 实体类,用于存储用户信息。
```
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String username;
private String password;
// 省略 getter 和 setter
}
```
4. 创建用户仓库。
创建一个 UserRepository 接口,继承 JpaRepository 接口,用于操作数据库。
```
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
5. 创建用户服务。
创建一个 UserService 接口和实现类,用于注册和登录。
```
public interface UserService {
void register(User user);
User login(String username, String password);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public void register(User user) {
userRepository.save(user);
}
@Override
public User login(String username, String password) {
User user = userRepository.findByUsername(username);
if (user != null && user.getPassword().equals(password)) {
return user;
}
return null;
}
}
```
6. 创建安全配置。
创建一个 SecurityConfig 类,继承 WebSecurityConfigurerAdapter 类,用于配置安全相关的信息。
```
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/register", "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(username -> userService.findByUsername(username));
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```
7. 创建控制器。
创建一个 UserController 类,用于处理注册和登录请求。
```
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public void register(@RequestBody User user) {
userService.register(user);
}
@PostMapping("/login")
public User login(@RequestParam String username, @RequestParam String password) {
return userService.login(username, password);
}
}
```
8. 运行应用程序。
运行应用程序,访问 http://localhost:8080/register 进行注册,访问 http://localhost:8080/login 进行登录。
以上就是一个简单的 Spring Boot 注册登录的实现步骤。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)