2016-07-28 20 views
2

設定済みのSSO/OAuth2セキュリティでSpringブートアプリケーションを開発中です。 残りのコントローラで認証が正常に動作し、残りのエンドポイントでApache Camelルートを保護する必要があります。Spring SecurityとOAuth2でApache Camelの残りのエンドポイントを保護する方法

  1. jettyエンドポイントへのハンドラのオプションで私のルート
  2. にポリシー(SpringSecurityAuthorizationPolicy)を加えることで、私のルート
  3. に認証プロセッサを追加することにより:私はそれを行うにはどのようにいくつかの方法があります理解したよう

私は残りのエンドポイントに新しい認証プロセッサーを追加することでこれを実行しようとしていますが、この例外を回避します:

はorg.springframework.security.oauth2.common.exceptions.OAuth2Exception: ませんAuthenticationProviderは、私がいることがわかりデバッグ中org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken

見つかりorg.springframework.security.authentication.ProviderManager.getProviders()には1つのプロバイダしか含まれていませんAnonymousAuthenticationProviderおそらく私は適切なプロバイダを登録する必要があります。

誰かがこの問題を解決するための正しい方法を教えてくれますか?

@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    protected void configure(HttpSecurity http) throws Exception { 
    http.csrf().disable().authorizeRequests().anyRequest().permitAll(); 
    } 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

    @Value("${oauth2.token.endpoint}") 
    private String tokenEndpoint; 

    @Bean 
    public ResourceServerTokenServices tokenService() { 
     RemoteTokenServices tokenServices = new RemoteTokenServices(); 
     tokenServices.setClientId("clientId"); 
     tokenServices.setClientSecret("clientSecret"); 
     tokenServices.setCheckTokenEndpointUrl(tokenEndpoint); 
     return tokenServices; 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().anyRequest().authenticated(); 
    } 
    } 

} 

@Configuration 
public class EmbeddedServerRoute { 
    @Bean 
    public RoutesBuilder embeddedServer() { 
    return new RouteBuilder() { 
     @Override 
     public void configure() throws Exception { 
     restConfiguration().component("jetty").port("8081").bindingMode(RestBindingMode.json); 
     } 
    }; 
    } 
} 


@Component 
public class RestTestRoute extends RouteBuilder { 

    @Autowired 
    private AuthProcessor authProcessor; 

    @Override 
    public void configure() throws Exception { 
    from("rest:get:/test").process(authProcessor).to("mock:end").end(); 
    } 
} 


@Component 
public class AuthProcessor implements Processor { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    private TokenExtractor tokenExtractor = new BearerTokenExtractor(); 

    private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new OAuth2AuthenticationDetailsSource(); 

    @Override 
    public void process(Exchange exchange) throws Exception { 
    HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class); 
    Subject subject = new Subject(); 
    Authentication auth = getAuth(request); 
    subject.getPrincipals().add(auth); 
    exchange.getIn().setHeader(Exchange.AUTHENTICATION, subject); 
    } 

    private Authentication getAuth(HttpServletRequest request) throws OAuth2Exception { 
    Authentication authentication = null; 
    try { 
     authentication = tokenExtractor.extract(request); 
     if (authentication != null) { 
     request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, authentication.getPrincipal()); 

     if (authentication instanceof AbstractAuthenticationToken) { 
      AbstractAuthenticationToken needsDetails = (AbstractAuthenticationToken) authentication; 
      needsDetails.setDetails(authenticationDetailsSource.buildDetails(request)); 
     } 
     return authenticationManager.authenticate(authentication); 
     } 
    } catch (Exception e) { 
     throw new OAuth2Exception(e.getMessage()); 
    } 
    throw new OAuth2Exception("Not Authorized to view resource"); 
    } 

} 

答えて

3

最後の解決策として、Apache Camelの残りのコンポーネントの代わりにSpringブートの組み込みサーブレットコンテナを使用することに決めました。だから、Spring Securityによって簡単に保護することができます。これは、追加のBeanを作成することによって実行できます。

関連する問題