[英]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.