gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:50:47 26 4
gpt4 key购买 nike

我正在使用spring MVC并且在jsessionid中遇到问题,我发现如果在浏览器中未启用cookie,则会将jsessionid注入(inject)到url中,从而产生这样的url:

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

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

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

有什么帮助吗?

最佳答案

直截了当:只要用户不登录或执行 POST 操作,就不要让您的应用创建 session 。不要调用 request.getSession()request.getSession(true)。不要为未登录的用户创建或管理 session 范围的 bean。确保您使用的框架不会在没有您要求的情况下不必要地创建 session 。

如果这真的由于您的应用程序的设计方式或使用的 (MVC) 框架的限制/错误而无法实现,那么您最好的选择是将 Googlebot 请求重定向到 URL,而无需JSESSIONID 标识符。您可以使用Tuckey's URL rewrite filter为此(例如,Apache HTTPD 著名的 mod_rewrite 的 Java 变体)。以下是其 configuration examples page 的相关性摘录.

Hide jsessionid for requests from googlebot.


<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 - /
</note>
<condition name="user-agent">googlebot</condition>
<from>^(.*?)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$</from>
<to>$1$2$3</to>
</outbound-rule>

关于java - 在spring mvc中删除url重写中的jsessionid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5276634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com