Wildfly 上 ManagementRealm 中的 webapp

webapp in ManagementRealm on Wildfly

我正在编写要部署在 Wildfly 上的管理 Web 应用程序。 它将由有权访问管理控制台 (http://localhost:9990/) 的相同用户使用。 如果我可以声明我的应用程序应该在 ManagementRealm 中使用 HTTP Basic 身份验证,就像控制台一样,那就太好了。

天真乐观的尝试没有奏效:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Admin Panel</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>ManagementRealm</realm-name>
    </login-config>
</web-app>

这根本不会触发 HTTP Basic 登录对话框。 有什么简单的方法可以将我的应用程序插入 ManagementRealm?

Wildfly 不会遵循安全约束,除非您将其绑定到安全角色:

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

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Admin Panel</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>ManagementRealm</realm-name>
    </login-config>

    <security-role>
        <role-name>*</role-name>
    </security-role>
</web-app>

这将加载基本身份验证,但随后您会遇到问题,ManagementRealm 仅绑定到 standalone.xml 中的管理端口,因此您必须更改它。您可能需要删除 ApplicationRealm 以免发生冲突。

    <management-interfaces>
        <http-interface security-realm="ManagementRealm" http-upgrade-enabled="true">
            <socket-binding http="management-http"/>
        </http-interface>
    </management-interfaces>

我发现我需要创建一个与 ManagementRealm 链接的安全域。配置分布在三个地方:

1) 需要添加一个新的安全域,使用 RealmDirect 登录模块委托给 ManagementRealm

<subsystem xmlns="urn:jboss:domain:security:1.2">
    <security-domains>
        ....
        <security-domain name="management" cache-type="default">
            <authentication>
                <login-module code="RealmDirect" flag="required">
                    <module-option name="realm" value="ManagementRealm"/>
                </login-module>
            </authentication>
        </security-domain>

这可以通过 jboss-cli:

完成
/subsystem=security/security-domain=management:add(cache-type=default)
/subsystem=security/security-domain=management/authentication=classic:add(\
    login-modules=[{\
        "code"=>"RealmDirect", "flag"=>"required", \
        "module-options"=>[("realm"=>"ManagementRealm")]\
    }])

2) 应用程序需要使用 WEB-INF/jboss-web.xml:

引用此安全域
<jboss-web>
    <security-domain>management</security-domain>
</jboss-web>

3) 比直接 web.xml 打开 HTTP 基本登录对话框:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <security-role>
        <role-name>*</role-name>
    </security-role>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Admin Panel</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>[message show in login dialog]</realm-name>
    </login-config>
</web-app>