Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring security oauth2 ClassCastException configuring DefaultTokenServices

I'm trying to run an sample application using spring boot and spring security oauth with a configured JdbcTokenStore and a DefaultTokenServices with infinite lifetime access tokens.

Running this application with gradle bootRun, the application won't start and throws an "Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy51 cannot be cast to org.springframework.security.oauth2.provider.token.DefaultTokenServices"

Why is there a proxy wrapped around the DefaultTokenServices bean?

A strange thing is - running the application with the InMemoryTokenStore ... everything works fine (See inmemory branch).

Source Code https://github.com/grafjo/oauth_demo/blob/master/src/main/java/demo/AuthorizationServerConfiguration.java

Full Trace: http://pastebin.com/SUcwz4S5

like image 593
joo Avatar asked Apr 24 '15 10:04

joo


1 Answers

A quick peek inside DefaultTokenService reveals it is annotated with @Transactional. Spring is going to wrap it in a proxy to service the transactions - as a result you need to be interacting with the class by its Interface.

For your tokenService bean:

@Bean
public DefaultTokenServices tokenServices() {
    final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setAccessTokenValiditySeconds(-1);
    defaultTokenServices.setTokenStore(tokenStore());
    return defaultTokenServices;
}

try changing it to this:

@Bean
public AuthorizationServerTokenServices tokenServices() {
    final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setAccessTokenValiditySeconds(-1);
    defaultTokenServices.setTokenStore(tokenStore());
    return defaultTokenServices;
}
like image 133
psuthe Avatar answered Sep 27 '22 19:09

psuthe