SpringSession核心配置文件 实现Session共享 (不是单点登录(不同根域名之间session共享)这个比较复杂是使用Spring的安全框架实现的)

1.在web.xml中加一个过滤器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>1_servlet8</display-name>

  <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>
  
  <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>
 



2.Spring配置文件:

```cpp
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="classpath:applicationContext-springSession.xml"/>
</beans>

3.applicationContext-springSession.xml配置文件

<?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">
    <!-- 启动Spring的注解  ,component-scan 包路径扫描用于扫描到我们自定的带有注解的,component-scan这个标签的作用包含annotation-config的功能 -->
    <context:annotation-config></context:annotation-config>

    <!-- 启动SpringSession, 并将Session存入Redis中 -->
    <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <!-- 设置Cookie的存放规则-->
        <property name="cookieSerializer"  ref="defaultCookieSerializer"/>

    </bean>

    <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
        <!-- 设置Cookie的访问路径 用于实现同一个域名下不同的项目中的Session共享-->
        <property name="cookiePath" value="/"/>
        <!-- 设置Cookie的域名 ,用于实现同根域名下 不同的二级子域名的Session共享问题
            注意:在Tomcat8(包括)以后域名设置不需要添加 . 例如myweb.com
                  在Tomcat8(不包括)以前域名设置需要添加 . 例如 .myweb.com
         -->
        <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.31.128"/>
        <property name="port" value="6379"/>
        <property name="password" value="123456"/>
    </bean>

</beans>

面试 总结: 我们怎么实现Session共享的?
1>首先: 我们采用的是SpringSession技术,是把session存放到Redis中;
2>我们设置一下存放规则: 设置
RedisHttpSessionConfiguration 类的属性 cookieSerializer ,其属性是一个对象类型,所以传引用 ref 值:defaultCookieSerializer
3.在 DefaultCookieSerializer这个类
对其属性进行设置 :1 >根据路径设值: name=“cookiePath”
2>根据域名设置属性: name=“domainName”;

发布了388 篇原创文章 · 获赞 40 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_30347133/article/details/105025299