最近在看一些关于spring session 的知识,特做一个笔记记录一下。

在项目中经常会遇到这么一种情况,同一个web项目有时需要部署多份,然后使用nginx实现负载均衡,那么遇到的问题就是,部署多份之后,如何实现一个session的共享功能。此时就可以使用spring session来实现。

参考网址:http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-jdbc-xml.html

1.引入spring session 的jar包依赖。

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.huan.spring</groupId>
<artifactId>spring-session-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>02_spring_session_jdbc</artifactId>
<packaging>war</packaging>
<name>02_spring_session_jdbc Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
</dependency>
</dependencies>
<build>
<finalName>02_spring_session_jdbc</finalName>
</build>
</project>

spring 的版本:4.1.5.RELEASE spring session-jdbc的版本:1.2.0.RELEASE

二、配置spring 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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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-4.1.xsd">
<context:annotation-config />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性driverClassName、 url、user、password -->
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
</bean>
<bean
class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration" />
<bean
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
</beans>

三、配置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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-session.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
</web-app>

四、编写测试代码

1.登录界面(login.jsp)

<body>
<form action="LoginServlet" method="post">
<table>
<tbody>
<tr>
<td>用户名:</td>
<td><input name="loginName" required="required" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" required="required"></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</tfoot>
</table>
</form>
</body>

2.后台的逻辑处理

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = -8455306719565291102L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
request.setCharacterEncoding("UTF-8");
String loginName = request.getParameter("loginName");
String password = request.getParameter("password");
request.getSession().setAttribute("login", true);
request.getSession().setAttribute(loginName, password);
response.sendRedirect(request.getContextPath() + "/show.jsp");
}
}

3.展示界面(show.jsp ---> 没有登录访问次界面,直接跳到登录界面)

<body>
输出session中的值:
<%
if (request.getSession().getAttribute("login") == null) {
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
%>
<%
Enumeration<String> enumeration = request.getSession().getAttributeNames();
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
Object value = request.getSession().getAttribute(key);
out.println(key + " --- " + value);
}
%>
<br>
-----------------
jsessionId:<%=request.getSession().getId() %>
----------------------
<%=request.getRemoteAddr()%>
<%=request.getRemotePort()%>
<%=request.getRemoteHost()%>
<%=request.getRemoteUser()%>
<%=request.getHeader("X-Real-IP")%>
<%=request.getHeader("Host")%>
<%=request.getHeader("X-Forwarded-For")%>
<%=request.getLocalPort()%>
</body>

4.nginx的简单配置 --- 启动ngnix

5.分别启动2个tomcat容器

6.修改其中一个tomcat容器中的show.jsp的值,随便加一个内容,以示区分。

7.页面上访问


 
 8.可以到oracle数据区中进行查询,用到的表

select * from SPRING_SESSION t;
select * from SPRING_SESSION_ATTRIBUTES t;

9.用到的sql语句所在的jar包目录

spring session实现session统一管理(jdbc实现)的更多相关文章

  1. .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

  2. 使用Spring Session做分布式会话管理

    在Web项目开发中,会话管理是一个很重要的部分,用于存储与用户相关的数据.通常是由符合session规范的容器来负责存储管理,也就是一旦容器关闭,重启会导致会话失效.因此打造一个高可用性的系统,必须将 ...

  3. hibernate 管理 Session(单独使用session,不spring)

    Hibernate 本身提供了三个管理 Session 对象的方法 Session 对象的生命周期与本地线程绑定 Session 对象的生命周期与 JTA 事务绑定 Hibernate 托付程序管理 ...

  4. Spring Security 之Session管理配置

    废话不多说,直接上代码.示例如下: 1.   新建Maven项目  session 2.   pom.xml <project xmlns="http://maven.apache.o ...

  5. hibernate 管理 Session(单独使用session,非spring)

    Hibernate 自身提供了三种管理 Session 对象的方法 Session 对象的生命周期与本地线程绑定 Session 对象的生命周期与 JTA 事务绑定 Hibernate 托付程序管理 ...

  6. spring 管理 jdbc 事务

    @Transactional    业务实现类 类名上方--这个类中的方法,执行操作前会打开事务. 默认:RuntimeException 自动回滚, 可以try  catch 的异常,不会滚 方法名 ...

  7. Spring Cloud分布式Session共享实践

    通常情况下,Tomcat.Jetty等Servlet容器,会默认将Session保存在内存中.如果是单个服务器实例的应用,将Session保存在服务器内存中是一个非常好的方案.但是这种方案有一个缺点, ...

  8. spring security控制session

    spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...

  9. Spring Session解决Session共享

    1. 分布式Session共享   在分布式集群部署环境下,使用Session存储用户信息,往往出现Session不能共享问题.   例如:服务集群部署后,分为服务A和服务B,当用户登录时负载到服务A ...

随机推荐

  1. Docker入门之container篇

    启动 启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容 器重新启动. 因为 Docker 的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器. ...

  2. Vue获取Abp VNext Token

    Abp VNext默认没公开访问Token的Api,但有个问题Cookie方式如果是手机或桌面程序不如Token方便 Axios默认是Json方式提交,abp登录需要使用application/x-w ...

  3. Vue Abp vNext用户登录(Cookie)

    因为Abp vNext没找到Vue的模板,网上也没找到相关vNext的例子,只能自己试着写写,asp.net core abp vue都是刚学不久,所以很粗糙也可能有错误的地方,如果您看到请指正,谢谢 ...

  4. redis存取数据sortedSet

    有序比无序的更耗性能 一.存取元素 1.添加元素,value存在则替换score值,不存在则添加: 2.获取某个元素的分数 3. 4.获取一定索引区间元素value值,默认按照对应score升序排序: ...

  5. 《微服务架构设计模式》读书笔记 | 第8章 外部API模式

    目录 前言 1. 外部API的设计难题 1.1 FTGO应用程序的服务及客户端 1.2 FTGO移动客户端API的设计难题 1.3 其他类型客户端API的设计难题与特点 2. API Gateway模 ...

  6. IIS中配置WCF站点

    http://msdn.microsoft.com/zh-cn/library/aa751852.aspx http://blog.csdn.net/hsg77/article/details/389 ...

  7. [AtcoderABC200E]Patisserie

    [AtcoderABC200E]Patisserie 题面翻译 对于一个三元组\((i,j,k)\) 我们对它按如下要求进行升序排序: 第一关键词 \(i + j + k\) 即三者总和 第二关键词 ...

  8. 洛谷P1160——队列安排(双向链表)

    题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进队列,这时队列中只有他一个人: 2.2-N号同学依次入列,编号为i的同学入列方式为:老师 ...

  9. HTML音乐悬浮播放器

    话不多说先上代码 <link rel="stylesheet" href="http://47.102.203.92/css/APlayer.min.css&quo ...

  10. python 金币小游戏

    我最近用python的pygame做了一个金币小游戏 游戏规则:移动挡板接住金币 游戏截图: 代码如下: import pygame.freetype import sys import random ...