SpringSecurity请求流转的本质

news2025/5/11 14:46:38

1. SpringSecurity核心源码分析

分析SpringSecurity的核心原理,那么我们从哪开始分析?以及我们要分析哪些内容?

  1. 系统启动的时候SpringSecurity做了哪些事情?
  2. 第一次请求执行的流程是什么?
  3. SpringSecurity中的认证流程是怎么样的?

1.1 系统启动

当我们的Web服务启动的时候,SpringSecurity做了哪些事情?当系统启动的时候,肯定会加载我们配置的web.xml文件

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>Archetype Created Web Application</display-name>

  <!-- 初始化spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- post乱码过滤器 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 前端控制器 -->
  <servlet>
    <servlet-name>dispatcherServletb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletb</servlet-name>
    <!-- 拦截所有请求jsp除外 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 配置过滤器链 springSecurityFilterChain 名称固定 -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

web.xml中配置的信息:

  1. Spring的初始化(会加载解析SpringSecurity的配置文件)
  2. SpringMVC的前端控制器初始化
  3. 加载DelegatingFilterProxy过滤器

Spring的初始化操作和SpringSecurity有关系的操作是,会加载介绍SpringSecurity的配置文件,将相关的数据添加到Spring容器中

image.png

SpringMVC的初始化和SpringSecurity其实是没有多大关系的

DelegatingFilterProxy过滤器:拦截所有的请求。而且这个过滤器本身是和SpringSecurity没有关系的!!!在之前介绍Shiro的时候,和Spring整合的时候我们也是使用的这个过滤器。 其实就是完成从IoC容器中获取DelegatingFilterProxy这个过滤器配置的 FileterName 的对象。

系统启动的时候会执行DelegatingFilterProxy的init方法

protected void initFilterBean() throws ServletException {
   
    synchronized(this.delegateMonitor) {
   
        // 如果委托对象为null 进入
        if (this.delegate == null) {
   
            // 如果targetBeanName==null
            if (this.targetBeanName == null) {
   
                // targetBeanName = 'springSecurityFilterChain'
                this.targetBeanName = this.getFilterName();
            }
// 获取Spring的容器对象
            WebApplicationContext wac = this.findWebApplicationContext();
            if (wac != null) {
   
                // 初始化代理对象
                this.delegate = this.initDelegate(wac);
            }
        }

    }
}
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
   
    // springSecurityFilterChain
    String targetBeanName = this.getTargetBeanName();
    Assert.state(targetBeanName != null, "No target bean name set");
    // 从IoC容器中获取 springSecurityFilterChain的类型为Filter的对象
    Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);
    if (this.isTargetFilterLifecycle()) {
   
        delegate.init(this.getFilterConfig());
    }

    return delegate;
}

image.png

init方法的作用是:从IoC容器中获取 FilterChainProxy的实例对象,并赋值给 DelegatingFilterProxy的delegate属性

1.2 第一次请求

客户发送请求会经过很多歌Web Filter拦截。

image.png

然后经过系统启动的分析,我们知道有一个我们定义的过滤器会拦截客户端的所有的请求。DelegatingFilterProxy

image.png

