iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Spring Security的登陆流程是什么
  • 886
分享到

Spring Security的登陆流程是什么

2023-06-25 12:06:09 886人浏览 泡泡鱼
摘要

本篇内容介绍了“spring Security的登陆流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在Spring Security

本篇内容介绍了“spring Security的登陆流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在Spring Security中,认证授权都是通过过滤器来实现的。

当开始登陆的时候,有一个关键的过滤器UsernamePassWordAuthenticationFilter,该类继承抽象类AbstractAuthenticationProcessingFilter,在AbstractAuthenticationProcessingFilter里有一个doFilter方法,一切先从这里说起。

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 completed         return;      }      this.sessionStrategy.onAuthentication(authenticationResult, request, response);      // Authentication success      if (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 failed      unsuccessfulAuthentication(request, response, ex);   }}

首先requiresAuthentication先判断是否尝试校验,通过后调用attemptAuthentication方法,这个方法也就是UsernamePasswordAuthenticationFilter 中的attemptAuthentication方法。

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);}

在UsernamePasswordAuthenticationFilter 的attemptAuthentication方法中,先是验证请求的类型,是否是POST请求,如果不是的话,抛出异常。(PS:登陆肯定要用POST方法了)
2.然后拿到username和password。这里使用的是obtainUsername方法,也就是get方法。

@Nullableprotected String obtainPassword(HttpServletRequest request) {   return request.getParameter(this.passwordParameter);}@Nullableprotected String obtainUsername(HttpServletRequest request) {   return request.getParameter(this.usernameParameter);}

由此我们知道了Spring Security中是通过get方法来拿到参数,所以在进行前后端分离的时候是无法接受JSON数据,处理方法就是自定义一个Filter来继承UsernamePasswordAuthenticationFilter,重写attemptAuthentication方法,然后创建一个Filter实例写好登陆成功和失败的逻辑处理,在HttpSecurity参数的configure中通过addFilterAt来替换Spring Security官方提供的过滤器。
3.创建一个UsernamePasswordAuthenticationToken 实例。
4.设置Details,在这里关键的是在WEBAuthenticationDetails类中记录了用户的remoteAddress和sessionId。

public WebAuthenticationDetails(HttpServletRequest request) {   this.remoteAddress = request.getRemoteAddr();   HttpSession session = request.getSession(false);   this.sessionId = (session != null) ? session.getId() : null;}

拿到一个AuthenticationManager通过authenticate方法进行校验,这里以实现类ProviderManager为例。

