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>-
Maven プラグインバージョンの場合は、
手順
以下の内容を
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(); } }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)プラグイン
<configuration>要素に以下の設定抽出を追加します。<cli-sessions> <cli-session> <script-files> <script>scripts/authentication.cli</script> </script-files> </cli-session> </cli-sessions>この例は、サーバーに定義されたセキュリティードメインにデフォルトの
undertowセキュリティードメインを設定するauthentication.cliCLI スクリプトを示しています。注記パッケージ化時にではなく、実行時に 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サーブレットを呼び出しますが、認証情報は指定しないでください。
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起動可能な JAR に対して HTTP 認証が有効になっていることを示す HTTP 200 ステータスが返されます。以下に例を示します。
HTTP/1.1 200 OK .... Hello testuser