Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StackOverflowError in spring oauth2 with custom ClientDetailsService

I made my own implementation of ClientDetailsService:

@Service
public class JpaClientDetailsService implements ClientDetailsService {
    @Autowired
    private ClientRepository clientRepositoy;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        ClientDetails client = clientRepositoy.findOne(clientId);
        if (client == null) {
            throw new ClientRegistrationException(String.format("Client with id %s not found", clientId));
        }
        return client;
    }
}

ClientRepository is a standard JpaRepository.

I configured an AuthorizationServerConfigurerAdapter like this:

@Configuration
@EnableAuthorizationServer
@EnableResourceServer
public class OAuth2ServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private ClientDetailsService clientDetailsService;

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

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

But when I go to http://localhost:9999/oauth/authorize?response_type=code&client_id=lipton, I get a

java.lang.StackOverflowError: null. Spring loops on com.sun.proxy.$Proxy81.loadClientByClientId(Unknown Source).

I don't understand why.

like image 364
Arnaud Avatar asked Aug 04 '15 00:08

Arnaud


2 Answers

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 {
like image 96
Arnaud Avatar answered Jan 04 '23 08:01

Arnaud


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
like image 21
IgorekPotworek Avatar answered Jan 04 '23 10:01

IgorekPotworek