ssm integra springsession para resolver la pérdida de sesiones

archivo web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--固定格式-->
    <filter>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

	<!--springmvc工程可选-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

archivo springSession

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <!--定义一个用于专门配置springSession的bean标签配置-->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="1800"/>
        <!--注入一个cookie序列化规则对象-->
        <!--同域名下 不同项目  或者   同根域名不同二级域名下 才需要加下面cookie序列化-->
        <property name="cookieSerializer" ref="defaultCookieSerializer"/>
    </bean>
<!--同域名下 不同项目  或者   同根域名不同二级域名下 加下面的俩个-->
    <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
        <!--指定springsession中的sessionId存放的位置 存放到域名的根路径下 实现同域名不同项目的session共享-->
        <property name="cookiePath" value="/"/>
        <!--指定springsession中的sessionId存放的位置 存放到跟域名下 即domainName为myweb.com
            适合应用在Nginx的虚拟主机的多城市站点部署上
        -->
        <property name="domainName" value="myweb.com"/>
    </bean>

    <!-- 配置jedis连接工厂,用于连接redis 项目配置过可省略 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="192.168.231.128"/>
        <property name="port" value="6379"/>
        <property name="password" value="123456"/>
    </bean>

</beans>

Introduzca el archivo springSession en el archivo de configuración principal de Spring

Springboot resuelve el problema de no poder sincronizar sesiones y serialización bajo diferentes nombres de dominio

package com.gulimall.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
public class GulimallSessionConfig {
    
    

    /*session的作用域*/
    @Bean
    public CookieSerializer cookieSerializer() {
    
    
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        cookieSerializer.setDomainName("gulimall.com");
        cookieSerializer.setCookieName("GULISESSION");
        return cookieSerializer;
    }

    /*序列化机制*/
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
    
    
        return new GenericJackson2JsonRedisSerializer();
    }
}

Supongo que te gusta

Origin blog.csdn.net/qq_36905956/article/details/105104989
Recomendado
Clasificación