JSF从HTTPS重定向到HTTP

问题描述:

我的测试服务器上的应用程序是通过https专门执行的。当我在没有重定向的情况下导航时,它完美地运行:

I have my application on a test server being executed exclusively over https. When I navigate without redirecting, it works perfectly:

示例:

<p:menuitem value="#{msg.customerScreen}" url="/restrict/customer.xhtml" />
<p:menuitem value="#{msg.productScreen}" url="/restrict/product.xhtml" />

但是当我需要重定向到另一个页面时,它会重定向到http而不是https。使用over http时,它完美运行:

But when I need to redirect to another page, it is redirecting to http instead of https. When using over http, it works perfectly:

<p:commandLink ajax="false" action="/commerce/store.xhtml?faces-redirect=true">
    <h:graphicImage library="images/BTN" name="btn_to_shop.gif"/>
</p:commandLink>

作为解决方法,我尝试重建网址:

As a workaround I tryed to reconstruct the URL:

<p:commandLink ajax="false" action="#{authorizerBean.getCompleteURL('/commerce/store.xhtml?faces-redirect=true')}">
    <h:graphicImage library="images/BTN" name="btn_to_shop.gif"/>
</p:commandLink>

public String getCompleteURL(String page) {
    try {
        FacesContext ctxt = FacesContext.getCurrentInstance();
        ExternalContext ext = ctxt.getExternalContext();

        URI uri = new URI(ext.getRequestScheme(), null, ext.getRequestServerName(), ext.getRequestServerPort(), ext.getRequestContextPath(), null, null);
        return uri.toASCIIString() + page;
    } catch (URISyntaxException e) {
        throw new FacesException(e);
    }
}

正在调用方法getCompleteURL并返回URL correclty ,但JSF没有重定向到新的URL。

The method getCompleteURL is being called and returning the URL correclty, but JSF is not redirecting to the new URL.

JBoss正在接收HTTP连接。谁管理HTTPS是Apache,重定向到JBoss:

JBoss is receiving an HTTP connection. Who manages the HTTPS is Apache, that redirects to JBoss:

<VirtualHost *:443>

    ...

    ProxyPass / http://server:8080/
    ProxyPassReverse / http://server:8080/
</VirtualHost>

我更愿意在不使用getCompleteURL的情况下解决此问题,但如果无法解决此问题,请帮助我用其他方法。

I would prefer to solve this issue without using the getCompleteURL, but if it is not possible, please help me with other approaches.

我找到了解决这个问题的方法。我认为它正在发生,因为Apache接收https连接并通过http转发JBoss。然后当我重定向到另一个页面时,JSF不知道它应该通过https进行。

I found a solution for this issue. I think it is happening because Apache receives the https connection and forwards for JBoss via http. Then when I redirect to another page, JSF doesn't know that it should be made via https.

使用ConfigurableNavigationHandler我可以在重定向时拦截并挂载正确的URL。

With a ConfigurableNavigationHandler I can intercept when it is redirected and mount the correct URL.

public class NavigationHandler extends ConfigurableNavigationHandler {

    private ConfigurableNavigationHandler concreteHandler;

    public NavigationHandler(ConfigurableNavigationHandler concreteHandler) {
        this.concreteHandler = concreteHandler;
    }

    @Override
    public void handleNavigation(FacesContext context, String fromAction, String outcome) {
        if (outcome != null && outcome.contains("faces-redirect=true")) {
            try {
                outcome = "https://server.com/project" + outcome;
                context.getExternalContext().redirect( outcome );
            } catch (IOException e) {
                throw new FacesException(e);
            }
        } else {
            concreteHandler.handleNavigation(context, fromAction, outcome);   
        }
    }
}

faces-config.xml中的

in faces-config.xml:

<application>
    <navigation-handler>com.example.NavigationHandler</navigation-handler>
</application>