本文关注应用的安全方面,涉及校验以及授权方面,以springboot自带的security板块作为讲解的内容

实例

建议用户可直接路由至博主的先前博客spring security整合cas方案。本文则针对相关的源码作下简单的分析,方便笔者以及读者更深入的了解spring的security板块

@EnableWebSecurity

这个注解很精髓,基本上可以作为security的入口,笔者贴一下它的源码

@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value = { java.lang.annotation.ElementType.TYPE })
@Documented
@Import({ WebSecurityConfiguration.class,
SpringWebMvcImportSelector.class })
@EnableGlobalAuthentication
@Configuration
public @interface EnableWebSecurity { /**
* Controls debugging support for Spring Security. Default is false.
* @return if true, enables debug support with Spring Security
*/
boolean debug() default false;
}

可以分为三个部分来分析,

SpringWebMvcImportSelector-支持mvc的参数安全校验,替代了@EnableWebMvcSecurity注解

WebSecurityConfiguration-Web的安全配置

@EnableGlobalAuthentication-支持公共的认证校验

SpringWebMvcImportSelector

首先先看下其如何整合mvc的安全校验,其是一个ImportSelector接口,观察下其复写的方法

public String[] selectImports(AnnotationMetadata importingClassMetadata) {
boolean webmvcPresent = ClassUtils.isPresent(
"org.springframework.web.servlet.DispatcherServlet",
getClass().getClassLoader());
return webmvcPresent
? new String[] {
"org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration" }
: new String[] {};
}

由上述代码可知,在classpath环境中存在mvc的关键类DispatcherServlet时便会引入WebMvcSecurityConfiguration类,那么此类又配置了什么东西呢?

里面的代码很简单,但关键是其是WebMvcConfigurer接口的实现类,根据之前的文章提到,该接口主要是用于配置MVC的相关功能,比如参数处理器、返回值处理器、异常处理器等等。

而该类只是扩展了相应的参数处理器,我们可以看下源码

	@Override
@SuppressWarnings("deprecation")
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
// 支持@AuthenticationPrinciple参数注解校验
AuthenticationPrincipalArgumentResolver authenticationPrincipalResolver = new AuthenticationPrincipalArgumentResolver();
authenticationPrincipalResolver.setBeanResolver(beanResolver);
argumentResolvers.add(authenticationPrincipalResolver);
// 废弃
argumentResolvers
.add(new org.springframework.security.web.bind.support.AuthenticationPrincipalArgumentResolver());
// csrf token参数
argumentResolvers.add(new CsrfTokenArgumentResolver());
}

针对@AuthenticationPrinciple注解的参数校验,本文不展开了,这里作下归纳

  1. 带有@AuthenticationPrinciple注解的参数其值会从SecurityContext的上下文读取相应的Authentication校验信息
  2. 有一个要求,被该注解修饰的参数须同SecurityContext的上下文存放的Authentication信息为同一接口,否则则会返回null。如果设置了errorOnInvalidType属性为true,则会抛异常
  3. 综上所述,该注解主要是方便将校验通过的Token用于参数赋值,其它的作用也不是很大

@EnableGlobalAuthentication

再来分析下springboot-security的公共认证校验是什么概念,贴下源码

@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value = { java.lang.annotation.ElementType.TYPE })
@Documented
@Import(AuthenticationConfiguration.class)
@Configuration
public @interface EnableGlobalAuthentication {
}

OK,直接进入相应的AuthenticationConfiguration类进行具体的分析


1.其引入了ObjectPostProcessorConfiguration配置用于创建AutowireBeanFactoryObjectPostProcessor类,作用应该是通过Spring上下文实例相应的实体类并注册到bean工厂中

	@Bean
public ObjectPostProcessor<Object> objectPostProcessor(
AutowireCapableBeanFactory beanFactory) {
return new AutowireBeanFactoryObjectPostProcessor(beanFactory);
}

2.创建基于密码机制的认证管理器Bean,类型为DefaultPasswordEncoderAuthenticationManagerBuilder

	@Bean
