Configuring JWT Token Services in axon

Hi all
I’m trying to configure spring security with OAuth and JWT tokens.Here’s a config of in the resourse server:

@Override
public void configure(ResourceServerSecurityConfigurer config) {
    config.tokenServices(tokenServices());
}

@Bean
@Qualifier("tokenStore")
public TokenStore tokenStore() {
    return new JwtTokenStore(accessTokenConverter());
}

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    Resource resource = new ClassPathResource("public.txt");
    String publicKey = null;
    try {
        publicKey = IOUtils.toString(new FileReader(resource.getFile()));
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    converter.setVerifierKey(publicKey);
    return converter;
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    return defaultTokenServices;
}

When i run the application, I get the following error:

`

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.provider.token.DefaultTokenServices]: Factory method ‘tokenServices’ threw exception; nested exception is java.lang.IllegalStateException: @Bean method ResourceServerConfiguration.tokenStore called as a bean reference for type [org.springframework.security.oauth2.provider.token.TokenStore] but overridden by non-compatible bean instance of type [org.axonframework.eventhandling.tokenstore.jpa.JpaTokenStore]. Overriding bean of same name declared in: class path resource [org/axonframework/boot/autoconfig/JpaAutoConfiguration.class]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
… 34 common frames omitted
Caused by: java.lang.IllegalStateException: @Bean method ResourceServerConfiguration.tokenStore called as a bean reference for type [org.springframework.security.oauth2.provider.token.TokenStore] but overridden by non-compatible bean instance of type [org.axonframework.eventhandling.tokenstore.jpa.JpaTokenStore]. Overriding bean of same name declared in: class path resource [org/axonframework/boot/autoconfig/JpaAutoConfiguration.class]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.obtainBeanInstanceFromFactory(ConfigurationClassEnhancer.java:402) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.softech.couponloyalty.config.ResourceServerConfiguration$$EnhancerBySpringCGLIB$$5ca61b9d.tokenStore() ~[classes/:na]
at org.softech.couponloyalty.config.ResourceServerConfiguration.tokenServices(ResourceServerConfiguration.java:86) ~[classes/:na]
at org.softech.couponloyalty.config.ResourceServerConfiguration$$EnhancerBySpringCGLIB$$5ca61b9d.CGLIB$tokenServices$2() ~[classes/:na]
at org.softech.couponloyalty.config.ResourceServerConfiguration$$EnhancerBySpringCGLIB$$5ca61b9d$$FastClassBySpringCGLIB$$99185459.invoke() ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.softech.couponloyalty.config.ResourceServerConfiguration$$EnhancerBySpringCGLIB$$5ca61b9d.tokenServices() ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
… 35 common frames omitted

`

My tokenstore bean implementation is being overriden by axon’s tokenstore. How do I bypass that?

Cheer
Harvey

If you rename your tokenStore() method (the one returning the JWT token), that should resolve the issue. Alternatively, you can specify a beanName in the @Bean annotation on that method.

Cheers,

Allard

Thanks Allard.