spring-security的过滤器链

栏目: 后端 · 发布时间: 7年前

内容简介:每次在项目中使用spring-security都想调试其内部逻辑,在这里记录一下调试的入口点,以防忘记。调试的入口在另外记住一点:当spring-mvc中配有CORS,并且在spring-security配置中没有将

每次在项目中使用spring-security都想调试其内部逻辑,在这里记录一下调试的入口点,以防忘记。

正文

调试的入口在 org.springframework.security.web.DefaultSecurityFilterChain#DefaultSecurityFilterChain(org.springframework.security.web.util.matcher.RequestMatcher, java.util.List<javax.servlet.Filter>)

public DefaultSecurityFilterChain(RequestMatcher requestMatcher, List<Filter> filters) {
	logger.info("Creating filter chain: " + requestMatcher + ", " + filters);
	this.requestMatcher = requestMatcher;
	this.filters = new ArrayList<>(filters); // 在这里加断点就能看到整个过滤器链了
}

另外记住一点:当spring-mvc中配有CORS,并且在spring-security配置中没有将 CorsConfigurationSource 明确配置到 corsFilter 这个 Bean 上时,spring-security会“借用”spring-mvc中的CORS配置,切勿配置多余的CORS,关于这个行为可从调试 org.springframework.web.filter.CorsFilter 得知(另外官方文档上也明确写了这个行为了)

...
        // https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#cors
        private CorsConfigurationSource corsConfigurationSource() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.setAllowedOrigins(Arrays.asList(webConfigurationProperties.getCors().getAllowedOrigins()));
            corsConfiguration.setAllowedMethods(Arrays.asList(webConfigurationProperties.getCors().getAllowedMethods()));
            corsConfiguration.setAllowedHeaders(Arrays.asList(webConfigurationProperties.getCors().getAllowedHeaders()));
            corsConfiguration.setExposedHeaders(Arrays.asList(webConfigurationProperties.getCors().getExposedHeaders()));
            corsConfiguration.setAllowCredentials(webConfigurationProperties.getCors().getAllowCredentials());
            corsConfiguration.setMaxAge(webConfigurationProperties.getCors().getMaxAge());
            UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
            urlBasedCorsConfigurationSource.registerCorsConfiguration(webConfigurationProperties.getCors().getMapping(), corsConfiguration);
            return urlBasedCorsConfigurationSource;
        }
        
        @Bean
        public CorsFilter corsFilter() {
            return new CorsFilter(corsConfigurationSource());
        }
...

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

挑战程序设计竞赛

挑战程序设计竞赛

秋叶拓哉、岩田阳一、北川宜稔 / 巫泽俊、庄俊元、李津羽 / 人民邮电出版社 / 2013-7-1 / CNY 79.00

世界顶级程序设计高手的经验总结 【ACM-ICPC全球总冠军】巫泽俊主译 日本ACM-ICPC参赛者人手一册 本书对程序设计竞赛中的基础算法和经典问题进行了汇总,分为准备篇、初级篇、中级篇与高级篇4章。作者结合自己丰富的参赛经验,对严格筛选的110 多道各类试题进行了由浅入深、由易及难的细致讲解,并介绍了许多实用技巧。每章后附有习题,供读者练习,巩固所学。 本书适合程序设计......一起来看看 《挑战程序设计竞赛》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码