关于Tomcat怎么禁用Session 的探讨
关于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管理等。
完全不用时,为啥不在dofilter中直接写一句request.getSession(false);?
完全不用时,为啥不在dofilter中直接写一句request.getSession(false);?
因为无法保证在后面的 Servlet(或Controller等)不会使用 request.getSession(true),shiro 的方案覆盖了这个场景。
完全不用时,为啥不在dofilter中直接写一句request.getSession(false);?
web项目不要Session的应该不多,本文说明的是不用Tomcat生成的Session,开发者自己管理Session。比如app服务器的token
完全不用时,为啥不在dofilter中直接写一句request.getSession(false);?
因为无法保证在后面的 Servlet(或Controller等)不会使用 request.getSession(true),shiro 的方案覆盖了这个场景。
哦,明白了,这个设置不是一次请求中先设置好就一直有效的。
所以如果要严格控制一个对象,就要自己写代理类,或者包装类来拦截这个方法,而后面拿到的都不是原始对象了。
所以还要保证的是在这之前的filter不调用getSession就OK了。
完全不用时,为啥不在dofilter中直接写一句request.getSession(false);?
web项目不要Session的应该不多,本文说明的是不用Tomcat生成的Session,开发者自己管理Session。比如app服务器的token
谢谢,我们的小项目还没碰到过,查了一下。session比token明显太重量级了,开销比较大,所以有时候不要。
大概查了一下,tomcat没有直接关闭session的配置,其它人的方法有:所有请求都模拟一个人的session,或者自己实现sessionManager。