2016-04-04 14 views
1

サーバー側でSpringブート、Springセキュリティoauth2、クライアント側でAngularJを使用するフロントエンドアプリケーションがあります。また、サードパーティのoauth2サーバーも使用しています。私の問題は、アプリケーションセッションが終了した後、sping-securityがすべてのリクエストを '/ login'にリダイレクトすることです(それが正確にどうなるべきか)、レスポンスヘッダーのauth-serverページでリダイレクトする場所を持つ302ステータスコードが得られました。セッション終了後CORSでエラーが発生し、oauth2サーバーにリダイレクトされます。

XMLHttpRequest cannot load ****://bla-bla/oauth/authorize?client_id=...andSomeAuthStuff. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '****://myipadress:8084' is therefore not allowed access.

質問はアプリケーションに、なぜ最初のエントリであるか、ページまたはログアウトを更新し、新しいログインは、このようなエラーを伴わず、すべてがうまく行くが、ときにのみ:私はエラーを取得するので、しかし、リダイレクトは起こりません。 「デッド」ページで

  1. 私はAJAXリクエストを作る:私は手順を再現

    :-(私は、CORSエラーが発生します(たとえば、私が死んビューからAJAX要求を行う)セッションタイムアウトを取得します私のAPIに(春ブート1.3.3 WARがpで実行中rovided Tomcatは8)
  2. 春には、要求をインターセプトし、応答します

一般:

Request URL:***//myipadress:8084/appname/login

Request Method:GET

Status Code:302 Found

Remote Address:myipadress:8084

レスポンスヘッダ:

Access-Control-Allow-Headers:Authorization, Content-Type, Accept, x-requested-with, Cache-Control

Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE, PUT

Access-Control-Allow-Origin:*

Access-Control-Max-Age:3600

Cache-Control:no-cache, no-store, max-age=0, must-revalidate

Location:*//authserver/oauth/authorize?client_id=******&redirect_uri=*://myipadress:8084/appname/login&response_type=code&state=BIQ68y

Pragma:no-cache

Server:Apache-Coyote/1.1

Set-Cookie:JSESSIONID=05D39263EEF7EC9E24AEE8E1E6693748; Path=/appname/; HttpOnly

X-Content-Type-Options:nosniff

X-Frame-Options:DENY

X-XSS-Protection:1; mode=block

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] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/login'; against '/login' 2016-04-04 14:10:42.613 DEBUG 5428 --- [o-8084-exec-144] uth2ClientAuthenticationProcessingFilter : Request is to process authentication 2016-04-04 14:10:42.615 DEBUG 5428 --- [o-8084-exec-144] w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession. 2016-04-04 14:10:42.657 DEBUG 5428 --- [o-8084-exec-144] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed 2016-04-04 14:10:42.658 DEBUG 5428 --- [o-8084-exec-144] o.s.s.web.DefaultRedirectStrategy : Redirecting to '****://authserver/oauth/authorize?client_id=*****&redirect_uri=***://myipadress:8084/appname/login&response_type=code&state=iNdnBk'

答えて

0

私はあなたのCustomWebSecurityConfigurerAdapterに@EnableWebSecurityアノテーションを追加する必要が信じています。

@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ヘッダーがすべてdissapeared:

+0

私はこの注釈をフックしましたが、問題は解決しません。私は鉱山に似たいくつかのケースと、それが応答した302リダイレクトの下にあったすべての問題を発見しました。残念なことに、すべてのソリューションは問題を解決しません – jackK

0

はまた、私は他の道を行くと、このような方法でフィルタを入れてみました!

0

理由はヘッダーではなく、応答のステータスコード302にあると思います。 302の代わりに401に来るにはどうすればいいですか?