本文讲述的是访问oauth/token的时候发生跨域问题,但已经加了cros还是不能解决的

通过日志查看得知,是因为Spring Security启动了两个过滤链

o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/oauth/token'], Ant [pattern='/oauth/token_key'], Ant [pattern='/oauth/check_token']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@e4348c0, org.springframework.security.web.context.SecurityContextPersistenceFilter@1382a7d8, org.springframework.security.web.header.HeaderWriterFilter@60e80279, org.springframework.security.web.authentication.logout.LogoutFilter@568c9ee1, org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter@285c63cf, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@6f1163f7, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6074d638, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@402feb85, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4ca907af, org.springframework.security.web.session.SessionManagementFilter@7fc56d61, org.springframework.security.web.access.ExceptionTranslationFilter@45b6c666, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7e451790]

o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/login'], Ant [pattern='/oauth/authorize'], Ant [pattern='/oauth/token'], Ant [pattern='/sms/validate'], Ant [pattern='/user']]], [com.eryun.identity.server.config.CustomerCorsFilter@3b0ed98a, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@76c587ce, org.springframework.security.web.context.SecurityContextPersistenceFilter@2ae5580, org.springframework.security.web.header.HeaderWriterFilter@533d7c61, org.springframework.web.filter.CorsFilter@2ae7c1d, org.springframework.security.web.authentication.logout.LogoutFilter@3fc5d397, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7d82ca56, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@36eb8e07, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5265a8dd, org.springframework.security.web.session.SessionManagementFilter@470f0637, org.springframework.security.web.access.ExceptionTranslationFilter@48f4264e, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@e4bb10b]

