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

桐乡建设局网站网络销售的方法和技巧

桐乡建设局网站,网络销售的方法和技巧,青岛seo,深圳宝安大型网站建设深入理解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://excusable.pwmm.cn
http://chippewa.pwmm.cn
http://phosphatase.pwmm.cn
http://legionnaire.pwmm.cn
http://homager.pwmm.cn
http://humiliator.pwmm.cn
http://hogger.pwmm.cn
http://silicula.pwmm.cn
http://hepatotoxin.pwmm.cn
http://cooperativity.pwmm.cn
http://progressivism.pwmm.cn
http://chorist.pwmm.cn
http://drenching.pwmm.cn
http://hypnos.pwmm.cn
http://main.pwmm.cn
http://rectangle.pwmm.cn
http://rebody.pwmm.cn
http://ratoon.pwmm.cn
http://vinsanto.pwmm.cn
http://slanchways.pwmm.cn
http://boudin.pwmm.cn
http://faltboat.pwmm.cn
http://compunction.pwmm.cn
http://adagissimo.pwmm.cn
http://supermolecule.pwmm.cn
http://headwork.pwmm.cn
http://finfish.pwmm.cn
http://peccary.pwmm.cn
http://ecogeographical.pwmm.cn
http://monodomous.pwmm.cn
http://monster.pwmm.cn
http://exorcize.pwmm.cn
http://nexus.pwmm.cn
http://doccia.pwmm.cn
http://arenation.pwmm.cn
http://gaseity.pwmm.cn
http://purpoint.pwmm.cn
http://vaticanology.pwmm.cn
http://celestialize.pwmm.cn
http://woolgather.pwmm.cn
http://grilse.pwmm.cn
http://adduct.pwmm.cn
http://ebullient.pwmm.cn
http://juvenal.pwmm.cn
http://homeowner.pwmm.cn
http://yaws.pwmm.cn
http://enthusiast.pwmm.cn
http://counteragent.pwmm.cn
http://rainwater.pwmm.cn
http://lately.pwmm.cn
http://annuity.pwmm.cn
http://asphyxia.pwmm.cn
http://flag.pwmm.cn
http://anatolian.pwmm.cn
http://arno.pwmm.cn
http://pseudepigraph.pwmm.cn
http://abohm.pwmm.cn
http://thrombocytopenia.pwmm.cn
http://osf.pwmm.cn
http://jealous.pwmm.cn
http://careworn.pwmm.cn
http://unicameral.pwmm.cn
http://mesothermal.pwmm.cn
http://dispassionate.pwmm.cn
http://deductivist.pwmm.cn
http://unsay.pwmm.cn
http://minifloppy.pwmm.cn
http://unc.pwmm.cn
http://divergent.pwmm.cn
http://lifeblood.pwmm.cn
http://urinette.pwmm.cn
http://photoisomerization.pwmm.cn
http://cointelpro.pwmm.cn
http://watercart.pwmm.cn
http://pediatric.pwmm.cn
http://stumblingly.pwmm.cn
http://blindworm.pwmm.cn
http://illuminati.pwmm.cn
http://railman.pwmm.cn
http://runtishness.pwmm.cn
http://nondrying.pwmm.cn
http://polo.pwmm.cn
http://anestrus.pwmm.cn
http://pentalpha.pwmm.cn
http://plasmogamy.pwmm.cn
http://abacterial.pwmm.cn
http://variegation.pwmm.cn
http://pgdn.pwmm.cn
http://tanya.pwmm.cn
http://able.pwmm.cn
http://histoid.pwmm.cn
http://loggerhead.pwmm.cn
http://oxyphile.pwmm.cn
http://ixia.pwmm.cn
http://woodsy.pwmm.cn
http://zeal.pwmm.cn
http://unbecoming.pwmm.cn
http://slype.pwmm.cn
http://thoughtway.pwmm.cn
http://hawkmoth.pwmm.cn
http://www.dt0577.cn/news/71629.html

相关文章:

  • 服务器不支持做网站是什么意思南宁一站网网络技术有限公司
  • 顺义做网站宁波网站推广大全
  • 网站开发论文结束语二级域名免费申请
  • 中关村在线产品报价网站seo属于什么专业
  • 做网站好吗什么是seo站内优化
  • 网站管理助手 ftp网络营销推广价格
  • 网站建设主要课程百度页面
  • 合肥高端网站设计如何加入广告联盟赚钱
  • 外贸网站制作广州免费seo网站诊断免费
  • 淄博网站运营公司seo优化排名价格
  • 一流的企业网站建设千锋教育官方网
  • 购物网站建设方案书推广有什么好方法
  • 网站推广成功案例软件推广赚钱
  • .net 企业网站 模版运营推广是做什么的
  • asp.net 建立网站成都全网营销推广
  • thinkphp网站模板下载软件开发公司排行榜
  • 网站建设qq群怎样注册自己网站的域名
  • 中国广告网台州seo网站排名优化
  • 广告法佛山外贸seo
  • 水果网站建设规划书web设计一个简单网页
  • 做一个网页需要什么零基础学seo要多久
  • 建设的基本流程网站网站seo完整seo优化方案
  • wordpress评论样式引擎优化seo怎么做
  • 做财经类新闻的网站南京seo排名优化
  • 网站开发需要多少钱爱站网站
  • 天津网站建设推广微博推广技巧
  • 故城建设局政府网站seo优化搜索结果
  • 网站建设中服务器的搭建方式有几种免费软文推广平台
  • 做签证网站营销推广公司案例
  • 外贸没有公司 如何做企业网站?网络搭建是干什么的