SpringSecurity源码分析3--UserDetail部分

news/2024/10/18 14:21:24/

前言:本章提及的类都是与用户名、密码相关的类

UserDetailsService.class
用于加载用户信息
在这里插入图片描述

DaoAuthenticationProvider.class
将数据库的信息拿出来进行认证

在这里插入图片描述

AbstractUserDetailsAuthenticationProvider.class
DaoAuthenticationProvider的父类,通过模板模式,真正进行认证的模块,

一个允许子类重写和处理UserDetails对象的基AuthenticationProvider。该类旨在响应UsernamePasswordAuthenticationToken身份验证请求。
在这里插入图片描述

AuthenticationProvider.class

在这里插入图片描述

java">@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication,() -> this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.onlySupports","Only UsernamePasswordAuthenticationToken is supported"));String username = determineUsername(authentication);boolean cacheWasUsed = true;UserDetails user = this.userCache.getUserFromCache(username);if (user == null) {cacheWasUsed = false;try {user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);}catch (UsernameNotFoundException ex) {this.logger.debug("Failed to find user '" + username + "'");// 抛出一个含糊的异常,提供安全保护机制if (!this.hideUserNotFoundExceptions) {throw ex;}throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));}Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract");}try {this.preAuthenticationChecks.check(user);additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);}catch (AuthenticationException ex) {if (!cacheWasUsed) {throw ex;}// 可能存在用户数据变更的问题,需要再次尝试获取// There was a problem, so try again after checking// we're using latest data (i.e. not from the cache)cacheWasUsed = false;user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);this.preAuthenticationChecks.check(user);additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);}this.postAuthenticationChecks.check(user);if (!cacheWasUsed) {this.userCache.putUserInCache(user);}Object principalToReturn = user;if (this.forcePrincipalAsString) {principalToReturn = user.getUsername();}return createSuccessAuthentication(principalToReturn, authentication, user);}

获取用户名
在这里插入图片描述

从缓存中获取UserDetail对象

在这里插入图片描述

java">private UserCache userCache = new NullUserCache();

在这里插入图片描述
默认都是null,可以通过setUserCache设置Cache

this.preAuthenticationChecks.check(user);

在这里插入图片描述

additionalAuthenticationChecks

在这里插入图片描述

密码校验
在这里插入图片描述

java">public interface PasswordEncoder {String encode(CharSequence rawPassword);boolean matches(CharSequence rawPassword, String encodedPassword);default boolean upgradeEncoding(String encodedPassword) {return false;}
}

this.postAuthenticationChecks.check(user);

在这里插入图片描述

createSuccessAuthentication

在这里插入图片描述
权限管理

java">	private GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();

在这里插入图片描述retrieveUser

  1. 建议直接抛出UsernameNotFoundException
  2. 时间攻击防护

在这里插入图片描述
通过预先编码这个密码,可以确保在后续的时间攻击防护方法中,不会因为实时编码操作而导致时间差异,这些时间差异可能会被攻击者用来推断密码或其他敏感信息。
在这里插入图片描述
在这里插入图片描述
加密方法升级后,重新更新密码
在这里插入图片描述
在这里插入图片描述

UsernamePasswordAuthenticationFilter.class
根据请求中的username

在这里插入图片描述
在这里插入图片描述

AbstractAuthenticationProcessingFilter.class

在这里插入图片描述

java">private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws IOException, ServletException {if (!requiresAuthentication(request, response)) {chain.doFilter(request, response);return;}try {Authentication authenticationResult = attemptAuthentication(request, response);if (authenticationResult == null) {// return immediately as subclass has indicated that it hasn't completedreturn;}// 给session策略提供钩子this.sessionStrategy.onAuthentication(authenticationResult, request, response);// Authentication successif (this.continueChainBeforeSuccessfulAuthentication) {chain.doFilter(request, response);}successfulAuthentication(request, response, chain, authenticationResult);}catch (InternalAuthenticationServiceException failed) {this.logger.error("An internal error occurred while trying to authenticate the user.", failed);unsuccessfulAuthentication(request, response, failed);}catch (AuthenticationException ex) {// Authentication failedunsuccessfulAuthentication(request, response, ex);}}

在这里插入图片描述
在这里插入图片描述

FormLoginConfigurer.class

在这里插入图片描述
在这里插入图片描述
默认页面,生产不会使用
在这里插入图片描述
默认密码来源
在这里插入图片描述
默认加载的类
在这里插入图片描述

SecurityConfigurerAdapter.class

链式调用
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

AbstractAuthenticationFilterConfigurer.class

在这里插入图片描述

