11.15. 使用 CLI 脚本为可引导 JAR 启用 HTTP 身份验证
您可以使用 CLI 脚本为可引导 JAR 启用 HTTP 身份验证。此脚本会在您的服务器中添加安全域和一个安全域。
先决条件
-
您已检查了最新的 Maven 插件版本,如
9.minor.micro.Final-redhat-XXXXX,其中 9 是主版本,次版本 为 微型 版本,X 是 Red Hat 构建号。例如:9.0.1.Final-redhat-00009。 您已创建了 Maven 项目,并添加用于创建需要 HTTP 身份验证的应用程序的依赖项。请参阅 创建可引导 JAR Maven 项目。
重要在设置 Maven 项目时,您必须在 Maven archetype 配置中指定 HTTP 身份验证值。例如:
$ mvn archetype:generate \ -DgroupId=com.example.auth \ -DartifactId=authentication \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-webapp \ -DinteractiveMode=false cd authentication注意流程中显示的示例指定以下属性:
-
${bootable.jar.maven.plugin.version}用于 Maven 插件版本。
您必须在项目中设置这些属性。例如:
<properties> <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version> </properties>-
流程
在
pom.xml文件的<build> 元素中添加以下内容。例如:<plugins> <plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-jar-maven-plugin</artifactId> <version>${bootable.jar.maven.plugin.version}</version> <configuration> <channels> <channel> <manifest> <groupId>org.jboss.eap.channels</groupId> <artifactId>eap-8.0</artifactId> </manifest> </channel> <channel> <manifest> <groupId>org.jboss.eap.channels</groupId> <artifactId>eap-xp-5.0</artifactId> </manifest> </channel> </channels> <feature-pack-location>org.jboss.eap.xp:wildfly-galleon-pack</feature-pack-location> <layers> <layer>datasources-web-server</layer> </layers> </configuration> <executions> <execution> <goals> <goal>package</goal> </goals> </execution> </executions> </plugin> </plugins>示例显示包含包含
elytron子系统的datasources-web-serverGalleon 层。更新
src/main/webapp/WEB-INF目录中的web.xml文件。例如:<?xml version="1.0" encoding="UTF-8"?> <web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"> <login-config> <auth-method>BASIC</auth-method> <realm-name>Example Realm</realm-name> </login-config> </web-app>创建用于存储 Java 文件的目录:
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/authentication/其中
APPLICATION_ROOT是 Maven 项目的根目录。创建包含以下内容的 Java 文件
TestServlet.java,并将该文件保存到APPLICATION_ROOT/src/main/java/com/example/authentication/目录中。package com.example.authentication; import jakarta.servlet.annotation.HttpMethodConstraint; import jakarta.servlet.annotation.ServletSecurity; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet(urlPatterns = "/hello") @ServletSecurity(httpMethodConstraints = { @HttpMethodConstraint(value = "GET", rolesAllowed = { "Users" }) }) public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { PrintWriter writer = resp.getWriter(); writer.println("Hello " + req.getUserPrincipal().getName()); writer.close(); } }创建一个 CLI 脚本,如
authentication.cli,并将它保存到可引导 JAR 的可访问目录中,如APPLICATION_ROOT/scripts目录。该脚本必须包含以下命令:/subsystem=elytron/properties-realm=bootable-realm:add(users-properties={relative-to=jboss.server.config.dir, path=bootable-users.properties, plain-text=true}, groups-properties={relative-to=jboss.server.config.dir, path=bootable-groups.properties}) /subsystem=elytron/security-domain=BootableDomain:add(default-realm=bootable-realm, permission-mapper=default-permission-mapper, realms=[{realm=bootable-realm, role-decoder=groups-to-roles}]) /subsystem=undertow/application-security-domain=other:write-attribute(name=security-domain, value=BootableDomain)在插件 <configuration> 元素
中添加以下配置提取:<cli-sessions> <cli-session> <script-files> <script>scripts/authentication.cli</script> </script-files> </cli-session> </cli-sessions>本例演示了
authentication.cliCLI 脚本,它将默认undertow安全域配置为为您的服务器定义的安全域。注意您可以选择在运行时执行 CLI 脚本,而不是打包时间。为此,请跳过这一步,然后继续下一步 10。
在 Maven 项目的根目录中,创建一个目录来存储 JBoss EAP JAR Maven 插件添加到可引导 JAR 中的属性文件:
$ mkdir -p APPLICATION_ROOT/extra-content/standalone/configuration/其中
APPLICATION_ROOT是包含应用的pom.xml配置文件的目录。此目录存储文件,如
bootable-users.properties和bootable-groups.properties文件。bootable-users.properties文件包含以下内容:testuser=bootable_passwordbootable-groups.properties文件包含以下内容:testuser=Users将以下
extra-content-content-dirs元素添加到现有 <configuration>元素中:<extra-server-content-dirs> <extra-content>extra-content</extra-content> </extra-server-content-dirs>extra-content目录包含属性文件。将应用打包为可引导 JAR。
$ mvn package启动应用程序:
mvn wildfly-jar:run如果您选择了跳过第 6 步,且在构建期间没有执行 CLI 脚本,使用以下命令启动应用程序:
mvn wildfly-jar:run -Dwildfly.bootable.arguments=--cli-script=scripts/authentication.cli调用 servlet,但不指定凭证:
curl -v http://localhost:8080/hello预期输出:
HTTP/1.1 401 Unauthorized ... WWW-Authenticate: Basic realm="Example Realm"调用服务器并指定您的凭证。例如:
$ curl -v -u testuser:bootable_password http://localhost:8080/hello返回 HTTP 200 状态,表示为可引导 JAR 启用了 HTTP 身份验证。例如:
HTTP/1.1 200 OK .... Hello testuser