【问题标题】:Listening for session-events not working after update to SpringBoot2在更新到 SpringBoot2 后监听会话事件不起作用
【发布时间】:2019-03-21 15:30:29
【问题描述】:

我有一个项目使用带有弹簧靴的弹簧。它具有一些自定义安全机制以及监听会话创建和 会话破坏事件:

@WebListener
public class MySessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("==== Session is created ====");

        //sets a custom session timeout
        httpSessionEvent.getSession().setMaxInactiveInterval(10);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("==== Session is destroyed ====");
    }
}

通过以下方式实例化:

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import sample.session.MySessionListener;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class WebAppInitializer implements ServletContextInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //only https allowed
        //servletContext.getSessionCookieConfig().setSecure(true);

        //add session listener for example to influence session-timeout
        servletContext.addListener(new MySessionListener());
    }
}

与:

@SpringBootApplication
public class SpringSecApp {
    public static void main(String[] args) {
        SpringApplication.run(new Class[] { SpringSecApp.class, WebAppInitializer.class }, args);
    }
}

现在我添加了一些配置以使 spring 将会话信息写入 MySql DB。为了实现这一点,我必须将我的 pom.xml 从 Spring-Boot 1.5.x 更新到 2.1.3。

现在这个会话信息写入工作正常,但我认识到会话事件不再处理;我想原因在于依赖关系,因为我没有更改上面发布的有关会话侦听的相关代码片段,但我找不到它。如果有任何帮助,我将不胜感激!我将包括 2 个 pom 版本;首先是当前的一个,然后是最后一个提交中的一个,我可以在其中确认会话事件处理工作正常。 当前版本:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>bous.philipp</groupId>
    <artifactId>spring-security-custom-auth-userdetails-REST</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>

            <!--Workaround for broken openjdk-version-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

以前的版本:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>bous.philipp</groupId>
    <artifactId>spring-security-custom-auth-userdetails-REST</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <version>4.2.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!--Workaround for broken openjdk-version-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

编辑:我尝试了 MyTwoCents 方法,它没有改变任何东西;此外,似乎根本没有执行 onStartup-方法(从该方法打印到标准输出不显示)。

EDIT2:我可以确认正在调用 ServletContextInitializer 的 onStartup-方法,但 MySessionListenersessionCreatedsessionDestroyed 根本没有被调用。

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    也许你需要这样做

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.addListener(new MySessionListener());
    
            super.onStartup(servletContext);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    
    }
    

    另附注from Doc

    用于以编程方式配置 Servlet 3.0+ 上下文的接口。 与 WebApplicationInitializer 不同,实现这个的类 接口(并且不实现 WebApplicationInitializer)不会 由 SpringServletContainerInitializer 检测到,因此不会被 由 Servlet 容器自动引导。

    【讨论】:

    • 抱歉回复晚了;不幸的是,您的方法没有改变任何东西。仍然没有错误或明显的异常......所以我感谢任何输入。它甚至似乎没有执行 onStartup 方法。
    • 我在javadevjournal.com/spring-boot/spring-boot-session-listener 找到了类似的解决方案,与问题代码的不同之处在于额外的@ServletComponentScan。也许改用这个注释再试一次。
    【解决方案2】:

    我不确定这是否是对这个问题的正确处理,但事实证明 这个问题本身是不合适的,因为根本原因没有转换 从引导 1 到引导 2,而是我切换的原因,那就是使用 一个 JDBC 会话存储库。

    在这种情况下我们可以参考:

    https://docs.spring.io/spring-session/docs/current/reference/html5/#api-jdbcoperationssessionrepository

    告诉我们:

    请注意,此实现不支持会话事件的发布。

    所以这个问题并不真正适合这个问题,但我想为可能来自类似方向的人发布这个问题,即使它不是一个可接受的答案。 这也意味着将标题更改为“如何解决......” 宁愿画一个我想避免的严重可追溯的后历史。我将提出一个更合适的问题。

    【讨论】:

      【解决方案3】:

      请看看这是否有效

      public class SessionExpirationListener implements ApplicationListener<SessionDestroyedEvent> {
      
          @Override
          public void onApplicationEvent(SessionDestroyedEvent event) {
      //your code
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-06
        • 2020-05-08
        • 2018-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多