博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
漫谈Spring Security 在Spring Boot 2.x endpoints中的应用(一)
阅读量:5035 次
发布时间:2019-06-12

本文共 5161 字,大约阅读时间需要 17 分钟。

    Spring Boot 2.x极大简化了默认的安全配置,并不是说有很多安全相关的配置,现在你只需要提供一个WebSecurityConfigurerAdapter继承类这样一个简单的操作,Spring Boot就可以规避很多安全问题。

Actuator 不再有各自单独的安全配置(management.security.*配置已被取消),每个endpoint的sensitive 标志也会被取消,这样使得安全配置更加明确了。

比如说:你有如下配置

endpoints:    info:        sensitive: false    mappings:        sensitive: truemanagement:    security:        roles: MY_ADMIN
now,you can do it like this:
import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/** * name: TestWebSecurityConfigureAdapter * * @author aboruo * @Description an example on adding our custom WebSecurityConfigurerAdapter * @Date create in 2019/9/9 20:50. */@EnableWebSecuritypublic class TestWebSecurityConfigureAdapter extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.csrf().disable().authorizeRequests()                .antMatchers("/actuator/health","/actuator/info")                .permitAll()                .antMatchers("/actuator/**")                .hasRole("MY_ADMIN")                .and().httpBasic();    }}

 

请注意,在2.x中,默认情况下 health 和info 是可以被访问的,(默认情况下 health 的详细信息不能被访问显示)。 为了与这些新的默认值保持一致,health 已被添加到首要的mather中。

Spring boot 2.x 不引入Spring Security时,endpoint实现(未完待续)

1. 先在spring-boot-autoconfigure的spring.factories文件找到autoconfiguration类

 

 查看此类

/** * {
@link EnableAutoConfiguration Auto-configuration} for Spring Security. * * @author Dave Syer * @author Andy Wilkinson * @author Madhura Bhave * @since 1.0.0 */@Configuration@ConditionalOnClass(DefaultAuthenticationEventPublisher.class)@EnableConfigurationProperties(SecurityProperties.class)@Import({ SpringBootWebSecurityConfiguration.class, WebSecurityEnablerConfiguration.class, SecurityDataConfiguration.class })public class SecurityAutoConfiguration { @Bean @ConditionalOnMissingBean(AuthenticationEventPublisher.class) public DefaultAuthenticationEventPublisher authenticationEventPublisher(ApplicationEventPublisher publisher) { return new DefaultAuthenticationEventPublisher(publisher); }}
DefaultAuthenticationEventPublisher: 默认使用的权限授权事件publisher
SecurityProperties: 安全设置相关属性配置文件,以:spring.security开头 通过 SecurityAutoConfiguration 又引入了几个关键的配置类
① SpringBootWebSecurityConfiguration
/** * The default configuration for web security. It relies on Spring Security's * content-negotiation strategy to determine what sort of authentication to use. If the * user specifies their own {
@link WebSecurityConfigurerAdapter}, this will back-off * completely and the users should specify all the bits that they want to configure as * part of the custom security configuration. * * @author Madhura Bhave * @since 2.0.0 */@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 boot 默认的安全配置类,它依赖于Spring安全的*内容协商策略来确定使用哪种身份验证。通过代码,我们可以看到:
  • 当用户定义了自己的WebSecurityConfigurerAdapter类时,SpringBootWebSecurityConfiguration将不会生效;
  • 当应用是web应用且类型是SERVLET类型时才会生效
② WebSecurityEnablerConfiguration 这是一个确认配置类,顾名思义:当applicationContext中存在WebSecurityConfigureAdapter类型的bean时,它才会生效,它的职责是这类bean加@EnableWebSecurity注解。
/** * If there is a bean of type WebSecurityConfigurerAdapter, this adds the * {
@link EnableWebSecurity} annotation. This will make sure that the annotation is * present with default security auto-configuration and also if the user adds custom * security and forgets to add the annotation. If {
@link EnableWebSecurity} has already * been added or if a bean with name {
@value BeanIds#SPRING_SECURITY_FILTER_CHAIN} has * been configured by the user, this will back-off. * * @author Madhura Bhave * @since 2.0.0 */@Configuration@ConditionalOnBean(WebSecurityConfigurerAdapter.class)@ConditionalOnMissingBean(name = BeanIds.SPRING_SECURITY_FILTER_CHAIN)@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)@EnableWebSecuritypublic class WebSecurityEnablerConfiguration {}

 

③ SecurityDataConfiguration 当应用环境中存在SecurityEvaluationContextExtension类时,自动添加带有Spring Data 的 spring security 集成。
/** * Automatically adds Spring Security's integration with Spring Data. * * @author Rob Winch * @since 1.3.0 */@Configuration@ConditionalOnClass(SecurityEvaluationContextExtension.class)public class SecurityDataConfiguration {    @Bean    @ConditionalOnMissingBean    public SecurityEvaluationContextExtension securityEvaluationContextExtension() {        return new SecurityEvaluationContextExtension();    }}

 

后续我们会对
SecurityRequestMatcherProviderAutoConfiguration
UserDetailsServiceAutoConfiguration
SecurityFilterAutoConfiguration
OAuth2ClientAutoConfiguration
OAuth2ResourceServerAutoConfiguration
这几个类逐一进行介绍,从而来了解它的工作原理。

转载于:https://www.cnblogs.com/aboruo/p/11494505.html

你可能感兴趣的文章
WCF 配置文件
查看>>
动态调用WCF服务
查看>>
oracle导出/导入 expdp/impdp
查看>>
类指针
查看>>
css修改滚动条样式
查看>>
2018.11.15 Nginx服务器的使用
查看>>
Kinect人机交互开发实践
查看>>
百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET...
查看>>
JAVA 技术类分享(二)
查看>>
android客户端向服务器发送请求中文乱码的问
查看>>
UOJ#220. 【NOI2016】网格 Tarjan
查看>>
Symfony翻译教程已开课
查看>>
Python模块之pickle(列表,字典等复杂数据类型与二进制文件的转化)
查看>>
通过数据库表反向生成pojo类
查看>>
css_去掉默认样式
查看>>
TensorFlow2.0矩阵与向量的加减乘
查看>>
NOIP 2010题解
查看>>
javascript中的each遍历
查看>>
String中各方法多数情况下返回新的String对象
查看>>
浅谈tcp粘包问题
查看>>