在spring mvc中删除url重写中的jsessionid

我正在使用spring MVC并且在jsessionid中遇到问题,我发现如果在浏览器中没有启用cookie,则会在URL中注入jsessionid,生成类似这样的URL:

HTTP://本地主机/类别; JSESSIONID = Bsls4aQFXA5RUDcmZKV5iw CID = 13001?

实际上浏览器没有问题,但是当Google抓取我的网站时,Google抓取工具似乎没有Cookie :),他们会以该格式存储我网站的url,而我的网站会显示在搜索结果中,其url就像包含jsessionid的url一样。

实际上它运行没有任何问题,但我更喜欢在没有jsessionid的情况下将url显示在Google搜索结果中。

有帮助吗?

要点:只要用户不登录或执行POST操作,就不要让你的应用创建会话。 不要调用request.getSession()request.getSession(true) 。 不要为未登录的用户创建或管理会话范围的bean。 确保您使用的框架不会不必要地创建会话,而不是您要这样做。

如果由于您的应用程序的设计方式或由于所使用的(MVC)框架的限制/错误而确实不可能,那么您最好的办法是将Googlebot请求重定向到没有JSESSIONID标识符的URL。 您可以使用Tuckey的URL重写filter (例如,Apache HTTPD的着名mod_rewrite的Java变体)。 以下是其配置示例页面中的相关摘录。

隐藏来自googlebot的请求的jsessionid。


  Strip URL Session ID's  Strip ;jsession=XXX from urls passed through response.encodeURL(). The characters ? and # are the only things we can use to find out where the jsessionid ends. The expression in 'from' below contains three capture groups, the last two being optional. 1, everything before ;jesessionid 2, everything after ;jesessionid=XXX starting with a ? (to get the query string) up to # 3, everything ;jesessionid=XXX and optionally ?XXX starting with a # (to get the target) eg, from index.jsp;jsessionid=sss?qqq to index.jsp?qqq from index.jsp;jsessionid=sss?qqq#ttt to index.jsp?qqq#ttt from index.jsp;jsessionid=asdasdasdsadsadasd#dfds - index.jsp#dfds from u.jsp;jsessionid=wert.hg - u.jsp from /;jsessionid=tyu - /  googlebot ^(.*?)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$ $1$2$3  

Spring可以配置为不这样做: 为什么jsessionid被附加到每个url?

Web应用程序可以配置为阻止它: http : //randomcoder.org/articles/jsessionid-considered-harmful

如果你不使用Spring http标签。
转到定义Springfilter链的applicationFilterChain bean。
通常,您将有一个名为httpSessionContextIntegrationFilter的filter或非常接近的filter,它基于类org.springframework.security.web.context.HttpSessionContextIntegrationFilter或从其inheritance。
添加属性:

  

并添加bean:

    

这应该等同于将disable-url-rewriting设置为true

我会插入一个filter,如果它检测到一个机器人(如googlebot)使用自定义的HttpServletResponse,它会覆盖encodeUrl方法,只返回原始URL。 如果filter没有检测到机器人,它只会让链继续,这应该让url编码等按照默认值继续。

在你的url中删除jsessionid的最简单方法是在登录页面上更改为标记,其中调用j_spring_security_check为