微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Spring MVC中删除URL重写中的jsessionid

如何解决在Spring MVC中删除URL重写中的jsessionid

要点:只要用户登录或不执行POST操作,就不要让您的应用创建会话。请勿致电request.getSession()request.getSession(true)。不要为未登录用户创建或管理会话范围的Bean。确保您正在使用的框架不会不必要地创建会话,除非您先声明要这样做。

如果由于应用程序的设计方式或所用(MVC)框架的局限性/错误确实 无法做到这一点,那么最好的选择是将Googlebot请求重定向到没有JSESSIONID标识符的URL。您可以为此使用Tuckey的URL重写过滤器(即Apache HTTPD的Java变体mod_rewrite)。以下是其配置示例页面中的相关摘录。

隐藏来自Googlebot的请求的jsessionid。

<outbound-rule>
     <name>Strip URL Session ID's</name>
     <note>
         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
MVC并在jsessionid中遇到问题,我发现,如果未在浏览器中启用cookie来生成如下所示的URL,则jsessionid将被注入到URL中:

http:// localhost / categories; jsessionid = Bsls4aQFXA5RUDcmZKV5iw?cid =
13001

实际上,浏览器没有问题,但是当Google抓取我的网站时,并且似乎Google抓取工具没有cookie
:)时,它们以该形式存储我的网站的url,并且我的网站出现在具有类似包含jsessionid的URL的搜索结果中。

实际上,它运行正常,没有任何问题,但是我更希望URL清晰显示在Google搜索结果中而没有jsessionid。

有什么帮助吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。