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>

流程

  1. 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-server Galleon 层。

  2. 更新 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>
  3. 创建用于存储 Java 文件的目录:

    $ mkdir -p APPLICATION_ROOT/src/main/java/com/example/authentication/

    其中 APPLICATION_ROOT 是 Maven 项目的根目录。

  4. 创建包含以下内容的 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();
        }
    
    }
  5. 创建一个 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)
  6. 在插件 <configuration> 元素 中添加以下 配置提取:

    <cli-sessions>
        <cli-session>
            <script-files>
                <script>scripts/authentication.cli</script>
            </script-files>
        </cli-session>
    </cli-sessions>

    本例演示了 authentication.cli CLI 脚本,它将默认 undertow 安全域配置为为您的服务器定义的安全域。

    注意

    您可以选择在运行时执行 CLI 脚本,而不是打包时间。为此,请跳过这一步,然后继续下一步 10。

  7. 在 Maven 项目的根目录中,创建一个目录来存储 JBoss EAP JAR Maven 插件添加到可引导 JAR 中的属性文件:

    $ mkdir -p APPLICATION_ROOT/extra-content/standalone/configuration/

    其中 APPLICATION_ROOT 是包含应用的 pom.xml 配置文件的目录。

    此目录存储文件,如 bootable-users.propertiesbootable-groups.properties 文件。

    bootable-users.properties 文件包含以下内容:

    testuser=bootable_password

    bootable-groups.properties 文件包含以下内容:

    testuser=Users
  8. 将以下 extra-content-content-dirs 元素添加到现有 < configuration> 元素中:

    <extra-server-content-dirs>
                <extra-content>extra-content</extra-content>
    </extra-server-content-dirs>

    extra-content 目录包含属性文件。

  9. 将应用打包为可引导 JAR。

    $ mvn package
  10. 启动应用程序:

    mvn wildfly-jar:run

    如果您选择了跳过第 6 步,且在构建期间没有执行 CLI 脚本,使用以下命令启动应用程序:

    mvn wildfly-jar:run -Dwildfly.bootable.arguments=--cli-script=scripts/authentication.cli
  11. 调用 servlet,但不指定凭证:

    curl -v http://localhost:8080/hello

    预期输出:

    HTTP/1.1 401 Unauthorized
    ...
    WWW-Authenticate: Basic realm="Example Realm"
  12. 调用服务器并指定您的凭证。例如:

    $ curl -v -u testuser:bootable_password http://localhost:8080/hello

    返回 HTTP 200 状态,表示为可引导 JAR 启用了 HTTP 身份验证。例如:

    HTTP/1.1 200 OK
    ....
    Hello testuser
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部