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

574 篇文章 4 订阅
272 篇文章 1 订阅

pom.xml

<?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>com.bjpowernode.springsession</groupId>
    <artifactId>01-springsession</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Spring Session 依赖start -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>
        <!-- Spring Session 依赖end -->

        <!-- Spring session redis 依赖start -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>
        <!-- Spring session redis 依赖end -->

        <!-- spring-data-redis依赖的JAR配置start -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.8.RELEASE</version>
        </dependency>
        <!-- spring-data-redis依赖的JAR配置end -->

        <!-- jedis依赖的JAR配置start -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!-- jedis依赖的JAR配置end -->

        <!-- spring web模块依赖 start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <!-- spring web模块依赖end -->

        <!-- servlet依赖的jar包start -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- servlet依赖的jar包start -->

        <!-- jsp依赖jar包start -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!-- jsp依赖jar包end -->

        <!--jstl标签依赖的jar包start -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--jstl标签依赖的jar包end -->

    </dependencies>
</project>

创建两个 Servlet处理请求:

package com.bjpowernode.springsession.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName:SetSession
 * Package:com.bjpowernode.springsession.servlet
 * Description:
 *
 * @date:2018/9/29 11:22
 * @author:bjpowernode.com
 */
@WebServlet("/set")
public class SetSession extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("sessionKey","Session Data!");
        resp.getWriter().print("Session Set OK!");
    }
}

package com.bjpowernode.springsession.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName:GetSession
 * Package:com.bjpowernode.springsession.servlet
 * Description:
 *
 * @date:2018/9/29 11:24
 * @author:bjpowernode.com
 */
@WebServlet("/get")
public class GetSession extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String str= (String) req.getSession().getAttribute("sessionKey");

        resp.getWriter().println(str);
    }
}
 



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

 

```cpp
<?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配置文件:

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

简单配置 1:(适用于同域名、同项目session共享)

<?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">
    </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>

简单配置 2:(适用于同域名、不同项目session共享)

<?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="/"/>
    </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>

路径都变成了 “/ ”
在这里插入图片描述

复杂配置:(适用于根域名相同,不同二级子域名场景)
在这里插入图片描述
必须是 127.0.0.1
在这里插入图片描述

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>

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

使用SpringSession技术做登录时候就不要设置 nginx的 ip_hash负载均衡策略了,这种策略基本不用,可以使用轮询策略。

单点登录案例

淘宝 、天猫

比如在淘宝上登录了,就不用再天猫上再登了。
使用单点登录技术: SSO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值