五、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);
    }
}
相关推荐
计算机-秋大田2 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS景区民宿预约系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
精通HelloWorld!10 小时前
使用HttpClient和HttpRequest发送HTTP请求
java·spring boot·网络协议·spring·http
拾忆,想起11 小时前
如何选择Spring AOP的动态代理?JDK与CGLIB的适用场景
spring boot·后端·spring·spring cloud·微服务
customer0813 小时前
【开源免费】基于SpringBoot+Vue.JS美食推荐商城(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
一 乐14 小时前
基于微信小程序的酒店管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·酒店管理系统
小万编程19 小时前
【2025最新计算机毕业设计】基于SpringBoot+Vue家政呵护到家护理服务平台(高质量源码,可定制,提供文档,免费部署到本地)
java·vue.js·spring boot·计算机毕业设计·java毕业设计·web毕业设计
XYu123011 天前
Spring Boot 热部署实现指南
java·ide·spring boot·intellij-idea
是小崔啊1 天前
Spring Boot - 数据库集成07 - 数据库连接池
数据库·spring boot·oracle
细心的莽夫1 天前
SpringBoot 基础(Spring)
spring boot·后端·spring