4

私は単純なAuthenticationEntryPointを持っています。これは許可されていないリクエストに対してWWW-Authenticateヘッダーを設定する必要があります。AuthenticationEntryPointはときどき呼び出されるだけです

@Component 
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { 

    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) 
      throws IOException, ServletException { 
     response.setHeader("WWW-Authenticate", "FormBased"); 
     response.sendError(401, authException.getMessage()); 
    } 
} 

は、私はしかし、AuthorizationServerConfigurer

@Override 
public void configure(AuthorizationServerSecurityConfigurer authorizationServerSecurityConfigurer) throws Exception { 
    authorizationServerSecurityConfigurer.authenticationEntryPoint(authenticationEntryPoint); 
} 

この開始メソッドは常に呼び出されていないののconfigureのいずれかの方法でそれを使用します。要求にAuthorizeヘッダーがない場合、またはAuthorizeヘッダー値が 'Basic'で始まらない場合に呼び出されます。ただし、Authorizeヘッダーが 'Basic'で始まる場合、開始メソッドは呼び出されません(応答の値はBasic realm="oauth2/client")。このメソッドが確実に呼び出されるようにするにはどうすればよいですか?

+0

あなたのリクエストを送信するURLは? –

+0

これらのリクエストは '/ oauth/token'へのPOSTです。 – cscan

答えて

4

としてはBasicAuthenticationFilterは関係なく、AuthorizationServerSecurityConfigurerで宣言さApplicationEntryPointBasicApplicationEntryPointを使用しているため、この問題が発生したAliDehghaniによって指摘しました。 BasicAuthenticationFilterは私CustomApplicationEntryPointを使用して取得するためには、私は新しいCustomBasicAuthenticationFilterを作成し、コンストラクタに@Autowire注釈を追加するために必要な:

@Component 
public class CustomBasicAuthenticationFilter extends BasicAuthenticationFilter { 

    @Autowired 
    public CustomBasicAuthenticationFilter(AuthenticationManager authenticationManager, 
            AuthenticationEntryPoint authenticationEntryPoint) { 
     super(authenticationManager, authenticationEntryPoint); 
    } 
} 

これは、その後、AuthorizationServerConfigurer

@Override 
public void configure(AuthorizationServerSecurityConfigurer authorizationServerSecurityConfigurer) throws Exception { 
    authorizationServerSecurityConfigurer 
      .authenticationEntryPoint(authenticationEntryPoint) 
      .addTokenEndpointAuthenticationFilter(customBasicAuthenticationFilter); 
} 
の設定]のいずれかの方法に追加されます

このアプリケーションでは、BasicAuthenticationFilterと機能的に同等のCustomBasicAuthenticationFilterが使用されています。しかし、それは今建設中にAuthenticationEntryPointと宣言された豆を含んでいます - それは私のCustomAuthenticationEntryPointです。

1

アクセストークンを取得するために、あなたはHTTPベーシックを使用してクライアントを認証する必要があります

Authorization: Basic Base64(client_id:client_secret) 

この開始メソッドは常にしかし、呼び出されません。 要求にAuthorizeヘッダーがない場合、またはAuthorize ヘッダーの値が「Basic」で始まらない場合に呼び出されます。承認 ヘッダは「基本」で始まる場合は、開始メソッドが呼び出されていない

春のセキュリティは、各フィルタは、特定の責任を持っており、そのうちの一つが処理でしょうBasicAuthenticationFilterある内部フィルター・チェーンを維持します基本認証。あなたがAuthorizationヘッダを通過しないか、あなたのAuthorizationヘッダがBasicに起動しない、それはセキュリティフィルターに他のフィルタの現在のフィルタをスキップしたい場合

if (header == null || !header.startsWith("Basic ")) { 
    chain.doFilter(request, response); 
    return; 
} 

:あなたはそのdoFilterInteral mthod、you would seeでのぞき見を取る場合鎖。最終的にAuthenticationExceptionのインスタンスがスローされ、ExceptionTranslationFilterExceptionTranslationFilterによってキャッチされ、AuthenticationEntryPointと呼ばれます。

ただし、基本認証ヘッダーを渡すと、BasicAuthenticationFilter自体が認証トークンを処理します。渡された資格情報が無効であった場合は、BasicAuthenticationFilterwould catch the exception itselfとはBasicAuthenticationEntryPointではなく、あなたのAuthenticationEntryPointを呼び出す:

catch (AuthenticationException failed) { 
    SecurityContextHolder.clearContext(); 

    if (debug) { 
     this.logger.debug("Authentication request for failed: " + failed); 
    } 

    this.rememberMeServices.loginFail(request, response); 

    onUnsuccessfulAuthentication(request, response, failed); 

    if (this.ignoreFailure) { 
     chain.doFilter(request, response); 
    } 
    else { 
     this.authenticationEntryPoint.commence(request, response, failed); 
    } 

    return; 
} 
+0

ああ、私は今理解しています。 BasicAuthenticationFilterで使用されるAuthenticationEntryPointを設定するにはどうすればよいですか? – cscan

関連する問題