2014-02-14 1 views
3

url의 토큰 id를 사용하여 나머지 서비스를 인증해야한다. 나는 이것으로 가이드를 사용하여 이것을 설정하기 위해 자바 설정을 사용하려고한다. post. 내 문제는 공급자에서 인증이 실패 할 때 throw되는 "BadCredentialsException"을 처리하는 방법을 모르겠다는 것입니다. 여기에 내 보안 구성이 있습니다 :자바 설정을 통한 스프링 보안 : 커스텀 제공자로부터 BadCredentialsException을 처리하는 방법

public static class SecurityConfigForRS extends 
     WebSecurityConfigurerAdapter { 

    @Autowired 
    TokenAuthenticationProvider tokenAuthenticationProvider; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception { 
     auth.authenticationProvider(tokenAuthenticationProvider); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() 
      throws Exception { 
     return super.authenticationManagerBean(); 
    } 

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

     http.regexMatcher("^/rest.*") 
       .addFilterBefore(
         new TokenAuthenticationFilter(
           authenticationManagerBean()), 
         AbstractPreAuthenticatedProcessingFilter.class) 
       .and().csrf().disable(); 

    } 
} 

다른 구현은 건너 뜁니다. 도움이된다면 나중에 게시 할 것입니다.

토큰이 없거나 유효하지 않은 경우 TokenAuthernticationProviderBadCredentialsException을 던집니다. 나는 이것을 받아 들여 401-Unauthorized을 돌려 보내야합니다. 이것을 할 수 있습니까?

답변

5

내가 만든 첫 번째 필터는 GenericFilterBean의 하위 클래스이며 인증 실패 처리기 또는 성공 처리기를 지원하지 않습니다. 그러나 AbstractAuthenticationProcessingFilter은 성공 및 실패 처리기를 지원합니다.

public class TokenAuthenticationProcessingFilter extends 
    AbstractAuthenticationProcessingFilter { 

public TokenAuthenticationProcessingFilter(
     RequestMatcher requiresAuthenticationRequestMatcher) { 
    super(requiresAuthenticationRequestMatcher); 
} 

@Override 
public Authentication attemptAuthentication(HttpServletRequest request, 
     HttpServletResponse response) throws AuthenticationException, 
     IOException, ServletException { 
    Authentication auth = new TokenAuthentication("-1"); 
    try { 
     Map<String, String[]> params = request.getParameterMap(); 
     if (!params.isEmpty() && params.containsKey("auth_token")) { 
      String token = params.get("auth_token")[0]; 
      if (token != null) { 
       auth = new TokenAuthentication(token); 
      } 
     } 
     return this.getAuthenticationManager().authenticate(auth); 
    } catch (AuthenticationException ae) { 
     unsuccessfulAuthentication(request, response, ae); 
    } 
    return auth; 
}} 

내 HTTP 보안은 다음과 같습니다 : 내 필터는만큼 간단

public static class SecurityConfigForRS extends 
     WebSecurityConfigurerAdapter { 

    @Autowired 
    TokenAuthenticationProvider tokenAuthenticationProvider; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception { 
     auth.authenticationProvider(tokenAuthenticationProvider); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() 
      throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Bean 
    protected AbstractAuthenticationProcessingFilter getTokenAuthFilter() 
      throws Exception { 
     TokenAuthenticationProcessingFilter tapf = new TokenAuthenticationProcessingFilter(
       new RegexRequestMatcher("^/rest.*", null)); 
     tapf.setAuthenticationManager(authenticationManagerBean()); 
     return tapf; 
    } 

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

     http.regexMatcher("^/rest.*") 
       .addFilterAfter(getTokenAuthFilter(), 
         BasicAuthenticationFilter.class).csrf().disable(); 

    } 
} 

필터 체인 순서 문제를 않습니다! BasicAuthenticationFilter 뒤에 배치하고 잘 작동합니다. 당연히 더 나은 해결책이 있을지도 모르지만 지금 당장이 작품!