Question

I have an application built with spring-security-oauth-2.0.0-M2, now I'm trying to upgrade this to 2.0.0.RC1. The samples configuration has changed significantly and now using java config.

I have converted the configuration to java config but I got stuck at this error.

Apr 21, 2014 5:32:43 PM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring root WebApplicationContext 17:32:44.703 [localhost-startStop-1] ERROR o.s.web.context.ContextLoader - Context initialization failed java.lang.NullPointerException: null at org.springframework.security.oauth2.config.annotat ion.web.configuration.AuthorizationServerEndpoints Configuration$TokenStoreRegistrar.postProcessBeanF actory(AuthorizationServerEndpointsConfiguration.j ava:179) ~[spring-security-oauth2-2.0.0.RC1.jar:na] at org.springframework.context.support.AbstractApplic ationContext.invokeBeanFactoryPostProcessors(Abstr actApplicationContext.java:694) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.context.support.AbstractApplic ationContext.invokeBeanFactoryPostProcessors(Abstr actApplicationContext.java:684) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:461) ~[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.web.context.ContextLoader.conf igureAndRefreshWebApplicationContext(ContextLoader .java:410) ~[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:306) ~[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 112) [spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:4937) [tomcat-embed-core-7.0.47.jar:7.0.47] at org.apache.catalina.core.StandardContext.startInte rnal(StandardContext.java:5434) [tomcat-embed-core-7.0.47.jar:7.0.47] at org.apache.catalina.util.LifecycleBase.start(Lifec ycleBase.java:150) [tomcat-embed-core-7.0.47.jar:7.0.47] at org.apache.catalina.core.ContainerBase$StartChild. call(ContainerBase.java:1559) [tomcat-embed-core-7.0.47.jar:7.0.47] at org.apache.catalina.core.ContainerBase$StartChild. call(ContainerBase.java:1549) [tomcat-embed-core-7.0.47.jar:7.0.47] at java.util.concurrent.FutureTask.run(FutureTask.jav a:262) [na:1.7.0_45] at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1145) [na:1.7.0_45] at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:615) [na:1.7.0_45] at java.lang.Thread.run(Thread.java:744) [na:1.7.0_45]

My configuration is mostly similar to sample sprklr config

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private TokenStore tokenStore;
    @Autowired
    private UserApprovalHandler userApprovalHandler;
    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory().withClient("some-client")
                .authorizedGrantTypes("authorization_code", "password", "client_credentials")
                .authorities("ROLE_USER", "ROLE_CLIENT")
                .scopes("play", "trust")
                .secret("xyz")
                .accessTokenValiditySeconds(6000)
                .and()
                .withClient("some-other-client")
                .authorizedGrantTypes("implicit")
                .authorities("ROLE_USER", "ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                .scopes("play", "trust")
                .autoApprove(true)
                .accessTokenValiditySeconds(6000);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        if (tokenStore == null) {
            throw new IllegalStateException("Token store is null");
        }
        if (userApprovalHandler == null) {
            thrownew IllegalStateException ("User approval handler is null");
        } if (authenticationManager == null) {
            throw new IllegalStateException("Auth manager is null");
        }
        endpoints
                .userApprovalHandler(userApprovalHandler)
                .authenticationManager(authenticationManager)
                .tokenStore(tokenStore);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.realm("myapp/client");
    }

}

As you can see I'm checking for nulls on configure(AuthorizationServerEndpointsConfigurer endpoints) method and all seems fine but somehow the registry in AuthorizationServerEndpointsConfiguration$TokenReg istrar is never set.

(Taken from spring security oauth code)

@Configuration
protected static class TokenStoreRegistrar implements BeanDefinitionRegistryPostProcessor {

    private BeanDefinitionRegistry registry;

    // Use a BeanFactoryPostProcessor to register a bean definition for a TokenStore in a safe way (without
    // pre-empting a bean specified by the user)
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (!registry.containsBeanDefinition(TOKEN_STORE_BEAN_NAME)) {
            registry.registerBeanDefinition(TOKEN_STORE_BEAN_NAME, new RootBeanDefinition(InMemoryTokenStore.class));
        }
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        this.registry = registry;
    }

}
Was it helpful?

Solution

It seems that will be fixed in next release because it works using the last snapshot version (2.0.0.BUILD-SNAPSHOT <2014-04-19>).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top