首页 > 其他分享 >Spring Security 认证流程

Spring Security 认证流程

时间:2022-10-31 15:12:15浏览次数:47  
标签:username Spring UsernamePasswordAuthenticationToken 认证 user Provider Security

Spring Security 认证流程

 

 

 本文以用户名/密码验证方式为例,讲解 Spring Security 的认证流程,在此之前,需要你了解 Spring Security 用户名/密码认证的基本配置。

Spring Security 是基于过滤器的,通过一层一层的过滤器,处理认证的流程,拦截非法请求。

 

认证上下文的持久化

处于最前面的过滤器叫做 SecurityContextPersistenceFilter,Spring Security 是通过 Session 来存储认证信息的,这个过滤器的 doFilter 方法在每次请求中只执行一次,作用就是,在请求时,将 Session 中的 SecurityContext 放到当前请求的线程中(如果有),在响应时,检查线程中是否有 SecurityContext,有的话将其放入 Session。可以理解为将 SecurityContext 进行 Session 范围的持久化。

认证信息的封装

接着进入 UsernamePasswordAuthenticationFilter,这是基于用户名/密码认证过程中的主角之一。

默认情况下,这个过滤器会匹配路径为 /login 的 POST 请求,也就是 Spring Security 默认的用户名和密码登录的请求路径。

这里最关键的代码是 attemptAuthentication 方法(由 doFilter 方法调用),源码如下:

@Override
public Authentication attemptAuthentication ( HttpServletRequest request, HttpServletResponse response )
 throws AuthenticationException {
 if ( this.postOnly && !request.getMethod () .equals ( "POST" )) {
 throw new AuthenticationServiceException ( "Authentication method not supported: " + request.getMethod ()) ;
   }
 String username = obtainUsername ( request ) ;
   username = ( username != null ) ? username : "";
   username = username.trim () ;
   String password = obtainPassword ( request ) ;
   password = ( password != null ) ? password : "";
   UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken ( username, password ) ;
   // Allow subclasses to set the "details" property
   setDetails ( request, authRequest ) ;
   return this.getAuthenticationManager () .authenticate ( authRequest ) ;
 }

在 attemptAuthentication 方法代码的第 12 行,使用从 request 中获取到的用户名和密码,构建了一个 UsernamePasswordAuthenticationToken 对象,我们可以看到这个构造方法的代码,非常简单:

public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
    super((Collection)null);
    this.principal = principal;
    this.credentials = credentials;
    this.setAuthenticated(false);
}

只是保存了用户名和密码的引用,并且将认证状态设置为 false,因为此时只是封装了认证信息,还没有进行认证。

我们再回到 attemptAuthentication 的代码,在方法的最后一行,将创建好的认证信息,传递给了一个 AuthenticationManager 进行认证。这里实际工作的是 AuthenticationManager 的实现类 ProviderManager

查找处理认证的 Provider 类

进入 ProviderManager 可以从源码中找到 authenticate 方法,代码比较长,我就不贴在这里了,你可以自行查找,我简述一下代码中的逻辑。

ProviderManager 本身不执行认证操作,它管理着一个 AuthenticationProvider 列表,当需要对一个封装好的认证信息进行认证操作的时候,它会将认证信息和它管理者的 Provider 们,逐一进行匹配,找到合适的 Provider 处理认证的具体工作。

可以这样理解,ProviderManager 是一个管理者,管理着各种各样的 Provider。当有工作要做的时候,它从来都不亲自去做,而是把不同的工作,分配给不同的 Provider 去操作。

最后,它会将 Provider 的工作成果(已认证成功的信息)返回,或者抛出异常。

那么,它是怎么将一个认证信息交给合适的 Provider 的呢?

在上一部分中,我们说到,认证信息被封装成了一个 UsernamePasswordAuthenticationToken,它是Authentication 的子类,ProviderManager 会将这个认证信息的类型,传递个每个 Provider 的 supports 方法,由 Provider 来告诉 ProviderManager 它是不是支持这个类型的认证信息。

认证逻辑

在 Spring Security 内置的 Provider 中,与 UsernamePasswordAuthenticationToken 对应的 Provider 是 DaoAuthenticationProviderauthenticate 方法在它的父类 AbstractUserDetailsAuthenticationProvider 中。我们来看它的 authenticate 方法:
@Override
public 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 ) ;
 }

 

