11.15. CLI スクリプトを使用した起動可能な JAR の HTTP 認証の有効化


CLI スクリプトを使用して、起動可能な JAR の HTTP 認証を有効にできます。このスクリプトは、セキュリティーレルムとセキュリティードメインをサーバーに追加します。

前提条件

  • 9.minor.micro.Final-redhat-XXXXX などの最新の Maven プラグインを確認した。この場合の 9 はメジャーバージョン、minor はマイナーバージョン、micro はマイクロバージョン、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
    注記

    この手順の例では、以下のプロパティーを指定します。

    • Maven プラグインバージョンの場合は、${bootable.jar.maven.plugin.version} です。

    これらのプロパティーをプロジェクトで設定する必要があります。以下に例を示します。

    <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. authentication.cli などの CLI スクリプトを作成し、これを APPLICATION_ROOT/scripts ディレクトリーなどの起動可能な JAR のアクセス可能なディレクトリーに保存します。スクリプトには以下のコマンドが含まれている必要があります。

    /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>

    この例は、サーバーに定義されたセキュリティードメインにデフォルトの undertow セキュリティードメインを設定する authentication.cli CLI スクリプトを示しています。

    注記

    パッケージ化時にではなく、実行時に CLI スクリプトを実行するオプションがあります。これを行うには、この手順をスキップして手順 10 に進みます。

  7. 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_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. サーブレットを呼び出しますが、認証情報は指定しないでください。

    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

    起動可能な JAR に対して HTTP 認証が有効になっていることを示す HTTP 200 ステータスが返されます。以下に例を示します。

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

詳細情報

試用、購入および販売

コミュニティー

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

Red Hat ドキュメントについて

Legal Notice

Theme

© 2026 Red Hat
トップに戻る