我有一个应用程序,它使用欢迎页面 index.jsp 和<iframe></iframe>
iframe 的内容是一个 jsf 页面。如果我访问 index.jsp,我会在第一次进入 firebug 时看到一个 cookie:
Set-Cookie JSESSIONID=C615DA89B6EF73F801973EA3DCD3B226; Path=/
的页面<iframe>
继承了这个jsessionid。但是:当我直接访问页面时,<iframe/>
我会在第一次请求时将 jsessionId 重写为所有没有 cookie 的 URL。之后使用 cookie。这一切都很好 - 如果:安全系统允许我执行 url 重写。
我运行 jboss 4.2.2
我想实现与 index.jsp 相同的行为 - 例如,始终使用 cookie 并始终避免 http 重写。
[编辑] 感谢 balusc 的回答,我写了这个:
public class JsessionIdAvoiderFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
ServletException {
boolean allowFilterChain = redirectToAvoidJsessionId((HttpServletRequest) req, (HttpServletResponse)res);
//I'm doing this because if I execute the request completely, it will perform a pretty heavy lookup operation. No need to do it twice.
if(allowFilterChain)
chain.doFilter(req, res);
}
public static boolean redirectToAvoidJsessionId(HttpServletRequest req, HttpServletResponse res) {
HttpSession s = req.getSession();
if(s.isNew()) {
//after the redirect we don't want to redirect again.
if(!(req.isRequestedSessionIdFromCookie()&&req.isRequestedSessionIdFromURL()))
{
//yeah we have request parameters actually on that request.
String qs = req.getQueryString();
String requestURI = req.getRequestURI();
try {
res.sendRedirect(requestURI+"?"+qs);
return false;
} catch (IOException e) {
logger.error("Error sending redirect. " + e.getMessage());
}
}
}
return true;
}
}
不要忘记将其添加到您的web.xml
<filter>
<display-name>JsessionId Filter</display-name>
<filter-name>jsessionIdAvoiderFilter</filter-name>
<filter-class>my.namespace.JsessionIdAvoiderFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>jsessionIdAvoiderFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>