StackOverflowError in spring oauth2 with custom ClientDetailsService

荒凉一梦 提交于 2019-12-03 00:53:05

I do not understand why, but if I inject my bean directly instead of injecting the interface, it works :

public class OAuth2ServerConfig extends AuthorizationServerConfigurerAdapter {
...
  @Autowired
  private JpaClientDetailsService clientDetailsService;
...

it also works if I annotate my service with @Primary annotation:

@Service
@Primary
public class JpaClientDetailsService implements ClientDetailsService {

I had similar problem. Finally I resolved bug when I gave my clientDetailsService another name i.e. myClientDetailsService and then injected this by name in AuthorizationServerConfig class:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Resource(name = "myClientDetailsService")
    private ClientDetailsService clientDetailsService;
...

I think that if my own implementation of ClientDetailsService wasn't yet created Spring inject into AuthorizationServerConfig some kind of proxy.

So, if you want to resolve this kind of bug you must be sure that Spring inject proper ClientDetailsService in AuthorizationServerConfig. You can achieve this if you:

  1. give spring information about preference of your own ClientDetailsService (Arnaud answer), or
  2. inject this service by name

What ended up working for me was adding the ClientDetailsService @Bean to the @EnableAuthorizationServer class:

@Configuration @EnableAuthorizationServer public class AuthorizationServerConfiguration implements AuthorizationServerConfigurer { ... @Autowired ClientDetailsService clientDetailsService; ... @Bean public ClientDetailsService clientDetailsService() { return new CustomClientDetailsServiceImpl(); } ... }

I would say that the best solution is to be explicit - if you are autowiring a clientDetailsService - then say so.

@Autowired
ClientDetailsService myClientDetailsService;

@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.setClientDetailsService(myClientDetailsService);
       ....
}

However, My problem was slightly different and the above solutions did not work. Here are the conditions that created it. I created a CustomTokenEndpointAuthenticationFilter - which required an instance of OAuth2RequestFactory. I created my instance of OAuth2RequestFactory with a call to endpoints.getOAuth2RequestFactory(); before the clientDetailsService had been set.

If The OAuth2RequestFactory gets created this way, DefaultOAuth2RequestFactory gets created with a default clientDetailsService, so even if you set the clientDetailsService later explicitly in the AuthorizationServerEndpointsConfigurer it will not be in the OAuth2RequestFactory used by your custom Filter.

So in sum in this edge case

public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
     oAuth2RequestFactory = endpoints.getOAuth2RequestFactory();
     endpoints.setClientDetailsService(myClientDetailsService);
    ...
    }

wont work but

public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.setClientDetailsService(myClientDetailsService);
 oAuth2RequestFactory = endpoints.getOAuth2RequestFactory();
...
}

will.

Another way to be sure is to create your own OAuth2RequestFatory

  oauth2RequestFactory = new DefaultTokenRequestFactory(myClientDetailsService);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!