二、概要说明

  1. 上文已详细介绍了四、Spring Boot集成Spring Security之认证流程
  2. 本文则着重介绍用户名密码认证过滤器UsernamePasswordAuthenticationFilter的实现原理过程
  3. 认证管理器(authenticationManager)
  4. 认证提供者(AuthenticationProvider)
  5. 自定义配置用户名密码实现(UserDetailsService)

三、UsernamePasswordAuthenticationFilter

1、结构及作用

  1. 继承AbstractAuthenticationProcessingFilter

    1. 初始化请求地址
    2. 初始化authenticationManager
    3. 初始化successHandler
    4. 初始化failureHandler
    5. 实现过滤器入口doFilter方法
    6. doFilter方法调用抽象方法attemptAuthentication,attemptAuthentication供子类实现完成用户名密码验证业务
    7. 认证成功时更新安全上下文,并调用successHandler.onAuthenticationSuccess
    8. 认证失败时删除安全上下文,并调用failureHandler.onAuthenticationFailure
  2. 实现attemptAuthentication方法
    1. 从请求中获取用户名密码
    2. 生成未认证的Authentication
    3. 调用authenticationManager的authenticate方法完成用户名密码验证

四、认证管理器(AuthenticationManager)

1、作用

  1. 完成Authentication的认证

2、ProviderManager(默认实现)

  1. ProviderManager实现AuthenticationManager接口
  2. AuthenticationManager的作用的是完成Authentication的认证
  3. 但是ProviderManager并未直接完成Authentication的认证
  4. 而是提供一个AuthenticationProvider集合
  5. 遍历AuthenticationProvider集合来完成Authentication的认证
  6. 当需要多种认证方式时,可以注册自定义的AuthenticationProvider,后续介绍注册方式

五、AuthenticationProvider

1、作用

  • 调用接口获取用户信息UserDetails
  • 验证用户及密码是否可用

2、DaoAuthenticationProvider(默认实现)

  1. DaoAuthenticationProvider继承AbstractUserDetailsAuthenticationProvider实现AuthenticationProvider接口
  2. 调用retrieveUser方法获取用户信息UserDetails
    1. 调用userDetailsService.loadUserByUsername获取用户信息UserDetails
  3. 验证用户是否存在并可用,不存在或者不可用时抛异常(过期、锁定、启用)
  4. 验证密码是否可用,不可用时抛异常(为空、过期)
  5. 使用密码加密器校验密码(界面输入的密码和数据库已加密的密码)
  6. 密码不一致时抛异常

六、UserDetailsService

1、作用

  • 通过用户名username获取用户信息UserDetails
  • 返回用户信息UserDetails

2、InMemoryUserDetailsManager(默认实现)

  1. 项目启动时会默认生成一个用户名密码,存在内存中
  2. 通过用户名获取该用户并返回

3、推荐实现:自定义UserDetailsService

  1. 通过用户名从数据库中获取到用户
  2. 数据库用户转为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的更多相关文章

  1. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  2. SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  3. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  4. Spring Boot 集成 Spring Security 实现权限认证模块

    作者:王帅@CodeSheep   写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...

  5. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  6. Spring Boot 集成spring security4

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  7. Spring boot集成spring session实现session共享

    最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...

  8. Spring Boot 集成 Spring Security

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. Spring Boot 集成 Spring Security 入门案例教程

    前言 本文作为入门级的DEMO,完全按照官网实例演示: 项目目录结构 Maven 依赖 <parent> <groupId>org.springframework.boot&l ...

  10. Spring Boot 集成 Spring Security 使用自定义的安全数据源

    编写一个类自定义实现 UserDetailsService 接口 @Service("customUserDetailService") public class CustomUs ...

随机推荐

  1. 【C】Re03

    一.变量 变量存储了两个东西: 1.内存空间地址 2.内存空间存放的值 本质是给内存地址起了一个别名,允许我们通过别名对内存进行访问 void variable01() { int a = 100; ...

  2. 【Windows】XP系统安装TIM/QQ 数字签名过期问题

    需要手动安装数字签名 右键安装包 -> 属性 但是我的TIM没有用,对QQ是有效的 参考自视频: https://www.bilibili.com/video/av413122971/

  3. 人形机器人(humanoid)(双足机器人、四足机器人) —— 硬件测试的方法

    硬件测试的方法: 硬件的稳定性.鲁棒性.为机器人设定好固有的执行策略,然后长时间的让机器人重复执行这些既定好的动作.该种测试方法主要测试硬件的设计是否合理,硬件在长时间的运行中是否可以稳定运行而不是出 ...

  4. OneFlow是否真的实现了单机代码无侵害的运行在分布式集群上?

    答案: 不是,但也是. 严格意义上来说,不是. 因为技术OneFlow的代码,要从单机改到分布式,也需要改配置,需要给所有的变量设置具体的全局存储还是局部存储,如果局部存储又应该如何划分,等等,这些其 ...

  5. 如何使用Python环境下的2D经典游戏仿真器(openai推出的)retro库运行游戏"刺猬索尼克" (SonicTheHedgehog-Genesis)

    很多资料上都有使用游戏仿真器(openai推出的)retro库运行游戏"刺猬索尼克"  (SonicTheHedgehog-Genesis),但是均没有给出详细的安装该款游戏的步骤 ...

  6. vscode配置远程项目开发

    0. vscode中安装remote development插件 (略) 1. 客户端进行配置 按键:ctrl+shift+p 在输入框中输入: Preferences: Open User Sett ...

  7. Windows 修改本地hosts文件

    在在使用win下面的一些php集成开发工具的时候(比如 phpstudy wampserver等) 有时候会有这样的需求:我不想通过localhost/xxx/xxx/xxx.php 这样的方式访问我 ...

  8. 【主席树】P3834 【模板】可持久化线段树 2

    P3834 [模板]可持久化线段树 2 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include <bits/stdc++.h> using namespace ...

  9. 第 111 场双周赛 - 力扣(LeetCode)

    第 111 场双周赛 - 力扣(LeetCode) 2824. 统计和小于目标的下标对数目 - 力扣(LeetCode) 枚举即可 class Solution { public: int count ...

  10. 手把手教你ubuntu下移植MJPG-streamer

    一.嵌入式视频图像开源库 在嵌入式系统中,常用的视频图像处理开源系统有:luvcview.cheese.motion.mjpg-streamer或者ffmpeg,其中: • luvcview: 基于V ...