@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {   //获取Authentication的运行时类   Class<? extends Authentication> toTest = authentication.getClass();   AuthenticationException lastException = null;   AuthenticationException parentException = null;   Authentication result = null;   Authentication parentResult = null;   int currentPosition = 0;   int size = this.providers.size();      for (AuthenticationProvider provider : getProviders()) {       //判断是否支持处理该类别的provider      if (!provider.supports(toTest)) {         continue;      }      if (logger.isTraceEnabled()) {         logger.trace(LogMessage.fORMat("Authenticating request with %s (%d/%d)",               provider.getClass().getSimpleName(), ++currentPosition, size));      }      try {          //获取用户的信息         result = provider.authenticate(authentication);         if (result != null) {            copyDetails(authentication, result);            break;         }      }      catch (AccountStatusException | InternalAuthenticationServiceException ex) {         prepareException(ex, authentication);         // SEC-546: Avoid polling additional providers if auth failure is due to         // invalid account status         throw ex;      }      catch (AuthenticationException ex) {         lastException = ex;      }   }   //不支持的话跳出循环再次执行   if (result == null && this.parent != null) {      // Allow the parent to try.      try {         parentResult = this.parent.authenticate(authentication);         result = parentResult;      }      catch (ProviderNotFoundException ex) {         // ignore as we will throw below if no other exception occurred prior to         // calling parent and the parent         // may throw ProviderNotFound even though a provider in the child already         // handled the request      }      catch (AuthenticationException ex) {         parentException = ex;         lastException = ex;      }   }   if (result != null) {       //擦除用户的凭证 也就是密码      if (this.eraseCredentialsAfterAuthentication && (result instanceof CredentialsContainer)) {         // Authentication is complete. Remove credentials and other secret data         // from authentication         ((CredentialsContainer) result).eraseCredentials();      }      // If the parent AuthenticationManager was attempted and successful then it      // will publish an AuthenticationSuccessEvent      // This check prevents a duplicate AuthenticationSuccessEvent if the parent      // AuthenticationManager already published it      if (parentResult == null) {          //公示登陆成功         this.eventPublisher.publishAuthenticationSuccess(result);      }      return result;   }   // Parent was null, or didn't authenticate (or throw an exception).   if (lastException == null) {      lastException = new ProviderNotFoundException(this.messages.getMessage("ProviderManager.providerNotFound",            new Object[] { toTest.getName() }, "No AuthenticationProvider found for {0}"));   }   // If the parent AuthenticationManager was attempted and failed then it will   // publish an AbstractAuthenticationFailureEvent   // This check prevents a duplicate AbstractAuthenticationFailureEvent if the   // parent AuthenticationManager already published it   if (parentException == null) {      prepareException(lastException, authentication);   }   throw lastException;}

 6.经过一系列校验,此时登陆校验基本完成,当验证通过后会执行doFilter中的successfulAuthentication方法,跳转到我们设置的登陆成功界面,验证失败会执行unsuccessfulAuthentication方法,跳转到我们设置的登陆失败界面。

“Spring Security的登陆流程是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

--结束END--

本文标题: Spring Security的登陆流程是什么

本文链接: https://www.lsjlt.com/news/304842.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring Security的登陆流程是什么
    本篇内容介绍了“Spring Security的登陆流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在Spring Security...
    99+
    2023-06-25
  • Spring Security登陆流程讲解
    在Spring Security中,认证授权都是通过过滤器来实现的。 当开始登陆的时候,有一个关键的过滤器UsernamePasswordAuthenticationFilter,该...
    99+
    2024-04-02
  • Security登录认证的流程是什么
    这篇文章主要介绍“Security登录认证的流程是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Security登录认证的流程是什么”文章能帮助大家解决问题。一、前言:流程图:二、前台发送请求用...
    99+
    2023-06-26
  • Ajax登陆如何使用Spring Security缓存跳转到登陆前的链接
    小编给大家分享一下Ajax登陆如何使用Spring Security缓存跳转到登陆前的链接,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解...
    99+
    2024-04-02
  • Spring Security基本架构与初始化操作流程是什么
    这篇文章主要介绍“Spring Security基本架构与初始化操作流程是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring Security基本架构与初始化操作流程...
    99+
    2023-07-05
  • SpringBoot整合Spring Security过滤器链加载执行流程是什么
    这篇文章主要讲解了“SpringBoot整合Spring Security过滤器链加载执行流程是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot整合Sprin...
    99+
    2023-07-05
  • Spring Security认证的方法是什么
    今天小编给大家分享一下Spring Security认证的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。...
    99+
    2023-06-28
  • Spring Security认证的完整流程记录
    目录前言认证上下文的持久化认证信息的封装查找处理认证的 Provider 类认证逻辑总结前言 本文以用户名/密码验证方式为例,讲解 Spring Security 的认证流程,在此之...
    99+
    2024-04-02
  • Spring Security认证机制是什么
    这篇“Spring Security认证机制是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Spring&nb...
    99+
    2023-07-05
  • Spring MVC的处理流程是什么
    本篇内容介绍了“Spring MVC的处理流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1、曾经的王者&mdash;&am...
    99+
    2023-06-15
  • Spring Security 安全框架的原理是什么
    本篇文章给大家分享的是有关Spring Security 安全框架的原理是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。认证授权分析用户在进行资源访问时,要求系统要对用户进...
    99+
    2023-06-20
  • Spring Security中WebSecurity和HttpSecurity的关系是什么
    这篇文章给大家分享的是有关Spring Security中WebSecurity和HttpSecurity的关系是什么的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。前几天有粉丝私...
    99+
    2024-04-02
  • Spring IOC核心流程是什么
    本篇内容主要讲解“Spring IOC核心流程是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring IOC核心流程是什么”吧! 初始化大致单步跟了下Spring IOC的初始化过程,...
    99+
    2023-06-02
  • Spring Security核心过滤器链是什么
    本篇内容主要讲解“Spring Security核心过滤器链是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Security核心过滤器链是什么”吧!一、Fi...
    99+
    2023-06-21
  • Spring容器启动流程是什么
    本篇内容介绍了“Spring容器启动流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!源码解析考虑到直接看源码是一个非常枯燥无味的过程...
    99+
    2023-06-15
  • Python远程登陆服务器的方法是什么
    这篇文章主要讲解了“Python远程登陆服务器的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python远程登陆服务器的方法是什么”吧!在 shell 环境中...
    99+
    2023-06-02
  • 云主机登录流程是什么
    云主机登录流程通常包括以下步骤:1. 获取云主机的IP地址:首先需要知道云主机的IP地址,可以从云服务商的管理界面或者API中获取。...
    99+
    2023-09-17
    云主机
  • 基于Security实现OIDC单点登录的详细流程
    目录一、说明二、OIDC核心概念三、什么是IDToken3.1.与JWT的AccessToken区别3.2.与UserInfo端点的区别四、OIDC单点登录流程五、SpringSec...
    99+
    2024-04-02
  • 远程登陆SQL Server 2014数据库的方法是什么
    远程登陆SQL Server 2014数据库的方法是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。远程登陆SQLServer (2014)...
    99+
    2024-04-02
  • java接入qq登陆的方法是什么
    要实现Java接入QQ登录,可以使用QQ互联开放平台提供的OAuth2.0方式进行认证和授权。以下是具体的步骤:1. 注册成为QQ互...
    99+
    2023-08-22
    java
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作