简体   繁体   中英

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM