如何在JBoss中从“根(/)”上下文自动重定向到“我的Web应用程序”?

18

我正在使用 JBoss 6.0。

我已经部署了我的 Web 应用程序:在 web-context 下部署了 myApp.ear,即 "/test"。 因此,如果我在浏览器 URL 中输入 "http://localhost:8080/test/",我会得到登录页面 (myLogin.jsp)。

由于我的 WAR 文件存在于 EAR 文件中,所以我使用 application.xml 文件中的 context-root 元素来指定上下文根 - 即:

<module>
    <web>
        <web-uri>myWeb.war</web-uri>
        <context-root>/test</context-root>
    </web>
</module>
我的问题是如何从“根目录”自动重定向用户到我的Web应用程序?
我的意思是,如果用户键入“http://localhost:8080/”,我期望我的Web应用程序的登录页面加载(而不是JBoss默认的ROOT.war的index.html页面)。
我从{JBOSS}\server\default\deploy\ROOT.war删除了现有的index.html并在那里创建了一个login.jsp。现在当我键入http://localhost:8080/时,可以看到正在调用“login.jsp”。但我无法将用户请求重定向到我的Web应用程序的登录页。
在那个login.jsp中,我尝试使用:<jsp:forward page="/test" />,但是我得到错误:“HTTP状态404 - /test”。
如果我像这样调用<jsp:forward page="/test/myLogin.jsp" />,我仍然收到相同的404错误。
有人能建议如何实现从根上下文自动重定向到我的Web应用程序吗?
3个回答

30

你需要将 index.html 保留在默认的部署文件夹中,并将请求转发到你的网页模块。

例如,在 index.html 文件中只需保留以下行:

<META HTTP-EQUIV="Refresh" CONTENT="0; URL=/test/"/> 

7

Senthil 的答案很好,但用户可以看到浏览器实际执行的重定向(页面闪烁)。JBoss服务器还支持使用30x代码进行HTTP重定向的rewrite [1, 2]功能。

您可以直接将rewrite添加到应用程序中(web.xmljboss-web.xml),并在rewrite.properties中指定重定向规则-此处未显示。

或者,您可以修改服务器配置而不触及原始应用程序。我认为这个解决方案很方便,因为应用程序保持不变。

用例:我们将其用于EJBCA部署(不是我们的应用程序),它将其上下文根设置为/ejbca。我们希望保留打包的ant脚本提供的默认部署过程,同时我们希望添加从//ejbca的重定向,作为某种默认值,以增加用户友好性。如果用户想要更改它,只需修改standalone.xml即可,无需重新部署整个应用程序。
编辑standalone.xml:
<subsystem xmlns="urn:jboss:domain:web:2.2" default-virtual-server="default-host" native="false">
    <virtual-server name="default-host" enable-welcome-root="true">
        <alias name="localhost"/>
        <rewrite pattern="^/$" substitution="/test" flags="L,QSA,R" />
    </virtual-server>
</subsystem>

这真的很优雅。好的!对我有用(但在我的情况下xmls="urn:jboss:domain:web:2.2")。 - kaleemsagard
1
我已经尝试过这个,但最终发生了异常 - Caused by: java.lang.IllegalStateException: JBAS018038:当虚拟主机配置启用欢迎根时,无法部署根上下文,禁用它并重新部署。 - Krishna
JBoss eAP 7似乎带有子系统urn:jboss:domain:undertow,需要稍微不同的配置。请参见https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.4/html/configuration_guide/configuring_the_web_server_undertow#configure_the_default_welcome_Web_application - Queeg

0

这对我在Wildfly 26.1.1和JBoss EAP 7.4上都有效。

    <subsystem xmlns="urn:jboss:domain:undertow:12.0" default-server="default-server" default-virtual-host="default-host" default-servlet-container="default" default-security-domain="other" statistics-enabled="${wildfly.undertow.statistics-enabled:${wildfly.statistics-enabled:false}}">
        <buffer-cache name="default"/>
        <filters>
            <rewrite name="myfilter" redirect="true" target="/myapp/"/>
        </filters>
        <server name="default-server">
            <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
            <https-listener name="https" socket-binding="https" ssl-context="applicationSSC" enable-http2="true"/>
            <host name="default-host" alias="localhost">
                <location name="/" handler="welcome-content"/>
                <http-invoker http-authentication-factory="application-http-authentication"/>
                <filter-ref name="myfilter" predicate="path('/')"/> 
            </host>
        </server>
        <servlet-container name="default">
            <jsp-config/>
            <websockets/>
        </servlet-container>
        <handlers>
            <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
        </handlers>
        <application-security-domains>
            <application-security-domain name="other" security-domain="ApplicationDomain"/>
        </application-security-domains>
    </subsystem>

看起来很多,但实际上并不是。我的解决方案显示了默认配置,我只需要添加<filters>部分和<filter-ref>

请注意,<filters>部分必须出现在<server>之前,否则JBoss将在启动时抱怨。这在https://access.redhat.com/solutions/2996371中没有记录,但最终我在https://docs.wildfly.org/19/wildscribe/subsystem/undertow/index.html中找到了。

jboss-cli中,这意味着:

/subsystem=undertow/configuration=filter/rewrite=myfilter/:add(redirect=true, target=/myapp)
/subsystem=undertow/server=default-server/host=default-host/filter-ref=myfilter/:add(predicate="path('/')")

自动生成的 standalone.xml 文件结构可能与上述不完全相同,但同样有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接