[英]Spring-boot-oauth2-angularjs, error with CORS after session expiry and redirect to oauth2 server
我有一个前端应用程序,它使用服务器端的Spring-boot,Spring security oauth2和客户端的AngularJs。 我还使用第三方oauth2服务器。 我的问题是,在应用程序会话到期后,sping-security将所有请求重定向到'/ login'(这正是应该如何)并且我获得了302状态代码,其位置在响应头中的auth-server页面上重定向。 但是重定向不会发生,因为出现错误:
XMLHttpRequest无法加载****:// bla-bla / oauth / authorize?client_id = ... andSomeAuthStuff。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 原因'****:// myipadress:8084'因此不允许访问。
问题是为什么第一次进入应用程序或刷新页面或注销和新登录不会出现这样的错误,一切顺利但只有当我得到会话超时时(例如我从死视图发出ajax请求),CORS发生错误:-(
我重现了以下步骤:
一般:
请求网址:*** // myipadress:8084 / appname / login
请求方法:GET
状态代码:302找到
远程地址:myipadress:8084
响应标头:
Access-Control-Allow-Headers:授权,内容类型,接受,x-requested-with,Cache-Control
Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE,PUT
访问控制允许来源:*
访问控制 - 最大 - 年龄:3600
Cache-Control:no-cache,no-store,max-age = 0,must-revalidate
位置:* // authserver / oauth / authorize?client_id = ******&redirect_uri = *:// myipadress:8084 / appname / login&response_type = code&state = BIQ68y
附注:无缓存
服务器:Apache-狼/ 1.1
设置Cookie:JSESSIONID = 05D39263EEF7EC9E24AEE8E1E6693748; 路径= /应用程序的名字/; 仅Http
X-Content-Type的选项:nosniff
X框选项:DENY
X-XSS-保护:1; 模式=块
CORS过滤器:
public class SimpleCORSFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
res.setHeader("Access-Control-Max-Age", "3600");
res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept, x-requested-with, Cache-Control");
chain.doFilter(request, res);
}
@Override
public void destroy() {
}
}
安全配置:
@EnableOAuth2Sso
@Configuration
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.logout()
.and().antMatcher("/**")
.authorizeRequests()
.anyRequest().authenticated()
.and().csrf().disable()
.addFilterBefore(new SimpleCORSFilter(), ChannelProcessingFilter.class);
}
}
在ajax请求后记录:
2016-04-04 14:10:42.613 DEBUG 5428 --- [o-8084-exec-144] osswumatcher.AntPathRequestMatcher:检查请求的匹配:'/ login'; 反对'/ login'2016-04-04 14:10:42.613 DEBUG 5428 --- [o-8084-exec-144] uth2ClientAuthenticationProcessingFilter:请求是处理身份验证2016-04-04 14:10:42.615 DEBUG 5428 - - [o-8084-exec-144] wcHttpSessionSecurityContextRepository:SecurityContext为空或内容为匿名 - 上下文不会存储在HttpSession中。 2016-04-04 14:10:42.657 DEBUG 5428 --- [o-8084-exec-144] sswcSecurityContextPersistenceFilter:SecurityContextHolder现已清除,请求处理完成2016-04-04 14:10:42.658 DEBUG 5428 --- [ o-8084-exec-144] ossweb.DefaultRedirectStrategy:重定向到'****:// authserver / oauth / authorize?client_id = *****&redirect_uri = ***:// myipadress:8084 / appname / login&response_type =代码&状态= iNdnBk”
我相信您需要将@EnableWebSecurity批注添加到CustomWebSecurityConfigurerAdapter。
另外,我尝试采用另一种方式,并以这种方式放置过滤器:
@Override
public void configure(HttpSecurity http) throws Exception {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
source.registerCorsConfiguration("/**", config);
CorsFilter filter = new CorsFilter(source);
http
.logout()
.and().antMatcher("/**")
.authorizeRequests()
.anyRequest().authenticated()
.and().csrf().disable()
.addFilterBefore(filter, ChannelProcessingFilter.class);
}
但它也没有帮助,CORS标题响应全部消失!
我认为原因不是在标题中,而是在响应中的状态代码302中。 我怎么来401而不是302?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.