springboot怎么才能实现登录功能,以及登录判断用户名和密码
时间: 2023-11-29 21:06:49 浏览: 127
springboot+mybatis-plus+jquery+filter+ajax 实现登录功能
实现登录功能的步骤如下:
1. 添加Spring Security依赖
在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
2. 配置Spring Security
在Spring Boot应用程序的主类上添加`@EnableWebSecurity`注解,然后创建一个继承自`WebSecurityConfigurerAdapter`的配置类,重写其中的`configure(HttpSecurity http)`和`configure(AuthenticationManagerBuilder auth)`方法。
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/home")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/login")
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
}
```
其中,`MyUserDetailsService`是自定义的用户认证服务,用户认证服务需要实现`UserDetailsService`接口,重写其中的`loadUserByUsername`方法。
```java
@Service
public class MyUserDetailsService 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("用户不存在");
}
return new CustomUserDetails(user);
}
}
```
`UserRepository`是自定义的用户数据访问接口,`CustomUserDetails`是自定义的用户详情类,实现`UserDetails`接口,重写其中的方法。
3. 登录页面
在Spring Boot应用程序中,可以将登录页面放在`src/main/resources/templates`目录下,例如`login.html`。
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form action="/login" method="post">
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</div>
<div>
<button type="submit">登录</button>
</div>
</form>
</body>
</html>
```
4. 登录判断用户名和密码
登录判断用户名和密码应该在用户认证服务中实现,这里以使用JPA方式访问MySQL数据库为例。假设有一个名为`User`的实体类,其中包含`username`和`password`两个属性,对应数据库表中的两个字段。`UserRepository`是自定义的用户数据访问接口,继承自`JpaRepository`。
```java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
在`MyUserDetailsService`中,可以调用`UserRepository`中的方法获取用户信息,然后将其封装成`CustomUserDetails`返回。
```java
@Service
public class MyUserDetailsService 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("用户不存在");
}
return new CustomUserDetails(user);
}
}
```
其中,`CustomUserDetails`的实现如下:
```java
public class CustomUserDetails implements UserDetails {
private User user;
public CustomUserDetails(User user) {
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
return authorities;
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
```
阅读全文