非静态 webapp 别名 [英] Non-static webapp alias

查看:35
本文介绍了非静态 webapp 别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否有一种干净的方法可以让名称未出现在请求 url 中的 web 应用程序回答请求.在某种程度上,url 应该是 webapp 的别名.此外,别名不应该是静态的或固定在这个单一的 webapp 中,而是我需要能够轻松地更改别名后面的 webapp 以进行版本增量.我想到的事情是

I would like to know, if there is a clean way to have a request answered by a webapp whose name does not appear in the request url. In a way, the url should be an alias of the webapp. Also, the alias should not be static or fixed with this single webapp but rather I need to be able to change the webapp behind the alias easily for version increments. The things, that come to my mind are

  • 构建一个外观"网络应用程序来重定向请求
  • 重命名完整的网络应用

这两种想法都不会导致预期的结果.我想要一个更轻量级的解决方案.

Both ideas don't lead to the desired result. I would like to have a more lightweight solution.

这可能吗?

推荐答案

感谢 Alexander 的帖子,我找到了正确的轨道,但我使用了一些额外的工作,这或多或少是我的第 1) 点中提到的外观 webapp原始问题.我将向任何感兴趣的人展示我的整个方法:

Thanks to the post from Alexander I found the right track but I used some additional work, which is more or less the facade webapp mentioned in point 1) from my original question. I will show my entire approach for anyone interested:

首先,我在 /tomcat/webapps 中部署了所有标准 web 应用程序,并启用了一个标准 context.xmlautoDeploy 模式.为了捕获与部署的上下文之一不匹配的所有其他请求,我设置了一个新的 webapp,作为不匹配请求的默认 webapp.我们称之为dispatcher-app.

First, I have all my standard webapps deployed in /tomcat/webapps with one standard context.xml and autoDeploy mode on. To catch all other requests, that do not match one of the deployed contexts, I set up a new webapp which acts as a default webapp for not matched requests. Let's call it dispatcher-app.

为了使这一项工作,我必须将其设置为ROOT.为了以其原始名称部署它,我将它放置在 /tomcat/webapps2 及其关联的 context.xml/tomcat/conf/Catalina/localhost/ROOT.xml.有必要将它放在 /webapps 之外,以防止在 tomcat 中与其他 webapp 使用的 autoDeploy 模式进行双重部署.其他信息可以在这里找到:http://tomcat.apache.org/tomcat-7.0-doc/config/context#Naming

To make this one work, I have to set it as ROOT. To deploy it under its original name, I located it under /tomcat/webapps2 and its associated context.xml in /tomcat/conf/Catalina/localhost/ROOT.xml. It's necessary to put it outside of /webapps to prevent double deployment in tomcat with the autoDeploy mode used for the other webapps. Additional info on that can be found here: http://tomcat.apache.org/tomcat-7.0-doc/config/context#Naming

ROOT.xml 看起来像这样:

<Context
    path=""
    docBase="/path/to/tomcat-base/webapps2/dispatcher-app"
    crossContext="true"
/>

dispatcher-app 的 web.xml 中确保,它使用

In web.xml of the dispatcher-app make sure, it uses

<servlet-mapping>
    <url-pattern>/</url-pattern>
 </servlet-mapping>

dispatcher-app 内部使用 uriInfo.getAbsolutePath() 查找用户请求的原始路径.然后它将来自 url 的应用程序与在 tomcat 中运行的应用程序进行匹配,从自定义配置文件中得知.如果匹配,配置文件就知道应该向哪个应用程序进行转发.例如,用户请求myserver/webapp-1/test?param=value,并转发到myserver/webapp-1.1.2/test?param=value.如果应用程序版本发生变化,可以在配置文件中手动更新要转发到的 webapp.

The dispatcher-app inside looks for the original path that was requested by the user using uriInfo.getAbsolutePath(). It then matches the application from the url with applications that run in tomcat, known from a custom configuration file. If there is a match, the configuration file knows, to which application the forward should be made. For example, the user requests myserver/webapp-1/test?param=value, and is forwarded to myserver/webapp-1.1.2/test?param=value. The webapp to forward to can be updated in the configuration file manually if application version changes.

实际转发应该使用

RequestDispatcher dispatcher=servletContext.getRequestDispatcher(redirectTo);
dispatcher.forward(request, response);

因为转发对用户不可见.

because the forward will not be visible to the user.

因为我无法运行(参见 tomcat 结果中的跨上下文请求转发在 java.lang.ClassCastException: org.glassfish.jersey.message.internal.TracingLogger) 中,我现在只是使用了 307 重定向.

As I could not make that run (see Cross-context request forwarding in tomcat results in java.lang.ClassCastException: org.glassfish.jersey.message.internal.TracingLogger), I simply used a 307 redirect for now.

return Response.status(307).location(new URI(redirectTo)).build();

这个的缺点是,浏览器会在地址栏中显示重定向 URL.

The drawback of this one is, the browser will show the redirect URL in the address bar.

更新:RequestDispatcher 现在可以工作,只是不适用于上面提到的这种特殊情况/网络应用程序.所以我更喜欢这种方式而不是重定向方法.

UPDATE: The RequestDispatcher works now, just not for this special case/webapp mentioned above. So I prefer that over the redirect approach.

需要注意的一点是,第二个 webapp 上基于容器的身份验证(例如 realm)将无效(就像我们在 tomcat facade 的前向后面"所做的那样),因此 dispatcher-app 本身需要一种身份验证方法,或者需要使用其他机制进行身份验证,例如 RequestFilter.

One thing to note is, that container-based authentication (e.g. realm) on the second webapp will not be effective (as we do the forward 'behind' tomcat facade) so the dispatcher-app needs an authentication method itself or authentication needs to be made with another mechanism, such as a RequestFilter.

这篇关于非静态 webapp 别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