public AuthenticationManagerBuilder authenticationManagerBuilder(
ObjectPostProcessor<Object> objectPostProcessor, ApplicationContext context) {
// 密码加密器
LazyPasswordEncoder defaultPasswordEncoder = new LazyPasswordEncoder(context);
// 认证事件传播器
AuthenticationEventPublisher authenticationEventPublisher = getBeanOrNull(context, AuthenticationEventPublisher.class);
// 默认的认证管理器
DefaultPasswordEncoderAuthenticationManagerBuilder result = new DefaultPasswordEncoderAuthenticationManagerBuilder(objectPostProcessor, defaultPasswordEncoder);
if (authenticationEventPublisher != null) {
result.authenticationEventPublisher(authenticationEventPublisher);
}
return result;
}

上述的密码加密器支持多种方式的加密,比如bcrypt(默认)/ladp/md5/sha-1等,感兴趣的读者可自行阅读。用户也可多用此Bean作额外的扩展,例如官方建议的如下代码

@Configuration
@EnableGlobalAuthentication
public class MyGlobalAuthenticationConfiguration { @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER")
.and().withUser("admin").password("password").roles("ADMIN,USER");
}
}

3.创建基于UserDetails的认证器,用于管理用户的授权信息

	@Bean
public static InitializeUserDetailsBeanManagerConfigurer initializeUserDetailsBeanManagerConfigurer(ApplicationContext context) {
return new InitializeUserDetailsBeanManagerConfigurer(context);
}

其会创建基于Datasource源的DaoAuthenticationProvider认证校验器,前提是ApplicationContext上下文存在UserDetailsServiceBean对象,否则会不创建。如果用户想基于数据库或者其他数据源的可尝试复写UserDetailsService接口

@Configuration
public class DaoUserDetailsServiceConfig { /**
* load user info by dao
*
* @see org.springframework.security.authentication.dao.DaoAuthenticationProvider
*/
@Configuration
public static class DefaultUserDetailsService implements UserDetailsService { private static final String DEFAULT_PASS = "defaultPass"; // admin authority
private Collection<? extends GrantedAuthority> adminAuthority; @Resource
private PasswordEncoder defaultPasswordEncoder; public DefaultUserDetailsService() {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_ADMIN");
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(authority); adminAuthority = Collections.unmodifiableList(authorities);
} @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User userdetails = new User(username, defaultPasswordEncoder.encode(DEFAULT_PASS), adminAuthority); return userdetails;
} @Bean
public PasswordEncoder daoPasswordEncoder() {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); return passwordEncoder;
}
}
}

注意:实现UserDetailsService的自定义实例请确保只有一个注册至ApplicationContext上,否则上述的基于数据源配置无法自动化配置;但也可通过AuthenticationManagerBuilder#userDetailsService()方法来进行相应的配置


4.创建AuthenticationProvider认证器,用于用户信息的校验

	@Bean
public static InitializeAuthenticationProviderBeanManagerConfigurer initializeAuthenticationProviderBeanManagerConfigurer(ApplicationContext context) {
return new InitializeAuthenticationProviderBeanManagerConfigurer(context);
}

同第三点,只是它就配置简单的AuthenticationProvider至相应的AuthenticationManagerBuilderBean中

所以综上所述,@EnableGlobalAuthentication注解的主要目的是配置认证管理器,里面包含了加密器以及相应的认证器

WebSecurityConfiguration

web方面的安全配置,笔者也根据加载的顺序来进行分析


1.获取WebSecurityConfigurer接口bean集合的AutowiredWebSecurityConfigurersIgnoreParents

	@Bean
public static AutowiredWebSecurityConfigurersIgnoreParents autowiredWebSecurityConfigurersIgnoreParents(
ConfigurableListableBeanFactory beanFactory) {
return new AutowiredWebSecurityConfigurersIgnoreParents(beanFactory);
}

此Bean用于获取所有注册在bean工厂上的WebSecurityConfigurer接口,用户也一般通过此接口的抽象类WebSecurityConfigurerAdapter来进行相应的扩展


2.设置Security的Filter过滤链配置,提前为创建过滤链作准备

	@Autowired(required = false)
