Spring Security源码

在这里插入图片描述
在这里插入图片描述
WebSecurityConfigurerAdapter已废弃,官方推荐使用HttpSecurity 或WebSecurity。
在这里插入图片描述
在这里插入图片描述
都继承了SecurityBuilder

public interface SecurityBuilder<O> {O build() throws Exception;}

亮点:通过这种方式很容易知道知道自己构建的Object

HttpSecurity

public interface SecurityFilterChain {/**** 判断请求是否能够匹配上这些过滤器**/boolean matches(HttpServletRequest request);/**** 过滤器列表**/List<Filter> getFilters();
}
public abstract class AbstractSecurityBuilder<O> implements SecurityBuilder<O> {private AtomicBoolean building = new AtomicBoolean();private O object;/*** 防止重复构建**/@Overridepublic final O build() throws Exception {if (this.building.compareAndSet(false, true)) {this.object = doBuild();return this.object;}throw new AlreadyBuiltException("This object has already been built");}/*** Gets the object that was built. If it has not been built yet an Exception is* thrown.* @return the Object that was built*/public final O getObject() {if (!this.building.get()) {throw new IllegalStateException("This object has not been built");}return this.object;}/*** 给子类自己实现*/protected abstract O doBuild() throws Exception;}
	@Overrideprotected final O doBuild() throws Exception {synchronized (this.configurers) {this.buildState = BuildState.INITIALIZING;beforeInit();init();this.buildState = BuildState.CONFIGURING;beforeConfigure();configure();this.buildState = BuildState.BUILDING;O result = performBuild();this.buildState = BuildState.BUILT;return result;}}protected void beforeInit() throws Exception {}protected void beforeConfigure() throws Exception {}/*** 重要方法**/protected abstract O performBuild() throws Exception;

在这里插入图片描述

	@SuppressWarnings("unchecked")@Overrideprotected DefaultSecurityFilterChain performBuild() {......return new DefaultSecurityFilterChain(this.requestMatcher, sortedFilters);}
	@Overrideprotected Filter performBuild() throws Exception {......FilterChainProxy filterChainProxy = new FilterChainProxy(securityFilterChains);......Filter result = filterChainProxy;if (this.debugEnabled) {.....result = new DebugFilter(filterChainProxy);}return result;}

