springboot登录注册项目代码
时间: 2023-09-04 15:12:14 浏览: 93
很抱歉,我无法提供完整的代码,但我可以给出一个基本的示例代码,帮助您快速入门。
首先,需要在pom.xml中引入Spring Boot、Spring Security、MyBatis等依赖。
```xml
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
```
接着,创建一个用户实体类User,包含id、用户名和密码三个属性。
```java
public class User {
private int id;
private String username;
private String password;
// getter和setter省略
}
```
然后,创建一个用户Dao接口UserDao,定义增删改查等方法。
```java
public interface UserDao {
int addUser(User user);
int deleteUser(int id);
int updateUser(User user);
User getUserById(int id);
User getUserByUsername(String username);
}
```
接着,创建一个用户Service接口UserService,定义注册和登录等方法。
```java
public interface UserService {
int register(User user);
User login(String username, String password);
}
```
然后,创建一个用户ServiceImpl类UserServiceImpl,实现UserService接口。
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public int register(User user) {
User u = userDao.getUserByUsername(user.getUsername());
if (u != null) {
throw new RuntimeException("用户名已存在");
}
return userDao.addUser(user);
}
@Override
public User login(String username, String password) {
User user = userDao.getUserByUsername(username);
if (user == null) {
throw new RuntimeException("用户名不存在");
}
if (!user.getPassword().equals(password)) {
throw new RuntimeException("密码不正确");
}
return user;
}
}
```
接着,创建一个WebSecurityConfig类,配置Spring Security。
```java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/register", "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.permitAll()
.and()
.logout()
.permitAll();
}
}
```
最后,创建一个UserController类,处理注册和登录请求。
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/register")
public String registerPage() {
return "register";
}
@PostMapping("/register")
public String register(User user) {
userService.register(user);
return "redirect:/login";
}
@GetMapping("/login")
public String loginPage() {
return "login";
}
@PostMapping("/login")
public String login(String username, String password, HttpSession session) {
User user = userService.login(username, password);
session.setAttribute("user", user);
return "redirect:/";
}
}
```
以上是一个基本的Spring Boot登录注册项目的示例代码,仅供参考。具体实现还需要根据实际需求进行修改和完善。
阅读全文