热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我可以关闭web.xml中的HttpSession吗?-CanIturnofftheHttpSessioninweb.xml?

IwouldliketoeliminatetheHttpSessioncompletely-canIdothisinweb.xml?Imsurethereare

I would like to eliminate the HttpSession completely - can I do this in web.xml? I'm sure there are container specific ways to do it (which is what crowds the search results when I do a Google search).

我想完全消除HttpSession - 我可以在web.xml中这样做吗?我确信有容器特定的方法可以做到这一点(当我进行谷歌搜索时,这是搜索结果的主要内容)。

P.S. Is this a bad idea? I prefer to completely disable things until I actually need them.

附:这是一个坏主意吗?在我真正需要它之前,我更喜欢完全禁用它们。

9 个解决方案

#1


68  

I would like to eliminate the HttpSession completely

我想完全消除HttpSession

You can't entirely disable it. All you need to do is to just not to get a handle of it by either request.getSession() or request.getSession(true) anywhere in your webapplication's code and making sure that your JSPs don't implicitly do that by setting <%@page session="false"%>.

你不能完全禁用它。您需要做的就是不要通过web应用程序代码中的任何位置的request.getSession()或request.getSession(true)来处理它,并确保您的JSP不会通过设置<%隐式地执行此操作@page session =“false”%>。

If your main concern is actually disabling the COOKIE which is been used behind the scenes of HttpSession, then you can in Java EE 5 / Servlet 2.5 only do so in the server-specific webapp configuration. In for example Tomcat you can set the COOKIEs attribute to false in element.

如果您的主要关注点实际上是禁用在HttpSession幕后使用的COOKIE,那么您可以在Java EE 5 / Servlet 2.5中仅在特定于服务器的webapp配置中执行此操作。例如,在Tomcat中,您可以在 元素中将COOKIEs属性设置为false。


Also see this Tomcat specific documentation. This way the session won't be retained in the subsequent requests which aren't URL-rewritten --only whenever you grab it from the request for some reason. After all, if you don't need it, just don't grab it, then it won't be created/retained at all.

另请参阅此Tomcat特定文档。这样,会话将不会保留在后续的URL重写请求中 - 只要您出于某种原因从请求中获取它时。毕竟,如果你不需要它,只是不要抓住它,那么根本不会创建/保留它。

Or, if you're already on Java EE 6 / Servlet 3.0 or newer, and really want to do it via web.xml, then you can use the new element in web.xml as follows to zero-out the max age:

或者,如果您已经使用Java EE 6 / Servlet 3.0或更高版本,并且真的想通过web.xml执行此操作,那么您可以使用web.xml中的新 元素进行清零最大年龄:


    1
    
        0
    

If you want to hardcode in your webapplication so that getSession() never returns a HttpSession (or an "empty" HttpSession), then you'll need to create a filter listening on an url-pattern of /* which replaces the HttpServletRequest with a HttpServletRequestWrapper implementation which returns on all getSession() methods null, or a dummy custom HttpSession implementation which does nothing, or even throws UnsupportedOperationException.

如果你想在你的web应用程序中硬编码,以便getSession()永远不会返回HttpSession(或“空”HttpSession),那么你需要创建一个监听/ *的url-pattern的过滤器,它用一个替换HttpServletRequest HttpServletRequestWrapper实现,它返回所有getSession()方法null,或者一个虚拟的自定义HttpSession实现,它什么都不做,甚至抛出UnsupportedOperationException。

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request) {
        @Override
        public HttpSession getSession() {
            return null;
        }
        @Override
        public HttpSession getSession(boolean create) {
            return null;
        }
    }, response);
}

P.S. Is this a bad idea? I prefer to completely disable things until I actually need them.

附:这是一个坏主意吗?在我真正需要它之前,我更喜欢完全禁用它们。

If you don't need them, just don't use them. That's all. Really :)

如果您不需要它们,请不要使用它们。就这样。真的:)

