关于Tomcat如何禁用Session 的探讨
——我一直不太信任自己的记忆力,所以我把它们都写下来
1.Web项目Session什么时候生成
需要明确一点,访问html文件的时候是不会生成Session的,Session的生成必须调用request.getSession()或者request.getSession(true),request.getSession(false)不会生成Session。JSP文件中默认自动添加<%@ page session=
"true"
>表示生成Session,jsp文件其实最后也是生成了Servlet类,添加了前面的配置后也会在Servlet类中调用
request.getSession(true)。
如果单独的jsp页面不想生成Session可以在文件开头加入<%@ page session="false"%>。但是有些时候我们整个项目都不需要tomcat管理Session,如果每一个JSP、Servlet都手动添加<%@ page session="false"%>是不是有点太麻烦了?
2.如何不生成Session
先来借鉴下Shiro是如何自己管理Session 的,Shiro框架在管理权限时,用户的Session是通过框架来管理的。
需要认证的资源被访问时Shiro会判断Session。在集成了Shiro框架的web项目中,对getSession()方法debug下去发现,原来Shiro重写了getSession()来实现Session的管理。下面是Shiro覆盖的源码
publi class ShiroHttpServletRequest extends HttpServletRequestWrapper { …… public HttpSession getSession(boolean create) { HttpSession httpSession; if (isHttpSessions()) { //默认session httpSession = super.getSession(false); if (httpSession == null && create) { //Shiro 1.2: assert that creation is enabled (SHIRO-266): if (WebUtils._isSessionCreationEnabled(this)) { httpSession = super.getSession(create); } else { throw newNoSessionCreationException(); } } } else {//Shiro管理的Session if (this.session == null) { boolean existing = getSubject().getSession(false) != null; Session shiroSession = getSubject().getSession(create); if (shiroSession != null) { this.session = new ShiroHttpSession(shiroSession, this, this.servletContext); if (!existing) { setAttribute(REFERENCED_SESSION_IS_NEW, Boolean.TRUE); } } } httpSession = this.session; } return httpSession; } public HttpSession getSession() { return getSession(true); } …… }
从上面Shiro源码可以看到原来Shiro重写了默认的HttpServletRequestWrapper类。
3.自己重写HttpServletRequestWrapper.getSession()
我们也可以模仿Shiro的思路
//1.继承HttpServletRequestWrapper 重写getSession()方法 public class MyHttpServletRequest extends HttpServletRequestWrapper{ ... } //2.配置一个过滤器,在过滤器中配置自己的Request类 @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request), response); }
上面的管理方法对于Session 的管理变的非常灵活,完全可以按照项目的需求继续拓展,比如Session的分布式管理,把Session数据缓存管理;移动端APP的Session管理等。
相关推荐
tomcat修改sessionId,同一台服务器部署多个tomcat需要修改sessionId,否则会出现session冲突的问题
设定tomcat中session过期时间的三种方式
tomcat 集群 nginx 使用redis 保证session同步
redis tomcat7 session共享
tomcat5 session 复制
tomcat集群session共享
Tomcat memcached Session依赖jar包
tomcat集群session共享问题解决方案,以及应用到的相关资料
Tomcat_Session的持久化,详细讲解tomcatsession管理的原理和持久化原理
如何实现服务器集群session共享,redis用来tomcat共享。
tomcat集群实现session复制tomcat集群实现session复制tomcat集群实现session复制tomcat集群实现session复制tomcat集群实现session复制tomcat集群实现session复制tomcat集群实现session复制tomcat集群实现session复制...
linux.tomcat-session共享.redis.安装包
多个服务器部署Tomcat共享Session的方法,自己亲身测试
使用说明:请严格按照资源内的readme文档来说明部署,不要搞错版本,完美支持Tomcat8和Tomcat9等新版本Tomcat,仅支持JDK8以上版本JDK。
nginx+tomcat8+memcached session共享所需jar包 直接放到tomcat/lib下即可
包含的jar包如下: commons-pool2-2.0.jar jedis-2.5.2.jar tomcat-juli-adapters.jar tomcat-juli.jar tomcat-redis-session-manager1.2.jar
一直以来,我误解认为启动了n个tomcat,则Session需要同步复制到n个Tomcat中存在,因此在启动了6个以上的Tomcat,性能会大大下降。 而实际情况下,采取Apache 加Tomcat进行负载均衡集群的时候,是可以不用将Session...
Tomcat8 我已经配置好了jar包,实现多Tomcatsession同步。
redis-tomcat-session共享包
linux-tomcat+nginx解决session共享,linux-tomcat+nginx解决session共享,linux-tomcat+nginx解决session共享,