java">@Overridepublic void configure(B http) throws Exception {PortMapper portMapper = http.getSharedObject(PortMapper.class);if (portMapper != null) {this.authenticationEntryPoint.setPortMapper(portMapper);}// 设置请求缓存器RequestCache requestCache = http.getSharedObject(RequestCache.class);if (requestCache != null) {this.defaultSuccessHandler.setRequestCache(requestCache);}// 设置处理器this.authFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));this.authFilter.setAuthenticationSuccessHandler(this.successHandler);this.authFilter.setAuthenticationFailureHandler(this.failureHandler);if (this.authenticationDetailsSource != null) {this.authFilter.setAuthenticationDetailsSource(this.authenticationDetailsSource);}// 设置Session策略SessionAuthenticationStrategy sessionAuthenticationStrategy = http.getSharedObject(SessionAuthenticationStrategy.class);if (sessionAuthenticationStrategy != null) {this.authFilter.setSessionAuthenticationStrategy(sessionAuthenticationStrategy);}// 记住我RememberMeServices rememberMeServices = http.getSharedObject(RememberMeServices.class);if (rememberMeServices != null) {this.authFilter.setRememberMeServices(rememberMeServices);}// 安全上下文SecurityContextConfigurer securityContextConfigurer = http.getConfigurer(SecurityContextConfigurer.class);if (securityContextConfigurer != null && securityContextConfigurer.isRequireExplicitSave()) {// 安全上下文仓库SecurityContextRepository securityContextRepository = securityContextConfigurer.getSecurityContextRepository();this.authFilter.setSecurityContextRepository(securityContextRepository);}// 添加过滤器F filter = postProcess(this.authFilter);http.addFilter(filter);}

在这里插入图片描述


http://www.ppmy.cn/news/1425369.html

相关文章

提高写作效率:ChatGPT写作攻略

ChatGPT无限次数:点击直达 提高写作效率:ChatGPT写作攻略 引言 在当今信息爆炸的时代,写作效率成为许多创作者和写作者关注的重点。随着人工智能技术的不断发展,ChatGPT作为一种自然语言处理模型,为提升写作效率提供了全新的可能…

WPS二次开发专题:WPS SDK事件回调

作者持续关注 WPS二次开发专题系列,持续为大家带来更多有价值的WPS开发技术细节,如果能够帮助到您,请帮忙来个一键三连,更多问题请联系我(QQ:250325397) 目录 1.什么是WPS SDK事件回调: 2.WPS …

有没有手机上使用的库存软件

库存软件是一种仓库的信息管理系统,它主要针对出库与入库这些数据进行管理,传统的库存管理都是在电脑上安装一个专门的数据库管理系统进行管理,这也是一种比较成熟的管理方式,那么有没有手机上使用的库存软件。 手机上使用的库存软…

【论文阅读】用于遥感弱监督语义分割的对比标记和标签激活

【论文阅读】用于遥感弱监督语义分割的对比标记和标签激活 文章目录 【论文阅读】用于遥感弱监督语义分割的对比标记和标签激活一、介绍二、联系工作三、方法3.1 对比token学习模块(CTLM)3.2 Class token对比学习3.3 标签前景激活模块 四、实验结果 Cont…

python 头文件怎么写

本文主要以python2为例。首先介绍一下Python头文件的编程风格,然后再给大家详细介绍import部分的基本用法。这两个部分就是Python中头文件的组成模块。 编程风格 #!/usr/bin/env python #在文件头部 ( 第一行 ) 加上 设置 Python 解释器 # -*- coding: utf…

228 基于matlab的神经网络人脸识别

基于matlab的神经网络人脸识别。 人脸识别以视网膜、 虹膜、 指纹等生物特征的识别作为生物标识符。生物特征识别不很容易伪造、 放错位置。新型脸识别使用的方法 RobustPCA 和径向基函数网络。程序已调通,可直接运行。 228 人脸识别 生物特征识 神经网络 - 小红书 …

联合概率、条件概率、边缘概率、贝叶斯定理

事件 事件是实现的一组结果(一个或多个)。就像"扔硬币时反面是事件",“从一副纸牌中选择国王(国王中的任何一个)也是事件”, “roll到5是事件等” 独立每个事件均不受其他事件影响。例: 抛硬币两次。第一次扔事件的结果不会影响第二个事件结果 相关(也…

websocket 连接,http 协议下用 ws, https 协议下必须要使用 wss

解决方案: https 相当于使用 httpssl 认证,使用 https 时 websocket 访问(比如建立链接时)必须要使用 wss。 详细解释: WebSocket 协议有两个主要版本:“ws”和“wss”。"ws"表示非加密的 Web…