【问题标题】:HTTP basic authentication for JAX-RS without web.xml没有 web.xml 的 JAX-RS 的 HTTP 基本身份验证
【发布时间】:2019-12-27 03:52:27
【问题描述】:

我正在运行在 JBoss EAP 7.1 上的 EAR 中的 EJB-JAR 中实现 REST 服务。

该服务的不安全版本运行良好,但即使添加基本的 HTTP 身份验证也是一项挑战,因为在 EJB-JAR 中我发现无法指定任何所需的 web.xml 条目,例如 <auth-method>BASIC</auth-method>

所以我的问题是:

如何配置 JAX-RS 以在 EJB-JAR 中使用 HTTP 身份验证?

附加信息:

  • 为了让事情更简单,我使用默认的 ManagementRealm 像这样

    <security-domain name="my-security-domain" cache-type="default">
        <authentication>
            <login-module code="Remoting" flag="optional">
                <module-option name="password-stacking" value="useFirstPass"/>
            </login-module>
            <login-module code="RealmDirect" flag="required">
                <module-option name="realm" value="ManagementRealm"/>
            </login-module>
        </authentication>
    </security-domain>
    
  • 在 EJB-jar 中:

    @Stateless
    @Path("/my-rest")
    @SecurityDomain(value = "my-security-domain")
    @DenyAll
    public class MyRestStatelessBean {
            @PUT
            @RolesAllowed("admin")
            @Path("/doAdminStuff")
            public void doAdminStuff() {
                // Implementation
            }
    }
    

【问题讨论】:

  • 一个 EAR 文件可以包含一个或多个 WAR 文件,并且 WAR 文件应该能够有一个 web.xml 可以指定它的安全要求。你是怎么打包的?
  • 我将我的 ejb jar 打包到 ear 文件中,并且我想避免将我的 REST bean 从 ejb-jar 移动到 WAR。这就是问题所在。

标签: jax-rs wildfly basic-authentication jboss-eap-7


【解决方案1】:

解决方案是使用 Undertow 的 Proactive authentication 功能,默认情况下实际上是开启的。在请求中指定 HTTP-BASIC-Authentication 标头,由于缺少 web.xml,Undertow 甚至通过我的 REST 服务尝试登录用户不需要任何类型的身份验证。

我的完整配置(使用来自mgmt-users.properties管理 JBoss 用户):

# Define my security domain
/subsystem=security/security-domain=MY-SECURITY-DOMAIN:add(cache-type=default)

# Link Untertow to Elytron for authentication
/subsystem=undertow/application-security-domain=MY-SECURITY-DOMAIN:add(   \
           http-authentication-factory="management-http-authentication"   \
)

# Add BASIC-HTTP-Authentication support to Elytron
/subsystem=elytron/http-authentication-factory=management-http-authentication:list-add( \
           name=mechanism-configurations,                                               \
           value={mechanism-name="BASIC",                                               \
               mechanism-realm-configurations=[{realm-name="ManagementRealm"}]          \
           }                                                                            \
)

# Not sure, why is this required...
/subsystem=ejb3/application-security-domain=MY-SECURITY-DOMAIN:add(  \
           security-domain="ManagementDomain")

【讨论】:

    猜你喜欢
    • 2016-06-08
    • 2010-12-28
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2019-05-09
    • 2015-03-02
    • 2018-01-24
    • 2023-03-07
    相关资源
    最近更新 更多