public void setFilterChainProxySecurityConfigurer(
ObjectPostProcessor<Object> objectPostProcessor,
@Value("#{@autowiredWebSecurityConfigurersIgnoreParents.getWebSecurityConfigurers()}") List<SecurityConfigurer<Filter, WebSecurity>> webSecurityConfigurers)
throws Exception {
// WebSecurity创建
webSecurity = objectPostProcessor
.postProcess(new WebSecurity(objectPostProcessor));
if (debugEnabled != null) {
webSecurity.debug(debugEnabled);
} // 根据@Order属性排序
Collections.sort(webSecurityConfigurers, AnnotationAwareOrderComparator.INSTANCE); Integer previousOrder = null;
Object previousConfig = null;
// 校验Order对应的值,不允许相同,否则会抛出异常
for (SecurityConfigurer<Filter, WebSecurity> config : webSecurityConfigurers) {
Integer order = AnnotationAwareOrderComparator.lookupOrder(config);
if (previousOrder != null && previousOrder.equals(order)) {
throw new IllegalStateException(
"@Order on WebSecurityConfigurers must be unique. Order of "
+ order + " was already used on " + previousConfig + ", so it cannot be used on "
+ config + " too.");
}
previousOrder = order;
previousConfig = config;
}
// 对排序过的SecurityConfigurer依次放入WebSecurity对象中
for (SecurityConfigurer<Filter, WebSecurity> webSecurityConfigurer : webSecurityConfigurers) {
webSecurity.apply(webSecurityConfigurer);
}
this.webSecurityConfigurers = webSecurityConfigurers;
}

这里便提一下,我们在继承WebSecurityConfigurerAdapter抽象类的时候,记得在其头上加上@Order属性,并且保证值唯一


3.创建Security过滤链

	@Bean(name = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)
public Filter springSecurityFilterChain() throws Exception {
// 如果用户没有配置WebSecurityConfigurer接口,则创建一个空的
boolean hasConfigurers = webSecurityConfigurers != null
&& !webSecurityConfigurers.isEmpty();
if (!hasConfigurers) {
WebSecurityConfigurerAdapter adapter = objectObjectPostProcessor
.postProcess(new WebSecurityConfigurerAdapter() {
});
webSecurity.apply(adapter);
}
// create Filter
return webSecurity.build();
}

看来Filter拦截器的配置是通过WebSecurity这个类来完成的,限于里面的代码过于复杂,本文就不展开了,感兴趣的读者可以重点关注下此类。由此可以得出Springboot的安全校验是通过过滤链的设计方式来完成的


4.URI权限校验Bean,其依赖于第三点的配置

	@Bean
@DependsOn(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)
public WebInvocationPrivilegeEvaluator privilegeEvaluator() throws Exception {
return webSecurity.getPrivilegeEvaluator();
}

5.安全校验表达式验证Bean,其也依赖于第三点的配置,应该是与第四点搭配使用

	@Bean
@DependsOn(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)
public SecurityExpressionHandler<FilterInvocation> webSecurityExpressionHandler() {
return webSecurity.getExpressionHandler();
}

小结

Springboot整合的Security板块内容很多,本文也展示不完,不过值得关注的是以下几个方面

1)WebSecurity的个性化配置类,一般是复写抽象接口WebSecurityConfigurerAdapter,再加上@EnableWebSecurity注解便可

2)AuthenticationManagerBuilder认证校验器,重点关注其中的密码校验器,用于密码的加密解密,默认使用bcrypt方式。如果用户想通过其他数据源获取用户信息,可以关注UserDetailsService接口。推荐用户均使用AuthenticationManagerBuilder类配置认证机制!

3)WebSecurity类,此类是Springboot Security模块的核心类,具体的过滤链配置均是由此类得到的。读者以及笔者应该对此加以关注

