2016-05-06 16 views
0

私は、次のWebセキュリティの設定があります:私はしようとしているとき、今春ControllerAdviceおよび認証/許可例外処理私の春のブートappliationで

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http 
      .headers().frameOptions().disable() 
      .and() 
       .antMatcher("/**").authorizeRequests() 
       .antMatchers("/actuator/health").permitAll() 
       .antMatchers("/actuator/**").hasAuthority(Authority.Type.ROLE_ADMIN.getName()) 
       .antMatchers("/login/**").permitAll() 
       .anyRequest().authenticated() 
      .and() 
       .formLogin() 
        .loginPage("/login") 
        .loginProcessingUrl("/login") 
        .failureUrl("/login?error").permitAll() 
      .and() 
       .logout() 
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
        .logoutSuccessUrl("/login?logout") 
      .and() 
       .csrf().csrfTokenRepository(csrfTokenRepository()).ignoringAntMatchers("/login/**") 
      .and() 
       .addFilterBefore(corsFilter, ChannelProcessingFilter.class) 
       .addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class) 
       .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class) 
       .exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint()); 
     // @formatter:on 
    } 

.... 

} 

ControllerAdviceを匿名ユーザーが安全なAPI URLにアクセスすると、次のエラーが表示されます。

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 
Fri May 06 22:59:05 EEST 2016 
There was an unexpected error (type=Forbidden, status=403). 
Access Denied 

このページの代わりに、私はControllerAdvice(JSONエラー応答を返すために)のような種類のエラーを処理する必要があります。これは他のすべての例外に対して正常に動作しますが、認証されたユーザに対してのみ有効です。

この認証/承認エラーを処理する方法は、GlobalControllerExceptionHandlerですか?

答えて

1

ControllerAdviceは、Controllerクラスを支援するように設計されており、ExceptionHandlerは、Controllerによって送出された例外を処理するためのものです。 AuthenticationExceptionAccessDeniedExceptionは、通常、Spring SecurityのAbstractSecurityInterceptorによってスローされます。だから、私はあなたがAuthenticationExceptionControllerAdviceを使って捕まえることができなくなると思っています.は既にそれを捕らえ、適切なHTTP応答に変換するからです。

あなたのWebSecurityConfigurerAdapterにはexceptionHandlingを使用することをお勧めします。これを使用して、AuthenticationEntryPointAccessDeniedHandlerを設定できます。ここでは、私がアクセスのための403 Forbiddenは例を否定返すことだし、認証を欠落しているため401 Unauthorizedはケースをトークン:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http 
       // The usual stuff 
       exceptionHandling() 
        .accessDeniedHandler((request, response, accessDeniedException) -> { 
         response.sendError(HttpServletResponse.SC_FORBIDDEN); 
        }) 
        .authenticationEntryPoint((request, response, authException) -> { 
         response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 
        }); 
    } 

} 

exceptionHandlingを使用して、あなたはAuthenticationException秒間AccessDeniedException sおよびauthenticationEntryPointを処理するためのaccessDeniedHandlerを使用するようにExceptionTranslationFilterを設定することができます。このプロセスについてより詳しく知るには、ExceptionTranslationFilterをご覧ください。

あなたが好きではない場合:

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 
Fri May 06 22:59:05 EEST 2016 
There was an unexpected error (type=Forbidden, status=403). 
Access Denied 

をし、それをカスタマイズしたい、あなたはErrorControllerの実装を提供し、エラーのMap<String, ErrorResponse>を返す必要があります。

関連する問題