RuNsTa
RuNsTa

Reputation: 83

JdbcTokenStore implementation with Spring-Boot and OAuth2

Hej,

I am trying to implement OAuth2 in an Application using Spring-Boot. I am struggling with the implementation of the JdbcTokenStore (did I understood this right, that this is to store the Token in the Database?)

My Code looks like this:

@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    private TokenStore tokenStore = new JdbcTokenStore(dataSource);

....

    @Override
    public void configure(OAuth2AuthorizationServerConfigurer oauthServer) throws Exception {
         oauthServer.tokenStore(tokenStore).authenticationManager(authenticationManager);
    }

Doing this I am getting following error:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [se.....config.OAuth2ServerConfig$OAuth2Config$$EnhancerBySpringCGLIB$$f6b9ba94]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: DataSource required

The database connection itself seems to work, at least I can login with user data (username / password) stored in the database when I use InMemoryTokenStore.

Can anyone tell me what I am doing wrong or recommend sources where good examples are for this issue?

Thanks a lot!

Upvotes: 1

Views: 4146

Answers (1)

Dave Syer
Dave Syer

Reputation: 58094

That's a basic dependency injection problem (nothing fancy and nothing oauth- or Boot- related). You can't initialize a field from an @Autowired (the wiring takes place after initialization). You need to pull that out into a @PostConstruct or a @Bean.

Upvotes: 1

Related Questions