/oauth/*被加入到另一个Spring Security自己的过滤链了,没有进入我自己写的 CustomerCorsFilter 里,那就只能把自己的过滤链先于系统自带的加载才行

package com.eryun.identity.server.config;import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import java.util.Arrays;
import java.util.List;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomerCorsFilter extends org.springframework.web.filter.CorsFilter {public CustomerCorsFilter() {super(configurationSource());}private static UrlBasedCorsConfigurationSource configurationSource() {CorsConfiguration corsConfig = new CorsConfiguration();List<String> allowedHeaders = Arrays.asList("x-auth-token", "content-type", "X-Requested-With", "XMLHttpRequest","Access-Control-Allow-Origin","Authorization","authorization");List<String> exposedHeaders = Arrays.asList("x-auth-token", "content-type", "X-Requested-With", "XMLHttpRequest","Access-Control-Allow-Origin","Authorization","authorization");List<String> allowedMethods = Arrays.asList("POST", "GET", "DELETE", "PUT", "OPTIONS");List<String> allowedOrigins = Arrays.asList("*");corsConfig.setAllowedHeaders(allowedHeaders);corsConfig.setAllowedMethods(allowedMethods);corsConfig.setAllowedOrigins(allowedOrigins);corsConfig.setExposedHeaders(exposedHeaders);corsConfig.setMaxAge(36000L);corsConfig.setAllowCredentials(true);UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", corsConfig);return source;}
}
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate CustomerCorsFilter customerCorsFilter;@Autowiredprivate UserDetailsConfig userDetailsConfig;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.cors().and().authorizeRequests().antMatchers("/login", "/oauth/authorize", "/oauth/token", "/sms/validate", "/user").permitAll().anyRequest().authenticated().and().formLogin().permitAll().and().csrf().disable().addFilterBefore(customerCorsFilter, WebAsyncManagerIntegrationFilter.class);}
}

Spring Security使用Oauth2时的跨域问题相关推荐

  1. Spring Boot集成Ueditor富文本编辑器,实现图片上传,视频上传,返回内容功能并且通过OSS转换为链接并且解决Spring Security静态资源访问以及跨域问题

    学习自https://cloud.tencent.com/developer/article/1452451 现在是晚上22点,刚刚和我们的前端交流完了富文本编辑器的一些意见和看法 还是老样子 需求 ...

  2. Spring Security、oauth2、单点登陆SSO的关系

    文章目录 概述 1. 什么是Spring Security 1.1 配置示例 1.2 spring security 基本原理 1.2 Spring Security存在的问题 2. 什么是oauth ...

  3. Spring Security 与 OAuth2 介绍

    个人 OAuth2 全部文章 Spring Security 与 OAuth2(介绍):https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 ...

  4. angular5使用httpclient时解决跨域问题

    angular5使用httpclient时解决跨域问题 参考文章: (1)angular5使用httpclient时解决跨域问题 (2)https://www.cnblogs.com/modou/p/ ...

  5. m3u8下载时出现跨域的解决方法

    m3u8下载时出现跨域复制下面的代码 当无法下载,资源发生跨域限制时,在视频源页面打开控制台,注入代码解决,点击复制下面代码 // 注入htmllet $section = document.crea ...

  6. SpringBoot中配置拦截器时,跨域失效

    SpringBoot中配置拦截器时,跨域失效 前后段分离的项目,配置了跨域后,访问正常,但是配置了拦截器以后,有的访问正常,有的出现跨域问题,发现出现跨域问题的都是拦截器里面没有放行的请求. @Con ...

  7. 关于 Spring Security OAuth2 中 CORS 跨域问题

    CORS 是一个 W3C 标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求 ...

  8. Spring Boot笔记-解决前后端分离在开发时的跨域问题

    这里可以用Nginx解决跨越问题,也可以用下面这种方式在开发时解决: @Configuration public class CorsConfig implements WebMvcConfigure ...

  9. 微信公众号网页版,获取用户code时出现跨域问题的解决办法之一

    前提是微信公众号后台设置没有问题. 网页授权域名不要设置有问题 根据微信[官方文档] 第一步:用户同意授权,获取code 在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高 ...

  10. Spring boot使用Spring Security和OAuth2保护REST接口

    在本文中,我将会演示SpringSecurity+OAuth2如何去保护SpringBoot上的RESTAPI端点,以及客户端和用户凭证如何存储在关系数据库中.因此,我们需要做到以下几点: 配置Spr ...

最新文章

  1. 谈谈 Swift 中的 map 和 flatMap
  2. pytorch CrossEntropyLoss用法
  3. [iOS]C语言技术视频-10-指针变量
  4. 【OS】Linux命令如何放到后台运行
  5. RESET MASTER 和RESET SLAVE 命令的使用方法 注意事项
  6. 【Qt】Qt下载教程
  7. 谷歌技术三宝之BigTable
  8. Linux 命令之 who -- 打印当前登录用户/显示目前登入系统的用户信息。
  9. 计量经济学第六版计算机答案,伍德里奇计量经济学导论计算机习题第六章第13题c_6.13...
  10. 洛谷P5703、P5704、P5705、P5706题题解(Java语言描述)
  11. 基础编程题目集 6-2 多项式求值 (15 分)
  12. VIM插件——vimplus安装(centos 7)
  13. 怎样用DNSPod做负载均衡?
  14. vscode可以配置哪些编程语言_vscode支持哪些编程语言
  15. 2016TI杯——寻迹小车
  16. 深富策略:个股情绪偏弱 市场继续缩量震荡
  17. 手机号码相关信息整理
  18. [USACO06DEC]最少的硬币The Fewest Coins
  19. 为什么Excel公式使用不了?
  20. oracle数据库存储管理--ASM

热门文章

  1. 中创向心力:如何落实好职业教育改革的重点任务?
  2. 一笔一划间蕴藏的学问 浅谈计算机字体
  3. 5道贪心算法 + 1道思维 + 1道搜索
  4. 惊呆了!授权员工浏览用户隐私信息,2.2 亿会员隐私信息泄露
  5. igp路由之OSPF
  6. linux怎样使用top命令查看系统状态
  7. Python读取及保存mat文件 注意事项
  8. 前端自适应屏幕分辨率
  9. C语言数据类型的范围
  10. 矩阵旋转 java_在Java中旋转NxN矩阵