当用户请求进来的时候会被doFilter方法拦截

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
   
    Filter delegateToUse = this.delegate;
    if (delegateToUse == null) {
   
        // 如果 delegateToUse 为空 那么完成init中的初始化操作
        synchronized(this.delegateMonitor) {
   
            delegateToUse = this.delegate;
            if (delegateToUse == null) {
   
                WebApplicationContext wac = this.findWebApplicationContext();
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2301512.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【AI工具之Deepseek+Kimi一键免费生成PPT】

1.打开Deepseek网页&#xff1a;DeepSeek 2.使用Deepseek获得一份PPT大纲&#xff08;输入背景需求约束条件进行提问&#xff09;如下图&#xff1a; 3.复制Deepseek输出的PPT大纲 4.打开Kimi网页&#xff1a;Kimi.ai - 会推理解析&#xff0c;能深度思考的AI助手 5.在Kimi中…

基于微信小程序的宿舍报修管理系统设计与实现,SpringBoot(15500字)+Vue+毕业论文+指导搭建视频

运行环境 jdkmysqlIntelliJ IDEAmaven3微信开发者工具 项目技术SpringBoothtmlcssjsjqueryvue2uni-app 宿舍报修小程序是一个集中管理宿舍维修请求的在线平台&#xff0c;为学生、维修人员和管理员提供了一个便捷、高效的交互界面。以下是关于这些功能的简单介绍&#xff1a; …

DeepSeek 助力 Vue 开发:打造丝滑的右键菜单(RightClickMenu)

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…

WLAN无线2.4G/5G频段划分和可用信道

互联网各领域资料分享专区(不定期更新)&#xff1a; Sheet

自己安装一台DeepSeek的服务器

找一台还可以的Linux服务器&#xff0c;登录后执行&#xff1a; curl -fsSL https://ollama.com/install.sh | sh 等待安装完成&#xff1a; 执行命令&#xff0c;根据服务器能力安装不同版本的AI模型&#xff1a; ollama run llama3.2 下一步就开始对话吧&#xff1a; llam…

StableDiffusion学习笔记——4、模型下载和学习

目录   大家好&#xff0c;我是阿赵。   继续学习StableDiffusion的使用。   这次来学习一下模型的相关知识 一、 什么是模型 之前我们了解过&#xff0c;在StableDiffusion里面生成图片或者视频&#xff0c;都需要先选择模型。模型用通俗的话来说&#xff0c;就是决定了…

微软宣布 Windows 11 将不再免费升级:升级需趁早

大家都知道如果你现在是Windows 10 系统&#xff0c;其实可以免费升级到正版 Windows 11&#xff0c;只要你的电脑配置满足 TPM2.0要求。 而最近微软已经公布了 Windows 10 的最后支持时间&#xff0c;也就是今年10月14日&#xff0c;在这之后微软将不再对Windows 10负责&#…

Java开发实习面试笔试题(含答案)

在广州一家中大公司面试&#xff08;BOSS标注是1000-9999人&#xff0c;薪资2-3k&#xff09;&#xff0c;招聘上写着Java开发&#xff0c;基本没有标注前端要求&#xff0c;但是到场知道是前后端分离人不分离。开始先让你做笔试&#xff08;12道问答4道SQL题&#xff09;&…

解析DrugBank数据库数据|Python

一、DrugBank 数据库简介 DrugBank 是一个综合性的生物信息学和化学信息学数据库&#xff0c;专门收录药物和靶点的详细信息。它由加拿大阿尔伯塔大学的 Wishart 研究组 维护&#xff0c;提供化学、药理学、相互作用、代谢、靶点等多方面的药物数据。DrugBank 结合了实验数据和…

专题 - Java Stream API

概述 分类 数据源 任何位置。 如:集合、数组、文件、随机数、 Stream 静态工厂等。 支持的数据类型 整型、长整型、双精度浮点型基本数据类型。引用数据类型。流管道的数据处理流程 流管道必须要有终止操作。否则永不执行,只是一个静默的无操作指令。流管道是懒运算的。当执…

【前端框架】vue2和vue3的区别详细介绍

Vue 3 作为 Vue 2 的迭代版本&#xff0c;在性能、语法、架构设计等多个维度均有显著的变革与优化。以下详细剖析二者的区别&#xff1a; 响应式系统 Vue 2 实现原理&#xff1a;基于 Object.defineProperty() 方法实现响应式。当一个 Vue 实例创建时&#xff0c;Vue 会遍历…

大模型WebUI:Gradio全解11——使用transformers.agents构建Gradio UI(3)

大模型WebUI&#xff1a;Gradio全解11——使用transformers.agents构建Gradio UI&#xff08;3&#xff09; 前言本篇摘要11. 使用transformers.agents构建Gradio UI11.3 创建和使用工具Tools11.3.1 默认工具箱与load_tool11.3.2 创建新工具11.3.3 管理代理的工具箱toolbox11.3…

路由基础 | 路由引入实验 | 不同路由引入方式存在的问题

注&#xff1a;本文为 “路由基础 | 路由表 | 路由引入” 相关文章合辑。 未整理去重。 路由基本概念 1—— 路由表信息、路由进表以及转发流程、最长掩码匹配原则 静下心来敲木鱼已于 2023-11-26 14:06:22 修改 什么是路由 路由就是指导报文转发的路径信息&#xff0c;可以…

网络原理-HTTP/HTTPS

文章目录 HTTPHTTP 是什么&#xff1f;理解“应用层协议”理解 HTTP 协议的⼯作过程HTTP 协议格式抓包⼯具的使用抓包⼯具的原理抓包结果协议格式总结 HTTP 请求&#xff08;Request&#xff09;认识 URLURL 的基本格式关于URL encode 认识“⽅法”&#xff08;method&#xff…

SpringBoot启动失败之application.yml缩进没写好

修改前&#xff1a; spring前面空格了 报错输出&#xff1a;Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the follow…

python爬虫系列课程2:如何下载Xpath Helper

python爬虫系列课程2:如何下载Xpath Helper 一、访问极简插件官网二、点击搜索按钮三、输入xpath并点击搜索四、点击推荐下载五、将下载下来的文件解压缩六、打开扩展程序界面七、将xpath.crx文件拖入扩展程序界面一、访问极简插件官网 极简插件官网地址:https://chrome.zzz…

CentOS建立ssh免密连接(含流程剖析)

一、场景举例(为啥需要免密连接) 1.服务集群间文件复制、通信 2.执行定时触发自动化脚本 3.本地连接远程服务器操作 服务器台数有很多&#xff0c;以上举例都是属于服务器之间的通信&#xff0c;如果每次执行上面操作都要输入账号密码岂不是效率太高了&#xff0c;容易被开…

自由学习记录(36)

Linux Linux 是一个开源的操作系统&#xff0c;其内核及大部分组件都遵循自由软件许可证&#xff08;如 GPL&#xff09;&#xff0c;允许用户查看、修改和分发代码。这种开放性使得开发者和企业可以根据自己的需求定制系统​。 “Linux”严格来说只是指由Linus Torvalds最初开…

【python碎碎笔记】

1.交互模式和编辑器模式 2. 保存文件格式.py &#xff08;表示python文件&#xff09; 3.缩进是python的命&#xff01; 4.内置函数 dir(__builtins__) [ArithmeticError, AssertionError, AttributeError, BaseException, BaseExceptionGroup, BlockingIOError, Broken…

【OS安装与使用】part3-ubuntu安装Nvidia显卡驱动+CUDA 12.4

文章目录 一、待解决问题1.1 问题描述1.2 解决方法 二、方法详述2.1 必要说明2.2 应用步骤2.2.1 更改镜像源2.2.2 安装NVIDIA显卡驱动&#xff1a;nvidia-550&#xff08;1&#xff09;查询显卡ID&#xff08;2&#xff09;PCI ID Repository查询显卡型号&#xff08;3&#xf…