当前位置: 首页 > news >正文

考试系统 微网站是什么样的dreamweaver网页制作

考试系统 微网站是什么样的,dreamweaver网页制作,工程网站建设,做网站必须要有的素材深入理解HttpSecurity的设计 一、HttpSecurity的应用 在前章节的介绍中我们讲解了基于配置文件的使用方式,也就是如下的使用。 也就是在配置文件中通过 security:http 等标签来定义了认证需要的相关信息,但是在SpringBoot项目中,我们慢慢脱离…

深入理解HttpSecurity的设计

一、HttpSecurity的应用

  在前章节的介绍中我们讲解了基于配置文件的使用方式,也就是如下的使用。

image.png

  也就是在配置文件中通过 security:http 等标签来定义了认证需要的相关信息,但是在SpringBoot项目中,我们慢慢脱离了xml配置文件的方式,在SpringSecurity中提供了HttpSecurity等工具类,这里HttpSecurity就等同于我们在配置文件中定义的http标签。要使用的话方式如下。

image.png

  通过代码结果来看和配置文件的效果是一样的。基于配置文件的方式我们之前分析过,是通过标签对应的handler来解析处理的,那么HttpSecurity这块是如何处理的呢?我们来详细分析下。

二、HttpSecurity的类图结构

image.png

  可以看出HttpSecurity的类图结构相对比较简单,继承了一个父类,实现了两个接口。我们分别来看看他们的作用是什么?

1.SecurityBuilder接口

  我们先来看看SecurityBuilder接口,通过字面含义我们就可以发现这是一个帮我们创建对象的工具类。

public interface SecurityBuilder<O> {/*** Builds the object and returns it or null.* @return the Object to be built or null if the implementation allows it.* @throws Exception if an error occurred when building the Object*/O build() throws Exception;}

  通过源码我们可以看到在SecurityBuilder中给我们提供了一个build()方法。在接口名称处声明了一个泛型,而build()方法返回的正好是这个泛型的对象,其实就很好理解了,也就是SecurityBuilder会创建指定类型的对象。结合HttpSecurity中实现SecurityBuilder接口时指定的泛型我们可以看出创建的具体对象是什么类型。

image.png

  可以看出SecurityBuilder会通过build方法给我们创建一个DefaultSecurityFilterChain对象。也就是拦截请求的那个默认的过滤器链对象。

image.png

然后进入到doBuild()方法,会进入到AbstractConfiguredSecurityBuilder中的方法

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

进入到HttpSecurity中可以查看performBuild()方法的具体实现。

	@Overrideprotected DefaultSecurityFilterChain performBuild() {// 对所有的过滤器做排序this.filters.sort(OrderComparator.INSTANCE);List<Filter> sortedFilters = new ArrayList<>(this.filters.size());for (Filter filter : this.filters) {sortedFilters.add(((OrderedFilter) filter).filter);}// 然后生成 DefaultSecurityFilterChainreturn new DefaultSecurityFilterChain(this.requestMatcher, sortedFilters);}

在构造方法中绑定了对应的请求匹配器和过滤器集合。

image.png

对应的请求匹配器则是 AnyRequestMatcher 匹配所有的请求。当然我们会比较关心默认的过滤器链中的过滤器是哪来的,这块儿我们继续来分析。

2.AbstractConfiguredSecurityBuilder

  然后我们再来看看AbstractConfiguredSecurityBuilder这个抽象类,他其实是SecurityBuilder的实现,在这儿需要搞清楚他们的关系。

image.png

类型作用
SecurityBuilder声明了build方法
AbstractSecurityBuilder提供了获取对象的方法以及控制一个对象只能build一次
AbstractConfiguredSecurityBuilder除了提供对对象细粒度的控制外还扩展了对configurer的操作

然后对应的三个实现类。

image.png

首先 AbstractConfiguredSecurityBuilder 中定义了一个枚举类,将整个构建过程分为 5 种状态,也可
以理解为构建过程生命周期的五个阶段,如下:

private enum BuildState {/*** 还没开始构建*/UNBUILT(0),/*** 构建中*/INITIALIZING(1),/*** 配置中*/CONFIGURING(2),/*** 构建中*/BUILDING(3),/*** 构建完成*/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;}}

通过这些状态来管理需要构建的对象的不同阶段。

2.1 add方法

  AbstractConfiguredSecurityBuilder中方法概览

image.png

  我们先来看看add方法。

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);}}}/*** Gets all the {@link SecurityConfigurer} instances by its class name or an empty* List if not found. Note that object hierarchies are not considered.* @param clazz the {@link SecurityConfigurer} class to look for* @return a list of {@link SecurityConfigurer}s for further customization*/@SuppressWarnings("unchecked")public <C extends SecurityConfigurer<O, B>> List<C> getConfigurers(Class<C> clazz) {List<C> configs = (List<C>) this.configurers.get(clazz);if (configs == null) {return new ArrayList<>();}return new ArrayList<>(configs);}

  add 方法,这相当于是在收集所有的配置类。将所有的 xxxConfigure 收集起来存储到 configurers
中,将来再统一初始化并配置,configurers 本身是一个 LinkedHashMap ,key 是配置类的 class,
value 是一个集合,集合里边放着 xxxConfigure 配置类。当需要对这些配置类进行集中配置的时候,
会通过 getConfigurers 方法获取配置类,这个获取过程就是把 LinkedHashMap 中的 value 拿出来,
放到一个集合中返回。

