【问题标题】:How to make "HTTPS redirect" work on WebSphere Application Server Liberty Profile?如何使“HTTPS 重定向”在 WebSphere Application Server Liberty Profile 上工作?
【发布时间】:2013-07-01 03:57:49
【问题描述】:

我想让 HTTP 重定向在 WebSphere Application Server Liberty Profile (WLP) 上工作。例如:-

当用户键入时: http://localhost:8080/helloworld,浏览器应该自动转到(被重定向)到 https://localhost:9443/helloworld

为了实现这一点,我遵循了document,第 6.2 节,第 1 页。 136.

下面是示例 server.xml 和 web.xml:-

server.xml

<server description="new server">

<!-- Enable features -->
<featureManager>
    <feature>jsp-2.2</feature>
    <feature>wab-1.0</feature>
    <feature>jaxrs-1.1</feature>
    <feature>blueprint-1.0</feature>
    <feature>localConnector-1.0</feature>
    <feature>ssl-1.0</feature>
    <feature>appSecurity-2.0</feature>
</featureManager>

<httpEndpoint host="localhost" httpPort="8081" httpsPort="9442" id="defaultHttpEndpoint">
</httpEndpoint>

<applicationMonitor updateTrigger="mbean"/>
<keyStore id="defaultKeyStore" password="{xor}Lz4sLCgwLTtu"/>

<application id="Hello.app" location="Hello.app.eba" name="Hello.app" type="eba"/>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>Hello</display-name>

    <security-constraint>
        <display-name>HTTPS Redirect Security Constraint</display-name>
        <web-resource-collection>
            <web-resource-name>Sample Web Service service</web-resource-name>
            <url-pattern>/Hello</url-pattern>
            <http-method>GET</http-method>

        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
</web-app>

为简洁起见,删除了 &lt;servlet&gt;&lt;servlet-mapping&gt; 标记。

以下是我正在使用的版本:- Java 7、WLP 8.5.5、Eclipse Juno、谷歌浏览器。

任何有关 HTTPS 重定向为何不起作用的帮助和指南将不胜感激。

【问题讨论】:

    标签: ssl https websphere websphere-8 websphere-liberty


    【解决方案1】:

    我怀疑问题出在您的安全限制上。看着它,我建议将您的 url-pattern 更改为:

    /你好世界

    而不是:

    /你好

    如果要匹配多个资源,可以使用通配符,例如:

    1. /* - 匹配所有内容
    2. /helloworld/* - 匹配 url 路径中包含 helloworld/ 的所有内容
    3. *.jsp - 匹配所有带有 jsp 扩展名的文件

    【讨论】:

    • 感谢您的帮助。但真正的问题似乎是我错过了一个重要的 标签。验证我的理论后将发布答案。
    • @Anuroop 不幸的是,您的理论是错误的,因为您不需要 auth-constraint 仅用于 https 重定向。您也不需要解决方案中的第 1 步和第 2 步,也不需要登录配置。因为重定向根本不需要登录。
    【解决方案2】:

    要使 HTTPS 重定向在 WLP 上工作,应注意以下几点:-

    1. 在 WLP 的server.xml 中添加用户、角色和密码。
    2. 将应用程序绑定到安全角色。
    3. 在 WLP 的 server.xml 中添加 appSecurity-2.0 功能。
    4. web.xml中添加以下标签
      1. &lt;login-config&gt;
      2. &lt;security-constraint&gt;
      3. &lt;security-constraint&gt;&lt;web-resource-name&gt;&lt;/security-constraint&gt;
      4. &lt;security-constraint&gt;&lt;auth-constraint&gt;&lt;/security-constraint&gt;
      5. &lt;security-constraint&gt;&lt;user-data-constraint&gt;&lt;/security-constraint&gt;

    以下是详细步骤:-

    1.在 WLP 的server.xml 中添加用户、角色和密码。

    <basicRegistry id="MyRegistry">
        <user password="{xor}Mjo6MT4z" name="anuroop" />
        <group name="MyGroup">
            <member name="anuroop" />
        </group>
    </basicRegistry>
    

    2。将应用程序绑定到安全角色。

    <application id="Hello.app" location="Hello.app.eba" name="Hello.app" type="eba">
        <application-bnd>
            <security-role name="Manager">
            <group name="MyGroup" />
        </security-role>
        </application-bnd>
    </application>
    

    3.在 WLP 的server.xml 中添加 appSecurity-2.0 功能。

    <featureManager>
        <feature>appSecurity-2.0</feature>
    </featureManager>
    

    4.1、4.2、4.3、4.4、4.5

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>BasicRegistry</realm-name>
        <form-login-config>
            <form-login-page>/Login.jsp</form-login-page>
            <form-error-page>/LoginError.jsp</form-error-page>
        </form-login-config>
    </login-config>
    
    <security-constraint>
    
            <display-name>HTTPS Redirect Security Constraint</display-name>
            <web-resource-collection>
                <web-resource-name>Sample Web Service service</web-resource-name>
                <url-pattern>/Hello</url-pattern>
                <http-method>GET</http-method>
            </web-resource-collection>
    
        <auth-constraint>
            <role-name>Manager</role-name>
        </auth-constraint>
    
        <user-data-constraint>
        <description>Ensure to allow only confidential communication</description>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    
    </security-constraint>
    

    【讨论】:

    • 似乎是 WLP 中的一个错误。您不应该只需要用户来执行重定向。
    • 除了 WebSphere Classic 9,当只使用安全约束时,它还需要登录,因此它不允许匿名访问。似乎支持匿名 websphere 重定向的最安全方法是通过@Vince Thyng 指出的过滤器
    【解决方案3】:

    我以不同的方式解决了这个问题,但我认为接受的答案可能会更好。您可以编写一个 servlet 过滤器,然后修改 web.xml 以将其与路径相关联。

    web.xml 代码:

      <web-app id="WebApp">
          <filter>
            <filter-name>HTTPSFilter</filter-name>
            <filter-class>
            HTTPSFilter
            </filter-class>
        </filter>
        <filter-mapping>
            <filter-name>HTTPSFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        ...
     </web-app>
    

    过滤代码:

    public class HTTPSFilter implements Filter {
        public void doFilter(ServletRequest req, 
                             ServletResponse res,
                             FilterChain chain) throws IOException, ServletException {
    
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
    
            // Forward to HTTPS if insecure HTTP was used
            if(!req.getScheme().startsWith("https")) {
                // Modify the Response object to be the SSL version of the URL
                String host         = request.getLocalName();
    
                String URI         = request.getRequestURI();
                if(URI == null) { URI = ""; }
    
                String queryString  = request.getQueryString();
                if(queryString == null) { queryString = ""; }
    
                response.sendRedirect("https://" + host + ":9443" + URI + ("".equalsIgnoreCase(queryString) ? "":"?") + queryString);
            }
    
            chain.doFilter(req, res);
        }
        public void init(FilterConfig config) throws ServletException {
        }
        public void destroy() {
        }
    }
    

    【讨论】:

    • 使用 web.xml 是正确的方式,因为它适用于任何应用服务器。你正在手工做容器应该做的事情。此外,您对 https 端口进行硬编码,因此如果有人需要更改默认端口,您的过滤器将失败。
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 2012-08-21
    • 1970-01-01
    • 2018-07-07
    • 2020-12-15
    • 2017-01-21
    • 2014-12-06
    • 1970-01-01
    相关资源
    最近更新 更多