springboot情操陶冶-web配置(九)
承接前文springboot情操陶冶-web配置(八),本文在前文的基础上深入了解下WebSecurity类的运作逻辑
WebSecurityConfigurerAdapter
在剖析WebSecurity的工作逻辑之前,先预热下springboot security推荐复写的抽象类WebSecurityConfigurerAdapter,观察下其是如何被实例化的,方便后续的深入理解
1.ApplicationContext环境设置
@Autowired
public void setApplicationContext(ApplicationContext context) {
this.context = context;
ObjectPostProcessor<Object> objectPostProcessor = context.getBean(ObjectPostProcessor.class);
LazyPasswordEncoder passwordEncoder = new LazyPasswordEncoder(context);
//
authenticationBuilder = new DefaultPasswordEncoderAuthenticationManagerBuilder(objectPostProcessor, passwordEncoder);
localConfigureAuthenticationBldr = new DefaultPasswordEncoderAuthenticationManagerBuilder(objectPostProcessor, passwordEncoder) {
@Override
public AuthenticationManagerBuilder eraseCredentials(boolean eraseCredentials) {
authenticationBuilder.eraseCredentials(eraseCredentials);
return super.eraseCredentials(eraseCredentials);
}
};
}
同前文的认证管理器创建差不多,但这里细心的可以看到其内部创建了两个一模一样的认证管理器对象,目的应该是为了引用AuthenticationConfiguration对象中的认证管理器作准备,下文会提及
2.引入前文所提的AuthenticationConfiguration对象
@Autowired
public void setAuthenticationConfiguration(
AuthenticationConfiguration authenticationConfiguration) {
this.authenticationConfiguration = authenticationConfiguration;
}
目的是间接调用此类获取对应的认证管理器AuthenticationManager对象,具体的读者可自行阅读
3.共享变量集合,security板块引入了共享变量,目的就跟缓存一样
private Map<Class<? extends Object>, Object> createSharedObjects() {
Map<Class<? extends Object>, Object> sharedObjects = new HashMap<Class<? extends Object>, Object>();
sharedObjects.putAll(localConfigureAuthenticationBldr.getSharedObjects());
sharedObjects.put(UserDetailsService.class, userDetailsService());
sharedObjects.put(ApplicationContext.class, context);
sharedObjects.put(ContentNegotiationStrategy.class, contentNegotiationStrategy);
sharedObjects.put(AuthenticationTrustResolver.class, trustResolver);
return sharedObjects;
}
OK,差不多就这样,开始我们的主角之旅把
WebSecurityConfiguration
根据前文可知,WebSecurity对象的创建与运作都是通过WebSecurityConfiguration来的,笔者在此处再作下简单的归纳
1.实例化WebSecurity对象并注册至ApplicationContext上下文对象中
2.获取ApplicationContext对象上注册的类型为WebSecurityConfigurer的接口集合,存放至WebSecurity的父类AbstractConfiguredSecurityBuilder的内部属性configurers(hashmap)中
3.运行WebSecurity的build()方法创建Filter过滤链
基于上述的结论我们再着重看下第三点的创建过滤链是如何完成的,本文侧重点也在于此
WebSecurity#build()
build()方法是由其父类AbstractSecurityBuilder来完成的,代码很简单
public final O build() throws Exception {
// 确保只会被build一次
if (this.building.compareAndSet(false, true)) {
this.object = doBuild();
return this.object;
}
throw new AlreadyBuiltException("This object has already been built");
}
接着跟踪doBuild()模板方法,发现是由其子类抽象类AbstractConfiguredSecurityBuilder来操作的
@Override
protected final O doBuild() throws Exception {
synchronized (configurers) {
buildState = BuildState.INITIALIZING;
// init
beforeInit();
init();
buildState = BuildState.CONFIGURING;
// configure
beforeConfigure();
configure();
buildState = BuildState.BUILDING;
// build
O result = performBuild();
buildState = BuildState.BUILT;
return result;
}
}
大致可以分为三步走,下面笔者就按照上述的三步一一分开剖析
初始化阶段
分为beforeInit()和init()两个操作
beforeInit()
beforeInit()初始化前的操作,默认是空的,可供用户去复写
init()
init()初始化方法比较有意思了,看下其源码
private void init() throws Exception {
// WebSecurity内的configurer是WebSecurityConfigurer类型的
Collection<SecurityConfigurer<O, B>> configurers = getConfigurers();
// 遍历调用公用的init()方法,关注此处的this指代WebSecurity对象
for (SecurityConfigurer<O, B> configurer : configurers) {
configurer.init((B) this);
}
// 候补
for (SecurityConfigurer<O, B> configurer : configurersAddedInInitializing) {
configurer.init((B) this);
}
}
好,笔者来看下configurer#init()方法,此处只针对WebSecurityConfigurer接口的初始化。即使用户没有去实现该接口,springboot也会默认有个类去实现
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnMissingBean(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
public class SpringBootWebSecurityConfiguration {
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER)
static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {
}
}
很明显,就是继承WebSecurityConfigurerAdapter类来实现的,那我们看下其init()方法的操作
public void init(final WebSecurity web) throws Exception {
// important
final HttpSecurity http = getHttp();
// configure interceptor?
web.addSecurityFilterChainBuilder(http).postBuildAction(new Runnable() {
public void run() {
FilterSecurityInterceptor securityInterceptor = http
.getSharedObject(FilterSecurityInterceptor.class);
web.securityInterceptor(securityInterceptor);
}
});
}
上述的代码比较关键,笔者按两个小步骤讲述一下
1.创建HttpSecurity对象,贴出源码仔细分析下
protected final HttpSecurity getHttp() throws Exception {
if (http != null) {
return http;
}
// 配置事件发行器
DefaultAuthenticationEventPublisher eventPublisher = objectPostProcessor
.postProcess(new DefaultAuthenticationEventPublisher());
localConfigureAuthenticationBldr.authenticationEventPublisher(eventPublisher);
// 获取父级认证管理器,涉及configure(AuthenticationManagerBuilder auth)方法复写
AuthenticationManager authenticationManager = authenticationManager();
authenticationBuilder.parentAuthenticationManager(authenticationManager);
Map<Class<? extends Object>, Object> sharedObjects = createSharedObjects();
// 创建HttpSecurity对象
http = new HttpSecurity(objectPostProcessor, authenticationBuilder,
sharedObjects);
// 是否屏蔽默认配置,默认为false
if (!disableDefaults) {
// @formatter:off
http
.csrf().and()
.addFilter(new WebAsyncManagerIntegrationFilter())
.exceptionHandling().and()
.headers().and()
.sessionManagement().and()
.securityContext().and()
.requestCache().and()
.anonymous().and()
.servletApi().and()
.apply(new DefaultLoginPageConfigurer<>()).and()
.logout();
// @formatter:on
ClassLoader classLoader = this.context.getClassLoader();
// 加载spring.factories中以AbstractHttpConfigurer作为Key的类型集合
List<AbstractHttpConfigurer> defaultHttpConfigurers =
SpringFactoriesLoader.loadFactories(AbstractHttpConfigurer.class, classLoader);
for(AbstractHttpConfigurer configurer : defaultHttpConfigurers) {
http.apply(configurer);
}
}
// 供用户自定义配置HTTP安全配置,默认支持表单和Basic方式提交认证信息
configure(http);
return http;
}
代码信息过多,不一一分析了,springboot推荐用户复写以下两个方法
- configure(AuthenticationManagerBuilder auth) 配置认证管理器,用户信息读取方式、加密方式均可通过此方法配置
- configure(HttpSecurity http) 配置http服务,路径拦截、csrf保护等等均可通过此方法配置
2.将HttpSecurity放入WebSecurity中,细看发现HttpSecurity是用来创建FilterChain过滤链的。并尝试塞入一个FilterSecurityInterceptor拦截器,默认一般都是会配置的。
配置阶段
分为beforeConfigure()和configure()阶段
beforeConfigure()
配置前的操作,供用户去复写
configure()
遍历其下的所有的WebSecurityConfigurerAdapter的实现类,统一调用configure(WebSecurity web)配置。很明显,用户也可以复写方法来配置,比如对HttpSecurity默认的配置不满意,也可以通过此类来复写之;屏蔽一些URL的访问等等。
创建阶段
真正的创建Filter对象是由performBuild()方法执行的,别看源码很长,其实就一个意思,通过HttpSecurity对象来创建Filter过滤链
@Override
protected Filter performBuild() throws Exception {
Assert.state(
!securityFilterChainBuilders.isEmpty(),
"At least one SecurityBuilder<? extends SecurityFilterChain> needs to be specified. Typically this done by adding a @Configuration that extends WebSecurityConfigurerAdapter. More advanced users can invoke "
+ WebSecurity.class.getSimpleName()
+ ".addSecurityFilterChainBuilder directly");
int chainSize = ignoredRequests.size() + securityFilterChainBuilders.size();
List<SecurityFilterChain> securityFilterChains = new ArrayList<>(
chainSize);
// 用户可通过调用websecurity.ignoring()方法来屏蔽一些访问路径
for (RequestMatcher ignoredRequest : ignoredRequests) {
securityFilterChains.add(new DefaultSecurityFilterChain(ignoredRequest));
}
for (SecurityBuilder<? extends SecurityFilterChain> securityFilterChainBuilder : securityFilterChainBuilders) {
// HttpSecurity#build()会被执行,执行逻辑就跟本文的WebSecurity一模一样
securityFilterChains.add(securityFilterChainBuilder.build());
}
FilterChainProxy filterChainProxy = new FilterChainProxy(securityFilterChains);
// 防火墙配置
if (httpFirewall != null) {
filterChainProxy.setFirewall(httpFirewall);
}
filterChainProxy.afterPropertiesSet();
Filter result = filterChainProxy;
postBuildAction.run();
return result;
}
具体的用户可自行去阅读
小结
本文主要讲述了WebSecurity与HttpSecurity之间的关系以及如何被创建,其实都是一样的,它们都是AbstractConfiguredSecurityBuilder的复写类,核心都是会执行其中的build()模板方法来创建过滤链。笔者或者读者只需要复写其中的几个重要方法便可实现简单的安全配置。希望本文对大家有用
springboot情操陶冶-web配置(九)的更多相关文章
- springboot情操陶冶-web配置(七)
参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...
- springboot情操陶冶-web配置(四)
承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...
- springboot情操陶冶-web配置(二)
承接前文springboot情操陶冶-web配置(一),在分析mvc的配置之前先了解下其默认的错误界面是如何显示的 404界面 springboot有个比较有趣的配置server.error.whit ...
- springboot情操陶冶-web配置(三)
承接前文springboot情操陶冶-web配置(二),本文将在前文的基础上分析下mvc的相关应用 MVC简单例子 直接编写一个Controller层的代码,返回格式为json package com ...
- springboot情操陶冶-web配置(一)
承接前文springboot情操陶冶-@SpringBootApplication注解解析,在前文讲解的基础上依次看下web方面的相关配置 环境包依赖 在pom.xml文件中引入web依赖,炒鸡简单, ...
- springboot情操陶冶-web配置(八)
本文关注应用的安全方面,涉及校验以及授权方面,以springboot自带的security板块作为讲解的内容 实例 建议用户可直接路由至博主的先前博客spring security整合cas方案.本文 ...
- springboot情操陶冶-web配置(六)
本文则针对数据库的连接配置作下简单的分析,方便笔者理解以及后续的查阅 栗子当先 以我们经常用的mybatis数据库持久框架来操作mysql服务为例 环境依赖 1.JDK v1.8+ 2.springb ...
- springboot情操陶冶-web配置(五)
本文讲讲mvc的异常处理机制,方便查阅以及编写合理的异常响应方式 入口例子 很简单,根据之前的文章,我们只需要复写WebMvcConfigurer接口的异常添加方法即可,如下 1.创建简单的异常处理类 ...
- springboot情操陶冶-@SpringBootApplication注解解析
承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上对@SpringBootApplication注解作下简单的分析 @SpringBootApplicat ...
随机推荐
- BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德
BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们 ...
- BZOJ_1712_[Usaco2007 China]Summing Sums 加密_矩阵乘法
BZOJ_1712_[Usaco2007 China]Summing Sums 加密_矩阵乘法 Description 那N只可爱的奶牛刚刚学习了有关密码的许多算法,终于,她们创造出了属于奶牛 ...
- 关于linux下部署JavaWeb项目,nginx负责静态资源访问,tomcat负责处理动态请求的nginx配置
1.项目的运行环境 linux版本 [root@localhost ~]# cat /proc/version Linux version -.el6.x86_64 (mockbuild@x86-.b ...
- typeof和instansof的区别
typeof typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型. 它返回值是一个字符串,该字符串说明运算数的类型.(typeof 运算符返回一个用来表示表达式的数据类型的字符串. ...
- Haskell学习-functor
原文地址:Haskell学习-functor 什么是Functor functor 就是可以执行map操作的对象,functor就像是附加了语义的表达式,可以用盒子进行比喻.functor 的定义可以 ...
- 创建一个 Spring Boot 项目,你会几种方法?
我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 42W+,如下图: 2017 年由于种种原因,就没有 ...
- [翻译 EF Core in Action 1.6]你的第一个EF Core应用程序
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- .netcore2.1在控制器中和类中,获取appsettings中值的方法
一般我们在开发项目中,都会从配置文件中获取数据库连接信息.自定义参数配置信息等. 在.netcore中在控制器和自定义类中,获取配置文件中参数方式如下: appsettings.json { &quo ...
- 折腾Java设计模式之解释器模式
解释器模式 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 意图 给定一个语言,定义它的文法表 ...
- 一起学Android之Http访问
概述 在Android开发中,一般通过网络进行访问服务器端的信息(存储和检索网络中的数据),如API接口,WebService,网络图片等.今天主要讲解Http访问的常用方法,仅供学习分享使用. 涉及 ...