spring boot用户和session mongo

时间:2017-01-27 09:10:48

标签: java spring mongodb spring-mvc session

我正在尝试配置spring boot以将mongoDB用于商店用户和会话。我可以在不同的项目中存储用户和会话,当我将它们放在同一个项目中时,SessionRepositoryFilter的 Autowired 会失败。

这里的错误:

27/01/17 09:08:32 INFO  [web.context.ContextLoader] Root WebApplicationContext: initialization completed in 8576 ms
27/01/17 09:08:33 ERROR [embedded.tomcat.TomcatStarter] Error starting Tomcat context: org.springframework.beans.factory.BeanCreationException
27/01/17 09:08:33 WARN  [context.embedded.AnnotationConfigEmbeddedWebApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
27/01/17 09:08:33 ERROR [springframework.boot.SpringApplication] Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at es.indra.itec.sis.srv.SisServerRunner.main(SisServerRunner.java:53)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:99)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:76)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:457)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:168)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:160)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 8 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration.WebSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.session.web.http.SessionRepositoryFilter es.indra.itec.sis.security.config.SecurityConfiguration$WebSecurityConfiguration.sessionSessionRepositoryFilter; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionRepositoryFilter': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:233)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:181)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:176)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:158)
    at org.springframework.boot.context.embedded.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:79)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getServletContextInitializerBeans(EmbeddedWebApplicationContext.java:237)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:224)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:85)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:209)
    at org.springframework.boot.context.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:55)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.session.web.http.SessionRepositoryFilter es.indra.itec.sis.security.config.SecurityConfiguration$WebSecurityConfiguration.sessionSessionRepositoryFilter; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionRepositoryFilter': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 34 more
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionRepositoryFilter': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 36 more

我不知道如何解决这个问题:

引起:org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为'sessionRepositoryFilter'的bean时出错:当前正在创建请求的bean:是否存在无法解析的循环引用?

配置文件是:

@Configuration
public class SecurityConfiguration {

    /**
     * Web Security for managing authentication.
     */
    @Configuration
    @EnableWebMvcSecurity
    protected static class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

        /**
         * User Service for authentication.
         */
        @Autowired
        private CustomUserDetailsService customUserService;

        @Autowired
        private SessionRepositoryFilter<ExpiringSession> sessionSessionRepositoryFilter;

        @Bean
        public HttpSessionStrategy httpSessionStrategy() {
            return new CookieHttpSessionStrategy();
        }

        @Bean
        public SessionRepositoryFilter<ExpiringSession> sessionRepositoryFilter(
                SessionRepository<ExpiringSession> sessionRepository,
                HttpSessionStrategy httpSessionStrategy
        ) {
            SessionRepositoryFilter<ExpiringSession> sessionRepositoryFilter = new SessionRepositoryFilter<>(sessionRepository);
            sessionRepositoryFilter.setHttpSessionStrategy(httpSessionStrategy);
            this.sessionSessionRepositoryFilter = sessionRepositoryFilter;
            return sessionRepositoryFilter;
        }

        @Bean
        public SessionRepository<ExpiringSession> sessionRepository() {
            return new MongoSessionRepository(10);
        }

        @Override
        @Bean
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }

        @Override
        protected final void configure(final AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(customUserService).passwordEncoder(SecurityConstants.PASSWORD_ENCODER);
        }

        @Override
        public void configure(WebSecurity webSecurity) throws Exception {
            // ExtJS For common server deployment
            webSecurity.ignoring().antMatchers("/");
            webSecurity.ignoring().antMatchers("/index.html");
            webSecurity.ignoring().antMatchers("/cache.appcache");
            webSecurity.ignoring().antMatchers("/classic.json*");
            webSecurity.ignoring().antMatchers("/resources/**");
            webSecurity.ignoring().antMatchers("/classic/**");
            webSecurity.ignoring().antMatchers("/sisfiles/**");

            // Websocket security
            webSecurity.ignoring().antMatchers("/message");
        }

        @Override
        public final void configure(final HttpSecurity http) throws Exception {
            // CSRF disabled because we only use our own services (necesary for
            // third parties)
            http.csrf().disable();

            http
            .logout().permitAll().and()
            .formLogin().permitAll().and()
            .addFilterBefore(this.sessionSessionRepositoryFilter, ChannelProcessingFilter.class);
            // Anyone can request for a security token
            http.authorizeRequests().antMatchers("/oauth/token").anonymous();

            //http.authorizeRequests().anyRequest().fullyAuthenticated();

        }

    }
        /**
         * Oauth2 Configuration.
         */
        @Configuration
        @EnableAuthorizationServer
        protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

            /**
             * Authentication Provider.
             */
            @Autowired
            @Qualifier("authenticationManagerBean")
            private AuthenticationManager authenticationManager;

            /**
             * User Service for authentication.
             */
            @Autowired
            private CustomUserDetailsService customUserService;

            /**
             * We need to override the Spring Security service for users,
             * because we want to use our own custom user service (connected to
             * BBDD).
             *
             * @return UserDetailsService Returns an instance of our custom
             *         service
             */
            @Bean
            public UserDetailsService userDetailsService() {
                return customUserService;
            }

            /**
             * Client Service for authentication.
             */
            @Autowired
            private IClientOAuth2DetailsService customClientDetailsService;

            /**
             * We need to override the Spring Security service for clients,
             * because we want to use our own custom client service (in memory,
             * but it migth goes to BBDD).
             *
             * @return ClientDetailsService Returns an instance of our custom
             *         service
             * @throws Exception
             *             Error
             */
            @Bean
            public ClientDetailsService clientDetailsService() throws Exception {
                return customClientDetailsService;
            }

            @Override
            public final void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
                endpoints.authenticationManager(authenticationManager);
            }

            /**
             * We need to set our custom client service for OAuth2
             * Configuration.
             */
            @Override
            public final void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
                clients.withClientDetails(clientDetailsService());
            }
        }
}

谢谢;)

1 个答案:

答案 0 :(得分:0)

在代码的这一部分中删除对this.sessionSessionRepositoryFilter的赋值,因为Spring会自动为你分配这个。

    @Bean
    public SessionRepositoryFilter<ExpiringSession> sessionRepositoryFilter(
            SessionRepository<ExpiringSession> sessionRepository,
            HttpSessionStrategy httpSessionStrategy
    ) {
        SessionRepositoryFilter<ExpiringSession> sessionRepositoryFilter = new SessionRepositoryFilter<>(sessionRepository);
        sessionRepositoryFilter.setHttpSessionStrategy(httpSessionStrategy);
        this.sessionSessionRepositoryFilter = sessionRepositoryFilter; //Remove this.
        return sessionRepositoryFilter;
    }