SpringSecurity教程3 从数据库中获取密码登录
该博客基于springsecurity教程一,请先观看 教程一
本篇主要讲了从数据库中获取用户名和密码来完成用户的登录。
1。首先要操作数据库,就要先在pom.xml文件中引入依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 自动生成setter getter-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
2.在MySQL中创建一张users表,自己插入一条数据吧。待会就根据你的数据来登录。
3.创建一个文件夹entity,创建实体类。Users.java:
public class Users {
private int uid;
private String username;
private String password;
public int getId() {
return uid;
}
public void setId(int id) {
this.uid = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4创建一个mapper文件夹,UserMapper.java,调用这里面的方法来完成查找数据。
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.znzz.springboot_security.entity.Users;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<Users> {
}
5.打开之前创建的MyUserDetailService.java,通过QueryWrapper来查找数据。
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.znzz.springboot_security.mapper.UserMapper;
import com.znzz.springboot_security.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//调用mapper里面的方法查询。从数据库中得到记录。
QueryWrapper<Users> wrapper = new QueryWrapper();
wrapper.eq("username",username);
Users users = userMapper.selectOne(wrapper);
//判断。
if(users == null){
throw new UsernameNotFoundException("用户不存在!");
}
System.out.println(users);
List<GrantedAuthority> auths =
AuthorityUtils.commaSeparatedStringToAuthorityList("admins,ROLE_sale");
//将得到的用户名和密码返回。
return new User(users.getUsername(),
new BCryptPasswordEncoder().encode(users.getPassword()),auths);
}
}
6.打开application.properties,编辑数据库的信息:注意,数据库,用户名,密码改成自己的。
server.port=8111
#spring.security.user.name=lsg
#spring.security.user.password=1q2w3e4r5t
#mysql配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stubase?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
7.在你的启动类上加一个注解,扫描mapper,路径改成自己的
@MapperScan("com.znzz.springboot_security.mapper")
最后,重启启动类,运行测试。浏览器数据localhost:8111/test/hello,输入自己插入数据库的用户名和密码。
成功!下一篇博客会讲解自定义登录界面等。
项目源码 : 项目源码github