NPE AuthorizationServerEndpointsConfiguration $ TokenRegistrar postProcessBeanFactory

时间:2014-04-21 22:53:55

标签: java spring spring-security spring-security-oauth2

我有一个使用spring-security-oauth-2.0.0-M2构建的应用程序,现在我正在尝试将其升级到2.0.0.RC1。示例配置已发生显着变化,现在使用java config。

我已将配置转换为java配置,但我遇到了这个错误。

  

2014年4月21日下午5:32:43 org.apache.catalina.core.ApplicationContext   log INFO:初始化Spring root WebApplicationContext 17:32:44.703   [localhost-startStop-1]错误o.s.web.context.ContextLoader - 上下文   初始化失败java.lang.NullPointerException:null at   org.springframework.security.oauth2.config.annotat   ion.web.configuration.AuthorizationServerEndpoints   配置$ TokenStoreRegistrar.postProcessBeanF   actory(AuthorizationServerEndpointsConfiguration.j ava:179)   〜[spring-security-oauth2-2.0.0.RC1.jar:na] at   org.springframework.context.support.AbstractApplic   ationContext.invokeBeanFactoryPostProcessors(摘要   actApplicationContext.java:694)   〜[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE] at   org.springframework.context.support.AbstractApplic   ationContext.invokeBeanFactoryPostProcessors(摘要   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]   在org.apache.catalina.core.StandardContext.startInte   rnal(StandardContext.java:5434)[tomcat-embed-core-7.0.47.jar:7.0.47]   在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。   调用(ContainerBase.java:1559)[tomcat-embed-core-7.0.47.jar:7.0.47] at   org.apache.catalina.core.ContainerBase $ StartChild。   调用(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] 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]

我的配置与示例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");
    }

}

正如您所看到的,我正在检查configure(AuthorizationServerEndpointsConfigurer端点)方法上的空值,并且一切看起来都很好,但不知何时,AuthorizationServerEndpointsConfiguration $ TokenReg istrar中的注册表永远不会设置。

(取自春季安全oauth代码)

@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;
    }

}

1 个答案:

答案 0 :(得分:1)

似乎将在下一版本中修复,因为它使用上一个快照版本(2.0.0.BUILD-SNAPSHOT< 2014-04-19>)。