五、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方法完成用户名密码验证

UsernamePasswordAuthenticationFilter

四、认证管理器(AuthenticationManager)

1、作用

  1. 完成Authentication的认证

2、ProviderManager(默认实现)

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

AuthenticationManager

五、AuthenticationProvider

1、作用

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

2、DaoAuthenticationProvider(默认实现)

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

AuthenticationProvider

六、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());
      //{noop}不使用密码加密器,密码123的都可以验证成功
      return new User(username, "{noop}123", true, true, true, true, AuthorityUtils.NO_AUTHORITIES);
   }
}

UserDetailsServiceImpl

posted @ 2024-10-14 17:30  那你为何对我三笑留情  阅读(123)  评论(0编辑  收藏  举报