2017-05-19 5 views
0

에 따라 여러 인증 공급자를 선택할 나는 같은 내 응용 프로그램을 구성한봄 부팅/봄 보안 경로

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests().antMatchers("/api/v1").authenticated(); 
    http.authorizeRequests().antMatchers("/api/v2").authenticated(); 
} 

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

내가 호출되는 /api/v2provider1를 호출하는 경우 지금 무슨 일하고 예외를 발생 또는 false를 반환하지 않는 경우 만 전화를받을 provider2 않습니다.

// In org.springframework.security.authentication.ProviderManager 

for (AuthenticationProvider provider : getProviders()) { 
     try { 
      result = provider.authenticate(authentication); 

      if (result != null) { 
       copyDetails(authentication, result); 
       break; 
      } 
     } 
     ... 
     ... 
} 

어떻게 내가 /api/v2에 충돌하는 경우에만 provider2가 호출되는 그래서 할 수 있습니까? 이 같은

뭔가 :

http.authorizeRequests().antMatchers("/api/v2") 
    .authenticated().authenticationProvider(provider2); 

(나는 HttpSecurityauthenticationProvider가 알고있는, 그러나,이 AuthenticationManagerBuilder#authenticationProvider 전화로 정확한 동일)

답변

0

당신은 봄 보안에 사용자 정의 필터를 추가 할 수 있습니다 사용자 지정 Authentication 구현을 사용하여 요청을 인증하려고 시도하는 필터 체인 AuthenticationManager.authenticate()에 전화하면 사용자 지정 인증의 인스턴스를 전달할 수 있습니다. 그런 다음 사용자 지정 공급자의 지원 (클래스 <?> 인증) 메서드를 사용자 지정 인증 클래스 만 수락하도록 업데이트해야합니다.

그런 다음 antmatcher를 사용하여 특정 끝점에 대한 보안 필터 체인에 사용자 지정 필터를 적용 할 수 있습니다.

+0

다음은 [맞춤 인증 필터] (http://stackoverflow.com/a/27564349/873590)의 예입니다. – dsep