OpenLiberty 忽略 WEB-INF/ibm-application-bnd.xml

OpenLiberty ignoring WEB-INF/ibm-application-bnd.xml

我的应用程序 (my-app.war) 有一个 JAX-RS 端点,由给定角色保护,如下所示:

Endpoint.java

@Path("endpoint/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@DeclareRoles({"ADMIN_ROLE"})
public class Endpoint {
  @POST
  @RolesAllowed({"ADMIN_ROLE"})
  public void post() {
  }
}

server.xml

<server>
  <featureManager>
    <feature>jaxrs-2.1</feature>
    <feature>appSecurity-3.0</feature>
  </featureManager>

  <application id="my-app" context-root="/" location="my-app.war" type="war">
  </application>

  <variable name="default.http.port" defaultValue="9080"/>
  <variable name="default.https.port" defaultValue="9443"/>

  <httpEndpoint id="defaultHttpEndpoint" httpPort="${default.http.port}" httpsPort="${default.https.port}"/>

  <basicRegistry realm="defaultRealm">
    <user name="test-user" password="test-password"/>
    <group name="ADMIN_GROUP">
      <member name="test-user"/>
    </group>
  </basicRegistry>

</server>

现在我必须将 ADMIN_GROUP 绑定到 ADMIN_ROLE

如果我在 server.xml application 上执行此操作,那么一切正常:

  <application id="my-app" context-root="/" location="my-app.war" type="war">
    <application-bnd>
      <security-role name="ADMIN_ROLE">
        <group name="ADMIN_GROUP" />
      </security-role>
    </application-bnd>
  </application>

应用程序已部署,并且在未经身份验证的情况下调用端点时,它 returns 403(需要身份验证)。当提供身份验证 header 时,它会按预期 returns 204(无内容)。

现在问题出在我尝试在 ibm-application-bnd.xml 中配置绑定时:

<?xml version="1.0" encoding="UTF-8"?>

<application-bnd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-application-bnd_1_2.xsd"
  xmlns="http://websphere.ibm.com/xml/ns/javaee"
  version="1.2">
<
  <security-role name="ADMIN_ROLE">
    <group name="ADMIN_GROUP" />
  </security-role>
</application-bnd>

我把它都放在 META-INF/ibm-application-bnd.xmlWEB-INF/ibm-application-bnd.xml 中了。但是它们都被完全忽略了(我什至在那里添加了 invalid xml,但没有从 OpenLiberty 收到错误消息)。

每次调用端点 returns 403,即使提供了身份验证。

https://www.ibm.com/docs/en/was-liberty/base?topic=liberty-configuring-authorization-applications-in 中,它表示 [已添加强调]:

Configure the authorization information such as the user and group to role mapping. You can configure the authorization table in the following ways:

  • If you have an EAR file, you can add the authorization configuration definition to the ibm-application-bnd.xml or ibm-application-bnd.xmi file.

  • If you have standalone WAR files, you can add the authorization table definitions to the server.xml file under the respective application element. You can use the WebSphere® Application Server Developer Tools for Eclipse to do this.

这是否意味着对于 war 应用程序,绑定 必须 在 server.xml 上?

根据 this answerMETA-INF/ibm-application-bnd.xml - 虽然未记录 - 可以在网络存档中使用。

然后我 mvn package 我的项目,并检查了它的 war 文件(仅显示相关文件):

  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2021-04-28 22:13   META-INF/
      131  2021-04-28 22:13   META-INF/MANIFEST.MF
        0  2021-04-28 22:13   WEB-INF/
        0  2021-04-28 22:13   WEB-INF/classes/
        0  2021-04-28 22:13   WEB-INF/classes/META-INF/
        0  2021-04-28 22:13   WEB-INF/lib/
        0  2021-04-22 17:58   WEB-INF/beans.xml
      134  2021-04-28 22:13   WEB-INF/classes/META-INF/ibm-application-bnd.xml
      592  2021-04-28 22:13   WEB-INF/classes/META-INF/persistence.xml
   300365  2021-04-15 09:58   WEB-INF/lib/log4j-api-2.14.1.jar
  1745700  2021-04-15 09:59   WEB-INF/lib/log4j-core-2.14.1.jar
      393  2021-04-28 21:50   WEB-INF/web.xml
        0  2021-04-28 22:13   META-INF/maven/
---------                     -------

现在请注意,有 META-INF/* 个条目和 WEB-INF/classes/META-INF/* 个条目。

然后我找到了 (与 liberty/openliberty 无关)。它的第一句话让我敬畏:

It looks like your META-INF folder is in src/main/resources directory.

跟我的一模一样。所以我将 META-INFsrc/main/resources 移动到 src/main/webapp.

现在 war 列表为:

  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2021-04-28 22:22   META-INF/
      131  2021-04-28 22:22   META-INF/MANIFEST.MF
        0  2021-04-28 22:22   WEB-INF/
        0  2021-04-28 22:22   WEB-INF/classes/
        0  2021-04-28 22:22   WEB-INF/lib/
      134  2021-04-28 22:13   META-INF/ibm-application-bnd.xml
      592  2021-04-27 10:19   META-INF/persistence.xml
        0  2021-04-22 17:58   WEB-INF/beans.xml
   300365  2021-04-15 09:58   WEB-INF/lib/log4j-api-2.14.1.jar
  1745700  2021-04-15 09:59   WEB-INF/lib/log4j-core-2.14.1.jar
      393  2021-04-28 21:50   WEB-INF/web.xml
        0  2021-04-28 22:22   META-INF/maven/
---------                     -------

现在文件 ibm-application-bnd.xml 得到处理。 并且有效。除了...

这样 JPA 就不再工作了。

看来META-INF/persistence.xml毕竟必须在src/main/resources

现在我有两个 META-INF/ 个文件夹 (¯\_(ツ)_/¯)。


更新

除了有两个 META-INF/ 文件夹(这本身就是一个问题),现在我的测试无法从 META-INF/ 访问资源(另一个在 WEB-INF/classes/META-INF 中很容易可用,因为 WEB-INF/classes/).

中的每个资源

那么您可能会问,为什么我需要从测试中访问 ibm-application-bnd.xml。因为我使用的是 arquillian,我需要创建一个部署,我需要包含这些角色 <-> 组映射。

现在我有 3 个选择:

  1. src/test/resources 中创建 ibm-application-bnd.xml 的副本;这里的问题是我需要保持两者同步;

  2. src/main/webapps 添加到 surefire/failsafe 类路径(参见 );

  3. 特别是 Liberty/OpenLiberty,我可以依赖隐式映射(假设每个组都绑定到同名的角色;去寻找 CWWKS2104I here)。这是我暂时选择的。

选项 1 和 3 有相同的缺点:测试代码并不完全是将要部署到生产环境的代码。

选项 2 还有另一个缺点:当 运行 通过 IDE 测试时,这个额外的文件夹也必须添加到类路径中(除非 IDE 与maven 并且可以读取 surefire/failsafe 配置)。