spring session实现session统一管理(jdbc实现)
最近在看一些关于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实现)的更多相关文章
- .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- 使用Spring Session做分布式会话管理
在Web项目开发中,会话管理是一个很重要的部分,用于存储与用户相关的数据.通常是由符合session规范的容器来负责存储管理,也就是一旦容器关闭,重启会导致会话失效.因此打造一个高可用性的系统,必须将 ...
- hibernate 管理 Session(单独使用session,不spring)
Hibernate 本身提供了三个管理 Session 对象的方法 Session 对象的生命周期与本地线程绑定 Session 对象的生命周期与 JTA 事务绑定 Hibernate 托付程序管理 ...
- Spring Security 之Session管理配置
废话不多说,直接上代码.示例如下: 1. 新建Maven项目 session 2. pom.xml <project xmlns="http://maven.apache.o ...
- hibernate 管理 Session(单独使用session,非spring)
Hibernate 自身提供了三种管理 Session 对象的方法 Session 对象的生命周期与本地线程绑定 Session 对象的生命周期与 JTA 事务绑定 Hibernate 托付程序管理 ...
- spring 管理 jdbc 事务
@Transactional 业务实现类 类名上方--这个类中的方法,执行操作前会打开事务. 默认:RuntimeException 自动回滚, 可以try catch 的异常,不会滚 方法名 ...
- Spring Cloud分布式Session共享实践
通常情况下,Tomcat.Jetty等Servlet容器,会默认将Session保存在内存中.如果是单个服务器实例的应用,将Session保存在服务器内存中是一个非常好的方案.但是这种方案有一个缺点, ...
- spring security控制session
spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...
- Spring Session解决Session共享
1. 分布式Session共享 在分布式集群部署环境下,使用Session存储用户信息,往往出现Session不能共享问题. 例如:服务集群部署后,分为服务A和服务B,当用户登录时负载到服务A ...
随机推荐
- 使用easyui为tab页增加右键菜单
在使用easyui进行上左右布局一文中,我们已经使用easyui搭建起了一个简单的上左右布局.在使用的过程中,我们经常会遇到tab页打开的太多,但只能一个一个的关闭的烦恼,这个时候有没有想到eclip ...
- tar解压缩问题
gzip: stdin: unexpected end of filetar: 归档文件中异常的 EOFtar: 归档文件中异常的 EOFtar: Error is not recoverable: ...
- Windows安装Docker & Docker-Compose & 配置docker私有仓库
一定要给windows先创建软连接,不然系统盘会爆表的: mklink /j .docker D:\Administrator\.docker Win7安装Docker Dockerfile # FR ...
- pycharm的常规使用
1.修改当前项目的Py版本,是py2还是py3 pycharm-->settings-->选中要运行的项目-->选择py版本(如果你两个py版本都装在本机的话) 2.显示行数 在每行 ...
- CDI 组件拦截器的使用和学习
拦截器的作用原理: 声明拦截器,加@Interceptor注解 方法有二: 1)为拦截器添加Qualifier: 2)不添加Qualifier.为拦截器添加具体的拦截方法,该方法加@AroundInv ...
- linux停止nginx服务 未成功
在上线新功能的时候,需要将服务器停掉,防止在更新过程中有用户进行操作额外的数据. 1:查看nginx主进程: ps -ef | grep nginx 这里root 后面的数字表示:主进程号nginx后 ...
- Nginx系列(9)- Nginx常用命令
Linux # 命令需要在Nginx的sbin目录下执行 cd /usr/local/nginx/sbin/ ./nginx #启动./nginx -s stop #停止 ./nginx -s qui ...
- 从浏览器渲染层面解析css3动效优化原理
引言 在h5开发中,我们经常会需要实现一些动效来让页面视觉效果更好,谈及动效便不可避免地会想到动效性能优化这个话题: 减少页面DOM操作,可以使用CSS实现的动效不多出一行js代码 使用绝对定位脱离让 ...
- 关于cgroup的几个核心名词及其关系
子系统(subsystem) 所谓子系统可以理解为操作系统里的各种资源(组件),如CPU,内存,磁盘,网卡(带宽) 层级(Hierarchies) 所谓层级就是子系统的集合,又 ...
- Python - with 语句
管理外部资源的背景 在编程中会面临的一个常见问题是如何正确管理外部资源,例如文件.锁和网络连接 有时,程序会永远保留这些资源,即使不再需要它们,这种现象称为内存泄漏 因为每次创建和打开给定资源的新实例 ...