五、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 ...
随机推荐
- 【SpringBoot】15 数据访问P3 整合Mybatis
重新建立一个SpringBoot工程 选择依赖组件 然后删除不需要的Maven&Git文件 还是先查看我们的POM文件 整合Mybatis的组件多了这一个,默认的版本是3.5.4 然后再看看整 ...
- 很好用的python游戏环境:强化学习算法走迷宫游戏环境(导航问题 navigation):分享一个python语言的迷宫游戏环境
项目的GitHub地址(作者:莫凡): https://github.com/MorvanZhou/mmaze 运行的示例代码: import mmaze start = (0, 0) end = ( ...
- NVIDIA H100 GPU:GPU的机密计算
国内总结的资料: https://zhuanlan.zhihu.com/p/644717121 相关论文: https://ieeexplore.ieee.org/document/7163017 博 ...
- openAI的仿真环境Gym Retro的Python API接口(续1)—— 游戏过程记录及回放
如题,本文主要介绍仿真环境Gym Retro的Python API接口 . 官网地址: https://retro.readthedocs.io/en/latest/python.html 本文环境配 ...
- 在使用pytorch官方给出的torchvision中的预训练模型参数时为保证收敛性要求使用原始的数据预处理方式
本文主要内容如题: 在使用pytorch官方给出的torchvision中的预训练模型参数时为保证收敛性要求使用原始的数据预处理方式 具体的pytorch官方讨论: https://github.co ...
- 7月30日CSP-S模拟赛赛后总结
7月30日模拟赛赛后总结 \[7月30日 \ \ 模拟赛 \ \ 赛后总结 \\ 2024年7月30日 \\ by \ \ \ hcy \] 洛谷同步:点我 一.做题情况 第一题比赛 \(100pts ...
- Apache SeaTunnel 4月回顾:明星贡献者与技术突破
各位热爱 SeaTunnel 的小伙伴们,SeaTunnel 社区 4 月份月报来啦!这里将记录 SeaTunnel 社区每月的重要更新,欢迎关注! 月度 Merge 之星 感谢以下小伙伴 4 月为 ...
- 聚焦OLAP性能提升,火山引擎ByteHouse发布六大场景解决方案
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群. 性能在数据分析中至关重要,它直接决定数据处理的效率与及时性,进一步对数据驱动的企业决策造成影响. 举个例 ...
- .NET 8 + Blazor 多租户、模块化、DDD框架、开箱即用
前言 基于 .NET 8 的开源项目,主要使用 WebAPI + Blazor 支持多租户和模块化设计,DDD构建.可以帮助我们轻松地搭建起一个功能完善的Web应用程序.除了帮助你快速构建应用程序之外 ...
- SMU Autumn 2023 Round 4(Div.1+2)
SMU Autumn 2023 Round 4(Div.1+2) A. Access Denied 通过分析样例可以得知如果所猜字符串与答案字符串长度不同,则只要\(5ms\),且答案最多\(20\) ...