博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringSecurity-模拟数据库认证
阅读量:3929 次
发布时间:2019-05-23

本文共 2958 字,大约阅读时间需要 9 分钟。

模拟数据库认证

1、首先我们准备数据库,在这我们只需要准备一个user表,因为还未涉及到授权的过程。

在这里插入图片描述

2、导入依赖

这里我们使用的是mysqlmybatis-plus、以及lombok

com.baomidou
mybatis-plus-boot-starter
3.0.5
org.projectlombok
lombok
mysql
mysql-connector-java
runtime
5.1.27

3、编写实现类和配置类

自定义配置类SecurityConfig2

/** * 自定义配置类 * 实现从数据库获取用户名和密码 */@Configurationpublic class SecurityConfig2 extends WebSecurityConfigurerAdapter {
@Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder()); } @Bean PasswordEncoder getPasswordEncoder(){
return new BCryptPasswordEncoder(); }}

编写UserDetailsService的实现类MyUserDetailsService

  • UserDetailsService的作用:

    SpringSecurity中进行身份验证的是AuthenticationManager接口,ProviderManager是它的一个默认实现,但它并不用来处理身份认证,而是委托给配置好的AuthenticationProvider,每个AuthenticationProvider会轮流检查身份认证。检查后或者返回Authentication对象或者抛出异常。

    验证身份就是加载响应的UserDetails,看看是否和用户输入的账号、密码、权限等信息匹配。此步骤由实现AuthenticationProviderDaoAuthenticationProvider(它利用UserDetailsService验证用户名、密码和授权)处理。包含GrantedAuthorityUserDetails对象在构建 Authentication对象时填入数据。

    在真实的系统中,我们希望用户的信息来自数据库,而不是写死的,我们就需要实现UserDetailsService接口。

@Service("userDetailsService")public class MyUserDetailsService implements UserDetailsService {
@Autowired private UserMapper userMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 调用userMapper方法。根据用户名查询数据库 QueryWrapper
usersQueryWrapper = new QueryWrapper<>(); // where username = ? usersQueryWrapper.eq("username",username); Users users = userMapper.selectOne(usersQueryWrapper); // 判断 if(users == null){
// 没有用户名,认证失败 throw new UsernameNotFoundException("用户名不存在"); } List
authorityList = AuthorityUtils.commaSeparatedStringToAuthorityList("admin"); // 从查询数据库返回users对象中获取用户名和密码 return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),authorityList); }}

3、编写mapper

@Repositorypublic interface UserMapper extends BaseMapper
{
}

4、配置数据库连接

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/testspring.datasource.username=rootspring.datasource.password=root

5、编写controller测试访问

@Controller@RequestMapping("/test")public class LoginController {
@RequestMapping("/index") @ResponseBody public String index(){
return "hello index"; } @RequestMapping("/hello") @ResponseBody public String hello(){
return "new hello"; }}

输入url进行访问/test/index,跳转到SpringSecurity的默认登录页面

在这里插入图片描述

输入账号密码,进行用户认证,认证成功。

在这里插入图片描述

转载地址:http://whtgn.baihongyu.com/

你可能感兴趣的文章
LaTex论文排版 | (21) 图表caption居中显示
查看>>
深度学习 | (4) 分类问题的Label为啥是one-hot?
查看>>
LaTex论文排版 | (22) argmax、argmin下标使用方法及任意、存在符号
查看>>
深度学习 | (5) 2分类、多分类问题评价指标以及在sklearn中的使用
查看>>
机器阅读理解 | (2) 文本问答概述
查看>>
预训练语言模型 | (1) 概述
查看>>
预训练语言模型 | (2) transformer
查看>>
预训练语言模型 | (3) Bert
查看>>
预训练语言模型 | (4) AlBert
查看>>
预训练语言模型 | (5) StructBert和RoBerta
查看>>
GNN在文本分类上的应用 | (1) TextGCN
查看>>
GNN在文本分类上的应用 | (2) Text Level Graph Neural Network for Text Classification
查看>>
GNN在文本分类上的应用 | (3) TensorGCN
查看>>
SemEval2019Task3_ERC | (1) Affect Classification in Dialogue using Attentive BiLSTMs
查看>>
SemEval2019Task3_ERC | (2) Attentive Conversation Modeling for Emotion Detection and Classification
查看>>
SemEval2019Task3_ERC | (3) Using Deep Sentiment Analysis Models and Transfer Learning for ERC
查看>>
SemEval2019Task3_ERC | (4) Emotion detection in conversations through Tweets,CNN and LSTM DNN
查看>>
Python杂谈 | (15) 使用Pycharm执行带命令行参数的脚本
查看>>
从源码分析:分析Java中的StringBuilder
查看>>
Linux(Ubuntu18)中启动ssh时的报错
查看>>