2016-04-22 10 views
3

私は自分のアプリケーションでOAuth 2を動作させようとしてきましたが、特に認証トークンに関する設定に関連するエラーが発生しました。アプリケーションは、許可サーバーとリソースサーバーの両方として機能するように設定されています。私は首尾よくトークンを発行するようにそれを設定しました。しかし、私は制限されたリソースに対する要求を送信しようとするたびに、私はエラーが言ってます:PreAuthenticationAuthenticationProviderを設定するにはどうすればよいですか?

org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken 

だから、私は私の構成でPreAuthenticationAuthenticationProviderを設定してみました:

@Autowired 
private UserDetailsManager userManager; 

@Bean 
public AuthenticationProvider preAuthenticationAuthenticationProvider() { 
    PreAuthenticatedAuthenticationProvider authenticationProvider = 
     new PreAuthenticatedAuthenticationProvider(); 
    UserDetailsByNameServiceWrapper userDetailsWrapper = new UserDetailsByNameServiceWrapper(userManager); 
    authenticationProvider.setPreAuthenticatedUserDetailsService(userDetailsWrapper); 
    return authenticationProvider; 
} 

しかし、私は取得していますNullPointerExceptionのような奇妙な場所で:

java.lang.NullPointerException: null 
    at org.springframework.security.authentication.AccountStatusUserDetailsChecker.check(AccountStatusUserDetailsChecker.java:17) ~[spring-security-core-4.0.3.RELEASE.jar!/:4.0.3.RELEASE] 

私はこのための最も簡単な構成とは何か思ったんだけど、私は最初の場所でそれを必要とする理由?それは私が@PreAuthorize注釈を持っているからですか?

ここで私は、リソースサーバーの設定方法は次のとおりです。

@Configuration 
protected static class ResourceServer extends ResourceServerConfigurerAdapter { 

    @Autowired 
    private TokenStore tokenStore; 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
     resources.tokenStore(tokenStore).authenticationManager(authenticationManager); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     //http configuration 
    } 

} 

TokenStoreがちょうどInMemoryTokenStoreAuthenticationManagerのインスタンスがこのように設定されている:

@Configuration 
protected static class WebSecurity extends WebSecurityConfigurerAdapter { 

    @Autowired 
    protected UserDetailsManager userManager; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(preAuthenticationAuthenticationProvider()) 
       .userDetailsService(userManager).passwordEncoder(PASSWORD_ENCODER); 
    } 

    @Bean 
    @Override 
    protected AuthenticationManager authenticationManager() throws Exception { 
     return super.authenticationManager(); 
    } 

    @Bean 
    protected AuthenticationProvider preAuthenticationAuthenticationProvider() { 
     PreAuthenticatedAuthenticationProvider authenticationProvider = 
       new PreAuthenticatedAuthenticationProvider(); 
     UserDetailsByNameServiceWrapper userDetailsWrapper = new UserDetailsByNameServiceWrapper(userManager); 
     authenticationProvider.setPreAuthenticatedUserDetailsService(userDetailsWrapper); 
     return authenticationProvider; 
    } 

} 
+0

奇妙な場所の意味を説明できますか?認証サーバーとリソースサーバーをどのように構成しましたか? – aksappy

+0

スタックトレースからいくつかの行で質問を更新しました。 –

+0

認証プロバイダが認証マネージャに正しく接続されていないようです。アノテーションベースの設定やXMLを使用していますか?リソースサーバーのセキュリティ構成を共有できますか? – aksappy

答えて

0

私が行方不明になった何されAuthorizationServiceTokenServicesはとResourceServerTokenServices。これらのインタフェースは両方ともSpringのDefaultTokenServicesによって実装されています。認証サーバの設定(AuthorizationServiceConfigurerAdapter)で

@Bean 
public DefaultTokenServices tokenServices() { 
    DefaultTokenServices tokenServices = new DefaultTokenServices(); 
    tokenServices.setTokenStore(tokenStore()); 
    tokenServices.setAuthenticationManager(authenticationManager); 
    return tokenServices; 
} 

、私は次のセットアップがあります。リソースのサーバ設定(ResourceServerConfigurerAdapter)で

@Override 
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
    endpoints.tokenServices(tokenServices()).authenticationManager(authenticationManager); 
} 

を:すべてのこれらのコンポーネントで

@Autowired 
private DefaultTokenServices tokenServices; 

@Override 
public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
    resources.tokenServices(tokenServices); 
} 

、私のアプリは、PreAuthenticationAuthenticationProvider beanが定義されていなくても動作します。

関連する問題