五、Spring Boot集成Spring Security之认证流程2
二、概要说明
- 上文已详细介绍了四、Spring Boot集成Spring Security之认证流程
- 本文则着重介绍用户名密码认证过滤器UsernamePasswordAuthenticationFilter的实现原理过程
- 认证管理器(authenticationManager)
- 认证提供者(AuthenticationProvider)
- 自定义配置用户名密码实现(UserDetailsService)
三、UsernamePasswordAuthenticationFilter
1、结构及作用
- 继承AbstractAuthenticationProcessingFilter
- 初始化请求地址
- 初始化authenticationManager
- 初始化successHandler
- 初始化failureHandler
- 实现过滤器入口doFilter方法
- doFilter方法调用抽象方法attemptAuthentication,attemptAuthentication供子类实现完成用户名密码验证业务
- 认证成功时更新安全上下文,并调用successHandler.onAuthenticationSuccess
- 认证失败时删除安全上下文,并调用failureHandler.onAuthenticationFailure
- 实现attemptAuthentication方法
- 从请求中获取用户名密码
- 生成未认证的Authentication
- 调用authenticationManager的authenticate方法完成用户名密码验证
四、认证管理器(AuthenticationManager)
1、作用
- 完成Authentication的认证
2、ProviderManager(默认实现)
- ProviderManager实现AuthenticationManager接口
- AuthenticationManager的作用的是完成Authentication的认证
- 但是ProviderManager并未直接完成Authentication的认证
- 而是提供一个AuthenticationProvider集合
- 遍历AuthenticationProvider集合来完成Authentication的认证
- 当需要多种认证方式时,可以注册自定义的AuthenticationProvider,后续介绍注册方式
五、AuthenticationProvider
1、作用
- 调用接口获取用户信息UserDetails
- 验证用户及密码是否可用
2、DaoAuthenticationProvider(默认实现)
- DaoAuthenticationProvider继承AbstractUserDetailsAuthenticationProvider实现AuthenticationProvider接口
- 调用retrieveUser方法获取用户信息UserDetails
- 调用userDetailsService.loadUserByUsername获取用户信息UserDetails
- 验证用户是否存在并可用,不存在或者不可用时抛异常(过期、锁定、启用)
- 验证密码是否可用,不可用时抛异常(为空、过期)
- 使用密码加密器校验密码(界面输入的密码和数据库已加密的密码)
- 密码不一致时抛异常
六、UserDetailsService
1、作用
- 通过用户名username获取用户信息UserDetails
- 返回用户信息UserDetails
2、InMemoryUserDetailsManager(默认实现)
- 项目启动时会默认生成一个用户名密码,存在内存中
- 通过用户名获取该用户并返回
3、推荐实现:自定义UserDetailsService
- 通过用户名从数据库中获取到用户
- 数据库用户转为UserDetails,数据库中未设置的属性像是否启用、账号未过期、密码未过期、账号未锁定直接设置为true即可
package com.yu.demo.service.impl;
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.stereotype.Service;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
//@Autowired
//private UserService userService;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//TODO 通过username从数据库中获取用户,将用户转UserDetails
//User user = userService.getByUsername(username);
//return new User(username, user.getPassword(), user.getEnable(), user.getAccountNonExpired(), user.getCredentialsNonExpired(), user.getAccountNonLocked(), user.getAuthorities());
return new User(username, "123", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
}
}
五、Spring Boot集成Spring Security之认证流程2的更多相关文章
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- Spring Boot 集成 Spring Security 实现权限认证模块
作者:王帅@CodeSheep 写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...
- Spring boot 集成Spring Security
依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- Spring Boot 集成spring security4
项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...
- Spring boot集成spring session实现session共享
最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...
- Spring Boot 集成 Spring Security
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot 集成 Spring Security 入门案例教程
前言 本文作为入门级的DEMO,完全按照官网实例演示: 项目目录结构 Maven 依赖 <parent> <groupId>org.springframework.boot&l ...
- Spring Boot 集成 Spring Security 使用自定义的安全数据源
编写一个类自定义实现 UserDetailsService 接口 @Service("customUserDetailService") public class CustomUs ...
随机推荐
- 怎么在Ubuntu系统云服务器搭建自己的幻兽帕鲁服务器?幻兽帕鲁搭建教程
<幻兽帕鲁>是一款备受瞩目的开放世界生存建造游戏,近期在游戏界非常火爆.玩家可以在游戏世界中收集神奇的生物"帕鲁",并利用它们进行战斗.建造.农耕.工业生产等各种活动. ...
- wandb原来是可以网络直连的,国内可以无障碍使用
一直不是很常使用神经网络训练可视化的工具,包括:tensorboard,等等,wandb平时也是直接就忽略,不过最近无意间看了看这个效果,感觉还是不错的,于是尝试了一下. 网上很多人说这个工具服务器在 ...
- 在docker 容器开启ssh , 并映射22端口到物理载体机上以使外网访问
1. 运行某镜像以启动容器 docker run -it -p 127.0.0.1:5000:22 c7fe6d9267f8 /bin/bash -p 为指定端口, 127.0.0.1 为映射到的物 ...
- Auto.js 入门教程(二)
来了来了 ~ 下面开始学习auto.js 了! 准备材料 : android7.0及以上版本的手机一部(需要开启 '无障碍服务') auto.js软件 vscode (安装配套插件Auto.js-VS ...
- 字符串系列目录&&说明
字符串准备写成一个系列. 目录 \(\text{KMP笔记}\) \(\text{Manacher笔记}\) [] [] [] 格式与说明 下面的说明和格式将被应用于整个系列. 说明 所有字符串的下标 ...
- 一款.NET开发的AI无损放大工具
前言 今天大姚给大家分享一款由.NET开源(GPL-3.0 license).基于腾讯ARC Lab提供的Real-ESRGAN模型开发的AI无损放大工具:AI-Lossless-Zoomer. Re ...
- Zabbix创建模板(templates)及监控项(item)
Zabbix监控--Zabbix创建模板(templates)及监控项(item) 生产环境中,有一个简单的原则,那就是无监控不上线,监控系统开源方案中,zabbix也算不错的选择.由于其系统接口的开 ...
- Kubernetes 初体验
在 DigitalOcean 创建一个 Kubernetes 集群 下载集群 Config 文件到 ~/.kube 目录 通过环境变量 KUBECONFIG 设置本地 kubectl 工具使用下载的配 ...
- uni-app 小程序用户信息之头像昵称填写
小程序获取用户头像昵称,微信又叒做妖,废除之前的接口,改成了头像昵称填写 通知:微信小程序端基础库2.27.1及以上版本,wx.getUserProfile 接口被收回,详见<小程序用户头像昵称 ...
- OCI runtime exec failed: exec failed: container_linux.go:296: starting container process caused "exec: \"bash\": executable file not found in $PATH": unknown
使用如下两个命令均无法进入容器 docker exec -it xxx /bin/bash docker exec -it xxx bash 以为是docker的问题,所以重启 systemctl r ...