2.2 doBuild方法

  然后来看看doBuild方法中的代码

 	@Overrideprotected final O doBuild() throws Exception {synchronized (this.configurers) {this.buildState = BuildState.INITIALIZING;beforeInit(); //是一个预留方法,没有任何实现init(); // 就是找到所有的 xxxConfigure,挨个调用其 init 方法进行初始化this.buildState = BuildState.CONFIGURING;beforeConfigure(); // 是一个预留方法,没有任何实现configure(); // 就是找到所有的 xxxConfigure,挨个调用其 configure 方法进行配置。this.buildState = BuildState.BUILDING;O result = performBuild();
// 是真正的过滤器链构建方法,但是在 AbstractConfiguredSecurityBuilder中 performBuild 方法只是一个抽象方法,具体的实现在 HttpSecurity 中this.buildState = BuildState.BUILT;return result;}}

init方法:完成所有相关过滤器的初始化

	private void init() throws Exception {Collection<SecurityConfigurer<O, B>> configurers = getConfigurers();for (SecurityConfigurer<O, B> configurer : configurers) {configurer.init((B) this); // 初始化对应的过滤器}for (SecurityConfigurer<O, B> configurer : this.configurersAddedInInitializing) {configurer.init((B) this);}}

configure方法:完成HttpSecurity和对应的过滤器的绑定。

	private void configure() throws Exception {Collection<SecurityConfigurer<O, B>> configurers = getConfigurers();for (SecurityConfigurer<O, B> configurer : configurers) {configurer.configure((B) this);}}

3.HttpSecurity

  HttpSecurity 做的事情,就是进行各种各样的 xxxConfigurer 配置

image.png

HttpSecurity 中有大量类似的方法,过滤器链中的过滤器就是这样一个一个配置的。我们就不一一介绍
了。每个配置方法的结尾都会来一句 getOrApply,这个是干嘛的?我们来看下

	private <C extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity>> C getOrApply(C configurer)throws Exception {C existingConfig = (C) getConfigurer(configurer.getClass());if (existingConfig != null) {return existingConfig;}return apply(configurer);}

  getConfigurer 方法是在它的父类 AbstractConfiguredSecurityBuilder 中定义的,目的就是去查看当前
这个 xxxConfigurer 是否已经配置过了。
  如果当前 xxxConfigurer 已经配置过了,则直接返回,否则调用 apply 方法,这个 apply 方法最终会调
用到 AbstractConfiguredSecurityBuilder#add 方法,将当前配置 configurer 收集起来
HttpSecurity 中还有一个 addFilter 方法.

	@Overridepublic HttpSecurity addFilter(Filter filter) {Integer order = this.filterOrders.getOrder(filter.getClass());if (order == null) {throw new IllegalArgumentException("The Filter class " + filter.getClass().getName()+ " does not have a registered order and cannot be added without a specified order. Consider using addFilterBefore or addFilterAfter instead.");}this.filters.add(new OrderedFilter(filter, order));return this;}

  这个 addFilter 方法的作用,主要是在各个 xxxConfigurer 进行配置的时候,会调用到这个方法,
(xxxConfigurer 就是用来配置过滤器的),把 Filter 都添加到 fitlers 变量中。

小结:这就是 HttpSecurity 的一个大致工作流程。把握住了这个工作流程,剩下的就只是一些简单的重
复的 xxxConfigurer 配置了

http://www.hkea.cn/news/642825/

相关文章:

  • 网站不去公安局备案自己怎么搭建网站
  • 外贸网站建设入门深圳网络推广哪家
  • 网站模板资源公司网站推广
  • 广东省建设教育协会官方网站首页html简单网页代码
  • 个人网站意义阿里指数官网最新版本
  • 网站开发方式有哪四种搜索引擎优化课程总结
  • 申请做网站、论坛版主app推广接单
  • 青海网站建设广州seo优化推广
  • 物流公司网站制作模板上海网站关键词排名
  • 广西建设人才网搜索引擎优化的目标
  • 比汉斯设计网站素材图片搜索识图入口
  • php网站架设教程英雄联盟韩国
  • 做毕设好的网站百度客服电话24小时
  • 上海手机网站建设电话咨询seo综合查询系统
  • wordpress 4.6 中文版沈阳seo
  • 文件管理软件天津搜索引擎优化
  • 九亭网站建设全国疫情高峰时间表最新
  • 青岛网站建设公司武汉seo收费
  • mvc网站建设的实验报告怎么做优化
  • 有官网建手机网站千锋教育培训多少钱费用
  • b2c交易模式的网站有哪些百度营销客户端
  • flash 学习网站重庆网站seo多少钱
  • 年终总结ppt模板免费下载网站小红书seo排名规则
  • 自己架设网站口碑营销的产品有哪些
  • 湖北省网站备案最快几天天津百度推广排名优化
  • app在线开发制作平台seo网络优化前景怎么样
  • 商务网站的基本情况网站建设工作总结
  • 山西建设厅网站网络销售怎么聊客户
  • 软装素材网站有哪些seo网络排名优化哪家好
  • 邯郸市做网站建设网络口碑营销案例分析