当前位置: 首页 > 知识库问答 >
问题:

Spring Boot用户和会话mongo

云鸿达
2023-03-14

我正在尝试将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
@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个答案

缑智敏
2023-03-14

删除这部分代码中对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;
    }
 类似资料:
  • 我正在使用Spring Boot、Spring Security性和spring会话(redis)构建spring REST web应用程序。我正在使用SpringCloud和zuul代理按照网关模式构建一个云应用程序。在这个模式中,我使用spring会话来管理redis中的HttpSession,并使用它来授权我的资源服务器上的请求。当执行更改会话权限的操作时,我希望更新该对象,以便用户不必注销

  • 我有一个cordova应用程序,我有一个登录页面和一个登录用户的内页。每当用户会话消失时,它就会请求登录信息。如何防止ths cordova应用程序在重启移动应用程序时删除会话uppon重启或恢复cookie和会话信息?所以登录屏幕不会每次都出现?

  • 是时候承认了:我们有意的避开了 web 开发中极其重要的方面。到目前为止,我们都在假定,网站流量是大量的匿名用户带来的。 这当然不对,浏览器的背后都是活生生的人(至少某些时候是)。我们忽略了一件重要的事情:互联网服务于人而不是机器。要开发一个真正令人心动的网站,我们必须面对浏览器后面活生生的人。 很不幸,这并不容易。HTTP 被设计为”无状态”,每次请求都处于相同的空间中。在一次请求和下一次请求之

  • 是时候承认了: 我们有意的避开了Web开发中极其重要的方面。 到目前为止,我们都在假定,网站流量是大量的匿名用户带来的。 这当然不对。 浏览器的背后都是活生生的人(至少某些时候是)。 这忽略了重要的一点: 互联网服务于人而不是机器。 要开发一个真正令人心动的网站,我们必须面对浏览器后面活生生的人。 很不幸,这并不容易。 HTTP被设计为”无状态”,每次请求都处于相同的空间中。 在一次请求和下一次请

  • 我在Codeigniter框架中面临一个新问题。她是我最讨厌的人 这是控制器 模型是

  • 问题内容: 如果我想使用Web应用程序跟踪与每个客户端的对话状态,那么哪个更好的替代方法是使用Session Bean还是HTTP Session? 使用HTTP会话: 使用会话EJB: 在执行ServletContextListener时注册为Web应用程序侦听器: 在JSP中: 同一JSP正文中的其他地方: 在我看来,它们几乎是相同的,主要区别在于UserState实例是在前者中传输的,而在后