java Tomcat 未向 Web 应用程序的上下文添加尾部斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11055608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:36:47  来源:igfitidea点击:

Tomcat is not adding trailing slash to web app's context

javaspringtomcatwar

提问by Badweather

I'd like to have Tomcat automatically add a trailing slash to my app's context if the url is entered without it.

如果在没有它的情况下输入 url,我希望 Tomcat 自动在我的应用程序上下文中添加一个尾部斜杠。

When I test with Jetty, it automatically adds the trailing slash to my app's context, but Tomcat doesn't do this.

当我使用 Jetty 进行测试时,它会自动将尾部斜杠添加到我的应用程序上下文中,但 Tomcat 不会这样做。

I'm uncertain what the context will be named once deployed, as I'm handing the WAR off to someone else, so any resource references in HTML is all relative. Is there any way to have Tomcat automatically redirect to the same context with a trailing slash added?

我不确定一旦部署后上下文将命名什么,因为我将 WAR 交给其他人,因此 HTML 中的任何资源引用都是相对的。有没有办法让 Tomcat 自动重定向到添加了尾部斜杠的相同上下文?

Currently Using Tomcat 7 with Spring 3.

目前使用 Tomcat 7 和 Spring 3。

回答by Pat

It's an old post, but as of Tomcat 7.0.67, you need to add the following attribute to your context.xml file:

这是一篇旧帖子,但从 Tomcat 7.0.67 开始,您需要将以下属性添加到您的 context.xml 文件中:

<Context mapperContextRootRedirectEnabled="true">...</Context>

As per the 7.0.67 changelog:

根据 7.0.67 变更日志

Move the functionality that provides redirects for context roots and directories where a trailing / is added from the Mapper to the DefaultServlet. This enables such requests to be processed by any configured Valves and Filters before the redirect is made. This behaviour is configurable via the mapperContextRootRedirectEnabled and mapperDirectoryRedirectEnabled attributes of the Context which may be used to restore the previous behaviour.

将为上下文根和目录提供重定向的功能从 Mapper 添加到 DefaultServlet。这使得此类请求能够在进行重定向之前由任何配置的阀门和过滤器处理。此行为可通过 Context 的 mapperContextRootRedirectEnabled 和 mapperDirectoryRedirectEnabled 属性进行配置,可用于恢复先前的行为。

And in the Tomcat context documentation:

在 Tomcat 上下文文档中

mapperContextRootRedirectEnabled: If enabled, requests for a web application context root will be redirected (adding a trailing slash) if necessary by the Mapper rather than the default Servlet. This is more efficient but has the side effect of confirming that the context path exists. If not specified, the default value of false is used.

mapperContextRootRedirectEnabled:如果启用,对 Web 应用程序上下文根的请求将在必要时由 Mapper 而不是默认 Servlet 重定向(添加尾部斜杠)。这更有效,但具有确认上下文路径存在的副作用。如果未指定,则使用默认值 false。

回答by alexgirao

It seems that your application's web.xml has a mapping to "/*". A servlet-mapping to "/*"causes tomcat to pass the request as-is to the web application (i.e. does not redirect).

您的应用程序的 web.xml 似乎映射到"/*". servlet 映射到"/*"导致 tomcat 将请求按原样传递给 Web 应用程序(即不重定向)。

To properly redirect, you must change the "/*"mapping to just "/", the latter means the default servlet.

要正确重定向,您必须将"/*"映射更改为 just "/",后者表示默认 servlet。

回答by Istvan Devai

Tomcat adds a trailing slash automatically. Just test it with the example application supplied with Tomcat..

Tomcat 会自动添加尾部斜杠。只需使用 Tomcat 提供的示例应用程序对其进行测试即可。

If - due to some special configuration - it does not, I'd write a Filter that examines the query string and redirects as needed by the application. Many times this is needed anyways (doing http->https redirections, etc.)

如果 - 由于某些特殊配置 - 它没有,我会编写一个过滤器来检查查询字符串并根据应用程序的需要进行重定向。很多时候这都是需要的(做 http->https 重定向等)

回答by Ervi B

Have you tried playing with URL Rewrite on Tomcat?
This might help: http://code.google.com/p/urlrewritefilter/

你试过在 Tomcat 上玩 URL Rewrite 吗?
这可能会有所帮助:http: //code.google.com/p/urlrewritefilter/

If that does not help, take a look at this: URL rewrite in tomcat web.xml

如果这没有帮助,请看一下:URL rewrite in tomcat web.xml

回答by em_bo

Pat's excellent answer helped me dig up a few more details on this. It seems this is related to some quirks in some versions of Tomcat (Tomcat 7 at 7.0.67+, and Tomcat 8 between 8.29 and 8.37) having to do with session cookies and URL redirection.

Pat 的出色回答帮助我挖掘了更多细节。这似乎与某些版本的 Tomcat(7.0.67+ 的 Tomcat 7,以及 8.29 和 8.37 之间的 Tomcat 8)中的一些怪癖有关,与会话 cookie 和 URL 重定向有关。

The bottom line seems to be that if the java server creates path-specific session cookies with a slash at the end (like "/app_name/"), then the server must also do an automatic initial redirect (/app_name --> /app_name/) ... otherwise, the session cookie will not get sent with the request, and it will never look to the server like you have a valid session. The may cause a redirect loop from the app to the authentication.

底线似乎是,如果 Java 服务器创建路径特定的会话 cookie,末尾带有斜杠(如“/app_name/”),那么服务器还必须执行自动初始重定向(/app_name --> /app_name /) ...否则,会话 cookie 将不会与请求一起发送,并且它永远不会像您拥有有效会话一样查看服务器。这可能会导致从应用程序到身份验证的重定向循环。

There are configurations in Tomcat that control both behaviors, but as far as I can tell, they were essentially out-of-sync in these versions, such that one might get the cookie withthe trailing slash, withoutgetting the redirect. There are several related issues/changes in the Tomcat changelog: https://tomcat.apache.org/tomcat-8.0-doc/changelog.html

Tomcat 中有一些配置可以控制这两种行为,但据我所知,它们在这些版本中基本上是不同步的,因此人们可能会获得带有斜杠的 cookie ,而不会获得重定向。Tomcat 更改日志中有几个相关的问题/更改:https: //tomcat.apache.org/tomcat-8.0-doc/changelog.html

As Pat has already noted, this is resolved by adding this attribute to your app's Context element:

正如 Pat 已经指出的,这是通过将此属性添加到您的应用程序的 Context 元素来解决的:

<Context mapperContextRootRedirectEnabled="true">