我想完全消除HttpSession - 我可以在web.xml中实现吗?我相信有容器特定的方法可以实现它(这就是当我进行谷歌搜索时拥挤的搜索结果)。
P.S. 这是个坏主意吗?我倾向于在确实需要它们之前完全禁用它们。
我想完全消除HttpSession - 我可以在web.xml中实现吗?我相信有容器特定的方法可以实现它(这就是当我进行谷歌搜索时拥挤的搜索结果)。
P.S. 这是个坏主意吗?我倾向于在确实需要它们之前完全禁用它们。
我想要完全消除 HttpSession
你不能完全禁用它。你只需要在你的web应用程序代码中不使用request.getSession()
或request.getSession(true)
获取它的句柄,并确保你的JSPs不会通过设置<%@page session="false"%>
来隐式地获取它。
如果你真正关心的是禁用HttpSession
背后使用的cookie,那么在Java EE 5 / Servlet 2.5中,你只能在服务器特定的webapp配置中这样做。例如,在Tomcat中,你可以在<Context>
元素中将cookies
属性设置为false
。
<Context cookies="false">
还可以查看这个Tomcat特定文档。通过这种方式,如果没有对URL进行重写,会话将不会在随后的请求中保留 - 只有在某些情况下从请求中获取它时才会保留。毕竟,如果您不需要它,那就不要获取它,那么它就根本不会被创建/保留。
或者,如果您已经使用Java EE 6 / Servlet 3.0或更新版本,并且确实想通过web.xml
来完成,那么您可以使用以下方法在web.xml
中使用新的<cookie-config>
元素将最大年龄设置为零:
<session-config>
<session-timeout>1</session-timeout>
<cookie-config>
<max-age>0</max-age>
</cookie-config>
</session-config>
如果您想在Web应用程序中硬编码以使 getSession()
永远不会返回 HttpSession
(或者是“空” HttpSession
),那么您需要创建一个侦听 url-pattern
为/*
的过滤器,该过滤器使用 HttpServletRequestWrapper
实现替换 HttpServletRequest
,该实现在所有 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);
}
附言:这是一个坏主意吗?我更喜欢完全禁用某些功能,直到我真正需要它们。
如果你不需要它们,就不要使用它们。就这样。真的:)
HttpServletRequestWrapper
听起来不错,我会选择它。在一个有许多开发者的大型项目中,你需要对架构决策(如无状态)进行一些执行。你不能指望人们"仅仅不使用它"。 - barfuin<session-config>
<tracking-mode>URL</tracking-mode>
</session-config>
为了执行这个架构决定,请编写类似于下面的内容:
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");
}
}
将其添加到web.xml中,并修复出现该异常的地方:
<listener>
<listener-class>com.ideas.bucketlist.web.PreventSessionListener</listener-class>
</listener>
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
XML的格式如下:
<http create-session="stateless">
<!-- config -->
</http>
顺便提一下,NEVER和STATELESS之间的区别
NEVER:Spring Security将不会创建HttpSession,但如果HttpSession已经存在,则会使用它。
STATELESS:Spring Security将不会创建HttpSession,并且永远不会使用它来获取SecurityContext。
<session-config>
<session-timeout>1</session-timeout>
<cookie-config>
<max-age>0</max-age>
</cookie-config>
</session-config>
<outbound-rule encodefirst="true">
<name>Strip URL Session ID's</name>
<from>^(.*?)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$</from>
<to>$1$2$3</to>
</outbound-rule>
从Servlet 3.0开始,您可以通过在ServletContextListener
的contextInitialized
方法中添加以下代码来使会话不被Servlet容器以任何方式跟踪:
servletContext.setSessionTrackingModes(Collections.emptySet());
对于RESTful应用程序,我会在每次请求的生命周期结束时简单地使其无效。可能有一些Web服务器,在新客户端访问时始终创建新会话,无论您是否调用request.getSession()
。
无法避免会话创建。但您可以在请求周期结束时检查是否违反了自己的要求。因此,创建一个简单的servlet过滤器,将其放置在第一位,并在chain.doFilter之后抛出异常,如果已创建会话:
chain.doFilter(request, response);
if(request.getSession(false) != null)
throw new RuntimeException("Somewhere request.getSession() was called");