2017-05-08 16 views
3

パラメータ付きSpringセキュリティ認証失敗ハンドラのリダイレクトに問題があります。セキュリティの設定でSpring Securityのカスタム認証失敗ハンドラがパラメータ付きのリダイレクト

私は

failureUrl("/login.html?error=true") 

その[OK]を使用する場合、それは動作します。私は、カスタム認証失敗ハンドラを使用すると、私が試した:

getRedirectStrategy().sendRedirect(request, response, "/login.html?error=true"); 

または

response.sendRedirect(request.getContextPath() + "/login.html?error=true"); 

彼らは常に返す:url/login.html

を私がなぜパラメータ(?error=true)を間違って表示されませんいただきました!知りませんか?

情報:私は春+ JSF +休止+春のセキュリティ

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http 
     .authorizeRequests() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin() 
      .loginPage("/login.html") 
      .usernameParameter("j_username") 
      .passwordParameter("j_password") 
      .loginProcessingUrl("/j_spring_security_check") 
      .failureHandler(customAuthenticationFailureHandler)// .failureUrl("/login.html?error=true")//.successHandler(authSuccsessHandler) 
      .defaultSuccessUrl("/dashboard.html") 
      .permitAll() 
      .and() 
     .logout() 
      .invalidateHttpSession(true) 
      .logoutSuccessUrl("/") 
      .permitAll() 
      .and() 
     .exceptionHandling() 
      .accessDeniedPage("/access.html") 
      .and() 
     .headers() 
      .defaultsDisabled() 
      .frameOptions() 
      .sameOrigin() 
      .cacheControl(); 

    http 
     .csrf().disable(); 
} 

これでcstom認証失敗ハンドラを使用するには:

@Component 
public class CustomAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler { 

    @Override 
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, 
      AuthenticationException exception) throws IOException, ServletException { 
     getRedirectStrategy().sendRedirect(request, response, "/login.html?error=true"); 

    } 
} 

私はいくつかのケースのためのパラメータを変更します。

+0

この答えは役に立つかもしれませんhttp://stackoverflow.com/questions/17199423/spring-security-3-1-custom-authentication-failure-url-with -url-parameters – hrdkisback

答えて

3

URL /login.html?error=trueへの匿名アクセスを許可していないため、ログインページ(/login.html)にリダイレクトされています。

AbstractAuthenticationFilterConfigurer#permitAllは、カスタム障害ハンドラの失敗URLに(誰のため)アクセスを許可しますが、ない:

failureUrl(String)のためだけでなく、HttpSecurityBuilderのURLを保証し、getLoginPage()getLoginProcessingUrl()は、任意のユーザーにアクセスを許可されています。

あなたが AbstractRequestMatcherRegistry#antMatchersで明示的にアクセスを許可する必要が

地図に使用されているHttpMethod気にしないAntPathRequestMatcherインスタンスのリスト。

ExpressionUrlAuthorizationConfigurer.AuthorizedUrl#permitAll

URLは誰もが許可されていることを指定します。 URL(servletPath + pathInfo)に対して事前に定義されたアリスタイルのパターンを比較し

のMatcher:AntPathRequestMatcherquery文字列を無視するので、あなたは、正確なURL /login.html?error=trueを許可する必要はありません

HttpServletRequestのURLのクエリ文字列は無視され、コンストラクタに渡された引数に応じて、一致する文字列は大文字小文字を区別しないか、大文字と小文字を区別します。

あなたの変更された構成:

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http 
     .authorizeRequests() 
      .antMatchers("/login.html").permitAll() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin() 
      .loginPage("/login.html") 
      .usernameParameter("j_username") 
      .passwordParameter("j_password") 
      .loginProcessingUrl("/j_spring_security_check") 
      .failureHandler(customAuthenticationFailureHandler)// .failureUrl("/login.html?error=true")//.successHandler(authSuccsessHandler) 
      .defaultSuccessUrl("/dashboard.html") 
      .permitAll() 
      .and() 
     .logout() 
      .invalidateHttpSession(true) 
      .logoutSuccessUrl("/") 
      .permitAll() 
      .and() 
     .exceptionHandling() 
      .accessDeniedPage("/access.html") 
      .and() 
     .headers() 
      .defaultsDisabled() 
      .frameOptions() 
      .sameOrigin() 
      .cacheControl(); 

    http 
     .csrf().disable(); 
} 
関連する問題