利用tomcat-redis-session-manager实现单点登录
Redis+Tomcat实现session流程
1.客户端首次请求服务端
2.服务端产生session并set cookie响应给客户端
3.客户端再次请求服务端,会带上cookie
4.服务端根据cookie找到对应的session
实现思路
如果我们要编写程序实现这个方案,需要解决以下问题:
1.session的安全性,即不容易被仿造。
2.session的唯一性,如果用tomcat产生session的策略,多台tomcat会产生的session会存在重复的可能。
3.session的有效期维护,session会有个有效期,用户在这个时间内不访问系统,session将会失效,如果
用户一直访问,则要自动延长session有效期。
4.在集群session服务器中,要考虑负载均衡,这也是需要编写客户端代码的,在分布式session缓存中,
需要根据sessionId哈希分布,那么就和服务器个数进行了耦合,在添加和移除服务器的时候,将出现数
据不一致的问题。
5.如何实现才能让应用程序改动最小,或者是不改动。
我们可以选择自己写程序来实现以上功能,不过在这里我使用一个现成的框架,即tomcat-redis-session-manager
有时间并感兴趣的朋友,可以在这个基础上自行实现一个,这样更适合自己的项目。
服务器部署分布:
Tomcat1 端口8081
Tomcat2 端口 8080
Tomcat2/Conf/Context.xml配置(redis为本地redis端口 6379密码java2000_wl)
<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
password="java2000_wl"
maxInactiveInterval="60" />
tomcat/conf/server.xml 配置(配置工程路径)
注意:tomcat1和tomcat2的path必须相同要不不会使用浏览器cookie中保存的session访问
<Context docBase="d:\redis_session2"path="/testSession" reloadable="true"source="org.eclipse.jst.jee.server:Redis_session2"/>
Tomcat1 配置:
Tomcat1/Conf/Context.xml配置(redis为本地redis端口 6379密码java2000_wl)
<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
password="java2000_wl"
maxInactiveInterval="60" />
tomcat/conf/server.xml 配置(配置工程路径)
注意:tomcat1和tomcat2的path必须相同要不不会使用浏览器cookie中保存的session访问
<Context docBase="d:\redis_session1"path="/testSession" reloadable="true"source="org.eclipse.jst.jee.server:Redis_session1"/>
工程redis_session1内含有一个servlet
@WebServlet("/SetSession")
public classSetSession extendsHttpServlet {
private static final long serialVersionUID= 1L;
/**
* @see HttpServlet#HttpServlet()
*/
publicSetSession() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response)
*/
protected voiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
// TODO Auto-generated method stub
Stringsession=request.getSession().getId();
// request.getSession().setAttribute("test","test--value1");
// request.getSession().setAttribute("login","true");
request.getSession().setAttribute("username","zhangsan");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>AServlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is tomcat1 ");
out.println(", the sessionId is "+session);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request,HttpServletResponse response)
*/
protected voiddoPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
// TODO Auto-generated method stub
}
}
工程2
@WebServlet("/GetSession")
public classGetSession extendsHttpServlet {
private static final long serialVersionUID= 1L;
/**
* @see HttpServlet#HttpServlet()
*/
publicGetSession() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response)
*/
protected voiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
// TODO Auto-generated method stub
Stringsession=request.getSession().getId();
// request.getSession().setAttribute("test","test--value1");
// request.getSession().setAttribute("login","true");
Stringusername=(String) request.getSession().getAttribute("username");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>AServlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is tomcat2 ");
out.println(", the sessionId is "+session);
out.println(" username is "+username+"</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
效果展示 tomcat1
Tomcat 2
Redis:
下载链接:http://download.csdn.net/detail/u013851082/9719329