可以通过@EnableWebSecurity(debug = true)开启debug模式
new DebugFilter(filterChainProxy)对filterChainProxy进行装饰

	public FilterChainProxy(List<SecurityFilterChain> filterChains) {this.filterChains = filterChains;}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {......doFilterInternal(request, response, chain);......}private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {FirewalledRequest firewallRequest = this.firewall.getFirewalledRequest((HttpServletRequest) request);List<Filter> filters = getFilters(firewallRequest);......// 虚拟过滤器,挨个应用filtersVirtualFilterChain virtualFilterChain = new VirtualFilterChain(firewallRequest, chain, filters);virtualFilterChain.doFilter(firewallRequest, firewallResponse);}// 获取匹配的filterprivate List<Filter> getFilters(HttpServletRequest request) {int count = 0;for (SecurityFilterChain chain : this.filterChains) {......if (chain.matches(request)) {return chain.getFilters();}}return null;}
@Overridepublic void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {if (this.currentPosition == this.size) {if (logger.isDebugEnabled()) {logger.debug(LogMessage.of(() -> "Secured " + requestLine(this.firewalledRequest)));}// Deactivate path stripping as we exit the security filter chainthis.firewalledRequest.reset();this.originalChain.doFilter(request, response);return;}this.currentPosition++;Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1);if (logger.isTraceEnabled()) {logger.trace(LogMessage.format("Invoking %s (%d/%d)", nextFilter.getClass().getSimpleName(),this.currentPosition, this.size));}nextFilter.doFilter(request, response, this);}
AbstractConfiguredSecurityBuilder.classprivate final LinkedHashMap<Class<? extends SecurityConfigurer<O, B>>, List<SecurityConfigurer<O, B>>> configurers = new LinkedHashMap<>();private final List<SecurityConfigurer<O, B>> configurersAddedInInitializing = new ArrayList<>();private final Map<Class<?>, Object> sharedObjects = new HashMap<>();private final boolean allowConfigurersOfSameType;// 构建状态private BuildState buildState = BuildState.UNBUILT;// 后置处理private ObjectPostProcessor<Object> objectPostProcessor;private <C extends SecurityConfigurer<O, B>> void add(C configurer) {Assert.notNull(configurer, "configurer cannot be null");Class<? extends SecurityConfigurer<O, B>> clazz = (Class<? extends SecurityConfigurer<O, B>>) configurer.getClass();synchronized (this.configurers) {if (this.buildState.isConfigured()) {throw new IllegalStateException("Cannot apply " + configurer + " to already built object");}List<SecurityConfigurer<O, B>> configs = null;if (this.allowConfigurersOfSameType) {configs = this.configurers.get(clazz);}configs = (configs != null) ? configs : new ArrayList<>(1);configs.add(configurer);this.configurers.put(clazz, configs);if (this.buildState.isInitializing()) {this.configurersAddedInInitializing.add(configurer);}}}

/*** 由于配置一个SecurityBuilder* Allows for configuring a {@link SecurityBuilder}. All {@link SecurityConfigurer} first* have their {@link #init(SecurityBuilder)} method invoked. After all* {@link #init(SecurityBuilder)} methods have been invoked, each* {@link #configure(SecurityBuilder)} method is invoked.** @param <O> The object being built by the {@link SecurityBuilder} B* @param <B> The {@link SecurityBuilder} that builds objects of type O. This is also the* {@link SecurityBuilder} that is being configured.* @author Rob Winch* @see AbstractConfiguredSecurityBuilder*/
public interface SecurityConfigurer<O, B extends SecurityBuilder<O>> {/*** 设置一些共享状态,而不是属性* Initialize the {@link SecurityBuilder}. Here only shared state should be created* and modified, but not properties on the {@link SecurityBuilder} used for building* the object. This ensures that the {@link #configure(SecurityBuilder)} method uses* the correct shared objects when building. Configurers should be applied here.* @param builder* @throws Exception*/void init(B builder) throws Exception;/*** Configure the {@link SecurityBuilder} by setting the necessary properties on the* {@link SecurityBuilder}.* @param builder* @throws Exception*/void configure(B builder) throws Exception;}
private enum BuildState {/*** This is the state before the {@link Builder#build()} is invoked*/UNBUILT(0),/*** The state from when {@link Builder#build()} is first invoked until all the* {@link SecurityConfigurer#init(SecurityBuilder)} methods have been invoked.*/INITIALIZING(1),/*** The state from after all {@link SecurityConfigurer#init(SecurityBuilder)} have* been invoked until after all the* {@link SecurityConfigurer#configure(SecurityBuilder)} methods have been* invoked.*/CONFIGURING(2),/*** From the point after all the* {@link SecurityConfigurer#configure(SecurityBuilder)} have completed to just* after {@link AbstractConfiguredSecurityBuilder#performBuild()}.*/BUILDING(3),/*** After the object has been completely built.*/BUILT(4);private final int order;BuildState(int order) {this.order = order;}public boolean isInitializing() {return INITIALIZING.order == this.order;}/*** Determines if the state is CONFIGURING or later* @return*/public boolean isConfigured() {return this.order >= CONFIGURING.order;}}

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

手机也能写前段代码,推荐一款万能编程软件

Python是一种强大的编程语言&#xff0c;广泛应用于各个领域&#xff0c;包括移动应用开发。如果你想在手机上进行Python编程&#xff0c;那么选择合适的软件工具就显得尤为重要。 一.python Pydroid 3 Pydroid 3是一款专为Android设备打造的Python IDE。它提供了一个完整的开…

RTC的Google拥塞控制算法 rmcat-gcc-02

摘要 本文档描述了使用时的两种拥塞控制方法万维网&#xff08;RTCWEB&#xff09;上的实时通信&#xff1b;一种算法是基于延迟策略&#xff0c;一种算法是基于丢包策略。 1.简介 拥塞控制是所有共享网络的应用程序的要求互联网资源 [RFC2914]。 实时媒体的拥塞控制对于许…

Linux系统安全②SNAT与DNAT

目录 一.SNAT 1.定义 2.实验环境准备 &#xff08;1&#xff09;三台服务器&#xff1a;PC1客户端、PC2网关、PC3服务端。 &#xff08;2&#xff09;硬件要求&#xff1a;PC1和PC3均只需一块网卡、PC2需要2块网卡 &#xff08;3&#xff09;网络模式要求&#xff1a;PC1…

Git——本地使用详解

目录 Git1、开始版本控制1.1、初始化Repository1.2、使目录脱离Git控制 2、把文件交给Git管控2.1、创建文件后交给Git2.2、git add之后再次修改文件2.3、git add "--all"与"."参数区别2.4、把暂存区的内容提交到存储库里存档 3、工作区、暂存区与存储库3.1…

idea找不到或无法加载主类

前言 今天在运行项目的时候突然出了这样一个错误&#xff1a;IDEA 错误 找不到或无法加载主类,相信只要是用过IDEA的朋友都 遇到过它吧&#xff0c;但是每次遇到都是一顿焦头烂额、抓耳挠腮、急赤白咧&#xff01;咋整呢&#xff1f;听我给你吹~ 瞧我这张嘴~ 问题报错 找不…

Kafka MQ 生产者

Kafka MQ 生产者 生产者概览 尽管生产者 API 使用起来很简单&#xff0c;但消息的发送过程还是有点复杂的。图 3-1 展示了向 Kafka 发送消息的主要步骤。 我们从创建一个 ProducerRecord 对象开始&#xff0c;ProducerRecord 对象需要包含目标主题和要发送的内容。我们还可以…

【机器学习300问】35、什么是随机森林?

〇、让我们准备一些训练数据 idx0x1x2x3x4y04.34.94.14.75.5013.96.15.95.55.9022.74.84.15.05.6036.64.44.53.95.9146.52.94.74.66.1152.76.74.25.34.81 表格中的x0到x4一共有5个特征&#xff0c;y是目标值只有0,1两个值说明是一个二分类问题。 关于决策树相关的前置知识&am…

为什么HashMap要使用红黑树?

1、典型回答 HashMap 中之所以使用红黑树&#xff0c;是因为红黑树最合适做 HashMap 多节点的数据存储和查询。因为使用二又搜索树在某些情况下会退化为链表&#xff0c;所以它的查询效率可能会存在问题&#xff0c;而使用 AVL 树&#xff0c;在添加或删除时&#xff0c;效率又…

程序员如何利用AI写代码

程序员可以通过多种方式利用AI来辅助编写代码&#xff0c;以下是一些常见的应用场景&#xff1a; 代码补全和语法检查&#xff1a;许多IDE&#xff08;集成开发环境&#xff09;和代码编辑器都内置了AI驱动的代码补全和语法检查功能。这些功能可以根据你正在编写的代码上下文&…

SkyTower1靶场练习小白向靶场安装sql语句被过滤万能SQL密码

下载链接&#xff1a; SkyTower: 1 ~ VulnHub 安装&#xff1a; 下载解压后打开vxbox&#xff0c;新建虚拟机&#xff0c;系统选择linux&#xff0c;使用已有虚拟硬盘文件 选择解压后的vdi文件 后直接创建 设置中的网络链接模式根据自己情况定 我这里选择的是桥接模式 完成后直…

Ubuntu 虚拟机安装

最小化安装后常用工具 sudo apt-get install vim# ifconfig apt install net-tools # nload apt install nload # 很多都要用到 apt install build-essential # 开发相关 apt install gcc gapt install iproute2 ntpdate tcpdump telnet traceroute \ nfs-kernel-server nfs…

kafka集群介绍

介绍 kafka是一个高性能、低延迟、分布式的消息传递系统&#xff0c;特点在于实时处理数据。集群由多个成员节点broker组成&#xff0c;每个节点都可以独立处理消息传递和存储任务。 路由策略 发布消息由key、value组成&#xff0c;真正的消息是value&#xff0c;key是标识路…