#2


6  

If you are building a stateless high load application you can disable using COOKIEs for session tracking like this (non-intrusive, probably container-agnostic):

如果您正在构建无状态高负载应用程序,则可以禁用使用COOKIE进行会话跟踪(非侵入式,可能与容器无关):


    URL

To enforce this architectural decision write something like this:

要强制执行此体系结构决策,请执行以下操作:

public class PreventSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
    throw new IllegalStateException("Session use is forbidden");
}

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    throw new IllegalStateException("Session use is forbidden");
}
}

And add it to web.xml and fix places where it fails with that exception:

并将其添加到web.xml并使用该异常修复失败的位置:


    com.ideas.bucketlist.web.PreventSessionListener

#3


4  

I use the following method for my RESTful app to remove any inadvertent session COOKIEs from being created and used.

我使用以下方法为我的RESTful应用程序删除任何无意的会话COOKIE被创建和使用。


    1
    
        0
    

However, this does not turn off HttpSessions altogether. A session may still be created by the application inadvertently, even if it disappears in a minute and a rogue client may ignore the max-age request for the COOKIE as well.

但是,这并没有完全关闭HttpSessions。应用程序仍可能无意中创建会话,即使它在一分钟内消失,并且流氓客户端也可能忽略COOKIE的max-age请求。

The advantage of this approach is you don't need to change your application, just web.xml. I would recommend you create an HttpSessionListener that will log when a session is created or destroyed so you can track when it occurs.

这种方法的优点是您不需要更改您的应用程序,只需更改web.xml。我建议您创建一个HttpSessionListener,它将在创建或销毁会话时记录,以便您可以跟踪它何时发生。

#4


2  

Rather than disabling you can rewrite the URL using a URL rewrite filter eg tuckey rewrite filter. This will give Google friendly results but still allow COOKIE based session handling.

您可以使用URL重写过滤器(例如tuckey重写过滤器)重写URL,而不是禁用。这将为Google提供友好的结果,但仍允许基于COOKIE的会话处理。

However, you should probably disable it for all responses as it's worse than just search engine unfriendly. It exposes the session ID which can be used for certain security exploits.

但是,你可能应该为所有响应禁用它,因为它比不友好的搜索引擎更糟糕。它公开了可用于某些安全漏洞的会话ID。

Example config for Tuckey filter:

Tuckey过滤器的配置示例:


  Strip URL Session ID's
  ^(.*?)(?:\;jsessiOnid=[^\?#]*)?(\?[^#]*)?(#.*)?$
  $1$2$3

#5


2  

I would like to eliminate the HttpSession completely - can I do this in web.xml? I'm sure there are container specific ways to do it

我想完全消除HttpSession - 我可以在web.xml中这样做吗?我确信有容器特定的方法来做到这一点

I don't think so. Disabling the HttpSession would be a violation of the Servlet spec which states that HttpServletRequest#getSession should return a session or create one. So I wouldn't expect a Java EE container to provide such a configuration option (that would make it non compliant).

我不这么认为。禁用HttpSession将违反Servlet规范,该规范声明HttpServletRequest#getSession应返回会话或创建会话。所以我不希望Java EE容器提供这样的配置选项(这会使它不兼容)。

Is this a bad idea? I prefer to completely disable things until I actually need them.

这是一个坏主意吗?在我真正需要它之前,我更喜欢完全禁用它们。

Well, I don't really get the point, just don't put anything in the session if you don't want to use it. Now, if you really want to prevent the use of the session, you can use a Filter to replace the request with a implementation of HttpServletRequestWrapper overriding getSession(). But I wouldn't waste time implementing this :)

好吧,我真的不明白,如果你不想使用它,就不要在会话中放任何东西。现在,如果您确实想要阻止使用会话,可以使用Filter将请求替换为HttpServletRequestWrapper的实现,从而覆盖getSession()。但我不会浪费时间实现这个:)