springboot情操陶冶-web配置(八)的更多相关文章

  1. springboot情操陶冶-web配置(九)

    承接前文springboot情操陶冶-web配置(八),本文在前文的基础上深入了解下WebSecurity类的运作逻辑 WebSecurityConfigurerAdapter 在剖析WebSecur ...

  2. springboot情操陶冶-web配置(七)

    参数校验通常是OpenApi必做的操作,其会对不合法的输入做统一的校验以防止恶意的请求.本文则对参数校验这方面作下简单的分析 spring.factories 读者应该对此文件加以深刻的印象,很多sp ...

  3. springboot情操陶冶-web配置(四)

    承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...

  4. springboot情操陶冶-web配置(二)

    承接前文springboot情操陶冶-web配置(一),在分析mvc的配置之前先了解下其默认的错误界面是如何显示的 404界面 springboot有个比较有趣的配置server.error.whit ...

  5. springboot情操陶冶-web配置(三)

    承接前文springboot情操陶冶-web配置(二),本文将在前文的基础上分析下mvc的相关应用 MVC简单例子 直接编写一个Controller层的代码,返回格式为json package com ...

  6. springboot情操陶冶-web配置(一)

    承接前文springboot情操陶冶-@SpringBootApplication注解解析,在前文讲解的基础上依次看下web方面的相关配置 环境包依赖 在pom.xml文件中引入web依赖,炒鸡简单, ...

  7. springboot情操陶冶-web配置(六)

    本文则针对数据库的连接配置作下简单的分析,方便笔者理解以及后续的查阅 栗子当先 以我们经常用的mybatis数据库持久框架来操作mysql服务为例 环境依赖 1.JDK v1.8+ 2.springb ...

  8. springboot情操陶冶-web配置(五)

    本文讲讲mvc的异常处理机制,方便查阅以及编写合理的异常响应方式 入口例子 很简单,根据之前的文章,我们只需要复写WebMvcConfigurer接口的异常添加方法即可,如下 1.创建简单的异常处理类 ...

  9. springboot情操陶冶-@SpringBootApplication注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上对@SpringBootApplication注解作下简单的分析 @SpringBootApplicat ...

随机推荐

  1. java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to net.sf.json.JSONObject的解决方法

    报错情况已经说明了,在百度查了好几个解决方法,这里总结一下: 首先:加一个判断是否为空,再做操作 // 得到json串 String jsonString = UtilPOSTGET.UPost(FO ...

  2. java的3大特性

    java的3大特性 1.继承: * 继承是从已有类得到继承信息创建新类的过程. * 提供继承信息的类被称为父类(超类.基类):得到继承信息的类被称为子类(派生类). * 继承让变化中的软件系统有定的延 ...

  3. Ubuntu16.04安装之后连不上无线网?有可能是Realtek rtl8822be的原因

    原以为昨天已基本写完在接触到Ubuntu以来遇到的所有问题了... 没想到今天去看有关ROS的资料时,居然无意间又看到了之前遇到的一个巨坑:安装完Ubuntu16.04之后,无线网用不了,根本无法连接 ...

  4. FCC(ES6写法) Map the Debris

    返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: avgAlt}. 思路: 直接使用公式 ...

  5. Hadoop 集群安装(从节点安装配置)

    1.Java环境配置 view plain copy sudo mv /tmp/java /opt/ jdk安装完配置环境变量,编辑/etc/profile: view plain copy sudo ...

  6. MQTT在react-native中的运行

    mqtt 1.什么是mqtt? MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式 ...

  7. 【计算机篇】Office 2016 for Mac 安装和破解教程

    免责声明 请亲们支持正版.这教程旨在分享,供参考. 为啥写这篇文章 对于大多数使用 Mac 的用户而言,虽然有苹果自家的办公软件,但功能少,用起来不舒服.而 Offer 2016 版的需要登录激活购买 ...

  8. [Swift]LeetCode36. 有效的数独 | Valid Sudoku

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  9. [Swift]LeetCode82. 删除排序链表中的重复元素 II | Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  10. Firefox 多行标签的解决方案分享

    更新了 Quantum 以后 TabMixPlus 就不能用了,我最中意的多行标签也没了,捣鼓了一下终于重新回来了. 指南: https://discourse.mozilla.org/t/tabs- ...