zyc的博客

疾风亦有归途

概述

spring-security系列的文章总算是写完了,中途有太多的的感悟和细节还没有表达出来,还有很多东西没有涉及到,由于精力有限,手头上还有其它工作需要做,spring-security研究就暂告一段落了,但是整个主干部分目前已经理清了,现在已经可以借助spring-security保护我们的系统了,下面我们对spring-security的学习做一个总结。

阅读全文 »

概述

在spring的很多自动配置类中,一般都会被注释一个enable注解,而在这个enable注解上一般都会使用@Import注解将一些类注册到spring容器中,这和我们平常使用的@Bean@Configuration注解有什么不同呢?本质上它们都是注册bean的,而@Import注解常见的用法是注解在需要通过注解属性动态注入的配置类的类上,这个说法可能有点绕口,我们可以理解为它需要根据注解里面的某些属性选择性的将一些类注入到spring容器中

1
2
3
4
5
6
7
8
9
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

// 要导入的Configuration,ImportSelector,ImportBeanDefinitionRegistrar或常规组件类
Class<?>[] value();

}

value方法表明该注解可以导入被@Configuration注解的类,ImportSelector类型的类,ImportBeanDefinitionRegistrar类型的类,或者是普通的java类,下面我们就分别看一下如何通过@Import注解将bean注入到spring容器中。

阅读全文 »

概述

从spring-security的基本概念核心过滤器的源码分析再到自动配置的原理,我们已经知道了spring-security是如何为我们创建默认的配置,创建默认的过滤器链以及这些过滤器执行的原理,现在还差的一点是这些过滤器是如何执行的,现在我们已经知道最终的生成的过滤器是FilterChainProxy的一个实例,最终就是它来对所有请求进行过滤的。

阅读全文 »

概述

在上一篇WebSecurityConfigurerAdapter源码分析中我们知道了HttpSecurity是如何被添加到WebSecurity中的,并且也知道HttpSecurity是用来构建securityFilterChain的,在实际项目配置中我们也一直在配置HttpSecurity,接下来我们就探索一下它是如何构建securityFilterChain。

阅读全文 »

概述

在上一篇WebSecurity源码分析中我们知道了,HttpSecurity是通过WebSecurityConfigurerAdapter进行配置的,默认情况下如果我们没有编写WebSecurityConfigurerAdapter的子类,那它是如何完成配置的呢?其实在SpringBootWebSecurityConfiguration中有一个默认的WebSecurityConfigurerAdapter的子类

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnMissingBean(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
public class SpringBootWebSecurityConfiguration {

@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER)
static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {

}

}

这就是spring-security为我们默认添加的一个适配器,如果我们在项目中自己定义了一个类继承WebSecurityConfigurerAdapter的话,这个默认的类就不会被加载了。

阅读全文 »

概述

WebSecurity是spring-security整个体系里面最为重要的一个安全类,通过之前的文章分析,我们可以得知spring-security是通过一个名称为springSecurityFilterChain的过滤器对所有的请求进行过滤的,同时在WebSecurityConfiguration源码分析中我们可以得知这个过滤器是通过以下方式被WebSecurity构建出来的

1
2
3
4
5
6
7
8
9
10
11
12
@Bean(name = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)
public Filter springSecurityFilterChain() throws Exception {
boolean hasConfigurers = webSecurityConfigurers != null
&& !webSecurityConfigurers.isEmpty();
if (!hasConfigurers) {
WebSecurityConfigurerAdapter adapter = objectObjectPostProcessor
.postProcess(new WebSecurityConfigurerAdapter() {
});
webSecurity.apply(adapter);
}
return webSecurity.build();
}

本章主要分析WebSecurity构建SpringSecurityFilterChain的流程,围绕WebSecurity的build方法展开。

阅读全文 »

概述

相信每个java程序员都曾经在编码的过程中遇到过NullPointerException,毕竟在程序运行期间,谁也不能百分百保证当前正在调用的对象一定不为null。本文要介绍的是java 1.8新加入的一个类Optional,使用它我们就可以在代码中优雅的处理null,避免了大量if (o == null)这种操作,同时最大限度避免程序在运行时抛出NullPointerException

阅读全文 »