五、Spring Boot集成Spring Security之认证流程2

二、概要说明

  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即可
java 复制代码
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);
    }
}
相关推荐
程序员张32 小时前
SpringBoot计时一次请求耗时
java·spring boot·后端
麦兜*8 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了9 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
大只鹅9 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
天河归来9 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
IT_10249 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
武昌库里写JAVA10 小时前
Oracle如何使用序列 Oracle序列使用教程
java·开发语言·spring boot·学习·课程设计
超级小忍11 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端
程序员张311 小时前
SQL分析与打印-p6spy组件
spring boot·sql·mybatis·mybatisplus·p6spy
CHENWENFEIc12 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试