Update: My initial suggestion was not optimal, the "right" (cough) way would be to replace the request.

更新:我的初步建议不是最佳的,“正确”(咳嗽)方式将取代请求。

#6


1  

For RESTful application, I simply invalidate it every time the request's lifecycle ends. There may be some web server that always creates new session when new client access whether you call request.getSession() or not.

对于RESTful应用程序,我每次请求的生命周期结束时都会使其无效。可能有一些Web服务器总是在新客户端访问时创建新会话,无论您是否调用request.getSession()。

#7


1  

In Spring Security 3 with Java Config, you can use HttpSecurity.sessionManagement():

在使用Java Config的Spring Security 3中,您可以使用HttpSecurity.sessionManagement():

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

Xml looks like this;

Xml看起来像这样;


  

By the way, the difference between NEVER and STATELESS

顺便说一下,NEVER和STATELESS之间的区别

NEVER:Spring Security will never create an HttpSession, but will use the HttpSession if it already exists

永远不会:Spring Security永远不会创建HttpSession,但如果它已经存在将使用HttpSession

STATELESS:Spring Security will never create an HttpSession and it will never use it to obtain the SecurityContext

STATELESS:Spring Security永远不会创建HttpSession,它永远不会使用它来获取SecurityContext

#8


0  

One cannot avoid the session creation. But you can check if you violate your own requirement at the end of a request cycle. So, create a simple servlet filter, which you place as first and after chain.doFilter throw an exception if a session was created:

人们无法避免会话创建。但您可以在请求周期结束时检查是否违反了自己的要求。因此,创建一个简单的servlet过滤器,在第一个和第一个之后放置。如果创建了一个会话,则会抛出一个异常:

chain.doFilter(request, response);
if(request.getSession(false) != null)
    throw new RuntimeException("Somewhere request.getSession() was called");

#9


0  

As of Servlet 3.0, you can make it so sessions are not tracked by the servlet container in any way, by adding code like this to the contextInitialized method of a ServletContextListener:

从Servlet 3.0开始,您可以通过向Servlet容器以任何方式跟踪会话,方法是将这样的代码添加到ServletContextListener的contextInitialized方法中:

servletContext.setSessionTrackingModes(Collections.emptySet());

Javadoc.

的Javadoc。


推荐阅读
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • t-io 2.0.0发布-法网天眼第一版的回顾和更新说明
    本文回顾了t-io 1.x版本的工程结构和性能数据,并介绍了t-io在码云上的成绩和用户反馈。同时,还提到了@openSeLi同学发布的t-io 30W长连接并发压力测试报告。最后,详细介绍了t-io 2.0.0版本的更新内容,包括更简洁的使用方式和内置的httpsession功能。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了在使用Python中的aiohttp模块模拟服务器时出现的连接失败问题,并提供了相应的解决方法。文章中详细说明了出错的代码以及相关的软件版本和环境信息,同时也提到了相关的警告信息和函数的替代方案。通过阅读本文,读者可以了解到如何解决Python连接服务器失败的问题,并对aiohttp模块有更深入的了解。 ... [详细]
  • Ubuntu安装常用软件详细步骤
    目录1.GoogleChrome浏览器2.搜狗拼音输入法3.Pycharm4.Clion5.其他软件1.GoogleChrome浏览器通过直接下载安装GoogleChro ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 深入理解Kafka服务端请求队列中请求的处理
    本文深入分析了Kafka服务端请求队列中请求的处理过程,详细介绍了请求的封装和放入请求队列的过程,以及处理请求的线程池的创建和容量设置。通过场景分析、图示说明和源码分析,帮助读者更好地理解Kafka服务端的工作原理。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • GreenDAO快速入门
    前言之前在自己做项目的时候,用到了GreenDAO数据库,其实对于数据库辅助工具库从OrmLite,到litePal再到GreenDAO,总是在不停的切换,但是没有真正去了解他们的 ... [详细]
author-avatar
单莼de笑脸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有