1

私は認可にspring oauth2を使用しています。私は、トークンをリフレッシュしようとするとエラーが発生することを発見しました:UserDetailsS​​erviceが必要です(興味深いことに、このエラーはUNIXマシンとWindows上でのみ発生します)。私はspring oauth2 version 2.0.7を使用しています。私は問題をローカライズすることができました。何らかの理由でDefaultTokenServiceのAuthenticationManagerが空ではなく、ユーザーがまだ存在するかどうかを確認するためにユーザーを認証しようとします。私はそれがいくつかの春のセキュリティ対春のoauth2構成の問題のために初期化されると思います。私はカスタムUserDetailsS​​erviceを使用していないので、この時点でユーザーを認証すべきではありません。しかし、私はそれをデバッグするとき、WebSecurityConfigurerAdapterからのものを使用しようとし、このエラーになることがわかります。カスタムダミーのUserDetailsS​​erviceを提供しても、そのカスタムダミーを使用していませんが、もう1つ、つまりnullを使用しようとします。私はここに何かを逃していますかなぜこれが起こっているのか分からないのですか?春のセキュリティを使用してリフレッシュ・トークン呼び出しが失敗しました。エラーのあるoauth2:UserDetailsS​​erviceが必要です

は、ここに私のOAuth2の設定ここで

@Configuration 
@EnableAuthorizationServer 
public class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private MySpringTokenStore tokenStore; 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Autowired 
    private MyClientDetailsServiceImpl clientDetailsService; 

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

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.withClientDetails(clientDetailsService); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 
     security.allowFormAuthenticationForClients(); 
    } 

    @Bean 
    public TokenStore tokenStore() { 
     return new InMemoryTokenStore(); 
    } 
} 

私の春のセキュリティ設定

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http 
     .authorizeRequests() 
      .antMatchers("/myRest/events/**", "/events/**", "/events", "/myRest/events").permitAll() 
      .antMatchers("/login.jsp", "/login").permitAll() 
     .and() 
      .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize")).disable() 
      .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/myRest/events")).disable() 
     .sessionManagement().sessionFixation().none(); 
     // @formatter:on 
    } 


    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/index*", "/myRest/events/**", "/events/**", "/myRest/events", "/events", "/swagger/**", "/kibana/**", 
      "/elastic/**", "/version/**", "/api-docs/**", "/js/**", "/oauth/uncache_approvals", "/oauth/cache_approvals"); 
    } 
} 

答えて

3

認証サーバのエンドポイントがUserDetailsServiceを必要です。以下のようなあなたのOAuth2Configクラスのconfigureユーザー詳細サービスでは:

@Autowired 
private UserDetailsService userDetailsService; 

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

またWebSecurityConfigurerAdapterでそれを設定することができます。

@Autowired 
private AuthorizationServerEndpointsConfiguration endpoints; 

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

    if (!endpoints.getEndpointsConfigurer().isUserDetailsServiceOverride()) { 
     UserDetailsService userDetailsService = http.getSharedObject(UserDetailsService.class); 
     endpoints.getEndpointsConfigurer().userDetailsService(userDetailsService); 
    } 

    // @formatter:off 
    http 
    .authorizeRequests() 
     .antMatchers("/myRest/events/**", "/events/**", "/events", "/myRest/events").permitAll() 
     .antMatchers("/login.jsp", "/login").permitAll() 
    .and() 
     .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize")).disable() 
     .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/myRest/events")).disable() 
    .sessionManagement().sessionFixation().none(); 
    // @formatter:on 
} 
+0

いいえ、それは真実ではありません。 Oauth2構成にはそのサービスは必要ありませんが、空のままにすることもできます。私はそのサービスの実装を提供しようとしましたが、同じ例外が投げられました。私はすでにこの問題を解決しました。問題は構成の順序(いくつかのオブジェクトがあるため)でした。 OAuth2構成の順序を100に、Webセキュリティ構成を101に設定しました。このようにして動作します。 –

関連する問題