代码比较长,我们说要点:

  1. 代码第 12 行,通过 retrieveUser 方法,获得 UserDetails 信息,这个方法的具体实现,可以在 DaoAuthenticationProvider 中找到,主要是通过 UserDetailsService 的 loadUserByUsername 方法,查找系统中的用户信息。
  2. 代码第 25 行,通过 preAuthenticationChecks.check 方法,进行了认证前的一些校验。校验的具体实现可以在 DefaultPreAuthenticationChecks 内部类中找到,主要是判断用户是否锁定、是否可用、是否过期。
  3. 代码第 26 行,通过 additionalAuthenticationChecks 方法,对用户名和密码进行了校验。具体实现可以在 DaoAuthenticationProvider 中找到。
  4. 代码第 39 行,通过 postAuthenticationChecks.check 方法,校验了密码是否过期。具体实现可以在 DefaultPostAuthenticationChecks 内部类中找到。
  5. 最后,如果以上校验和认证都没有问题,则通过 createSuccessAuthentication 方法,创建成功的认证信息,并返回。此时,就成功通过了认证。

在最后的 createSuccessAuthentication 方法中,会创建一个新的 UsernamePasswordAuthenticationToken 认证信息,这个新的认证信息的认证状态为 true。表示这是一个已经通过的认证。

这个认证信息会返回到 UsernamePasswordAuthenticationFilter 中,并作为 attemptAuthentication 方法的结果。

doFilter 方法中,会根据认证成功或失败的结果,调用相应的 Handler 类进行后续的处理,最后,认证的信息也会被保存在 SecurityContext 中,供后续使用。


链接:https://juejin.cn/post/7054569250307964958
来源:稀土掘金

 

 

 

 

标签:username,Spring,UsernamePasswordAuthenticationToken,认证,user,Provider,Security
From: https://www.cnblogs.com/xingmeng63/p/16844318.html

相关文章

  • Springboot整合mybatis-plus-2增删改查
    文章目录​​一、插入​​​​1.主键id配置自增的​​​​2.时间等自动填充​​​​1.直接在数据字段设置即可​​​​2.使用ioc反射原理​​​​二、改(update)​​​​三、......
  • Springboot 整合 thymeleaf-1
    文章目录​​thymeleaf百叶香​​​​一、添加依赖​​​​1.创建项目的时候引入依赖​​​​2.直接在pom里面添加依赖​​​​二、建立页面html并添加命名空间​​​​三......
  • Spring-13-监听器
    格外加入三个依赖<!--servlet依赖--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0<......
  • Spring-12-事务
    文章目录​​1.使用注解写​​​​1.在ApplicationContext.xml写入代码​​​​2.在实现接口类的公共方法上面写注解​​​​方法一:​​​​方法二:​​​​2.使用在配置中实......
  • Spring-14-Aspect总结-Spring和Mybatis整合
    ......
  • Springboot之Shiro权限管理
    文章目录​​Springboot之Shiro权限管理介绍​​​​1.shiro有三个核心api(接口)​​​​2.如何使用:​​​​第一步导入shiro与springboot整合依赖​​​​第二步配置shiro......
  • springboot之spring-boot-admin显示actuator数据报错: Application run failed org.yam
    1.报错显示:13:43:13.869[restartedMain]DEBUGorg.springframework.boot.diagnostics.FailureAnalyzers-FailureAnalyzerorg.springframework.boot.diagnostics.anal......
  • 狂神说Spring——持续学习中
    1、Spring1.1简介Spring:春天—>给软件行业带来了春天2002年,RodJahnson首次推出了Spring框架雏形interface21框架。2004年3月24日,Spring框架以interface21框架为基......
  • 于SpringBoot打造在线教育系统(4)-- SpringBoot集成ElementUI
    补一个代码,上一节漏掉了,就是访问后台首页的时候,还得需要一个视图接口啊。来,在这:@RequestMapping("/admin/{page}")publicStringadminView(@PathVariable(name="page")......
  • 我只会HelloWorld,但是我却完成了一个SpringBoot项目!(1)
    ##序本系列的写作风格是第一人称,目的是为了让教程看起来更有意思一点,叶小凡是我某本JS书籍的主人公名字。以下经历纯属虚构,如有雷同,纯属巧合!##01兔哥,收我为徒吧我叫叶小凡......