8.14. CLI スクリプトを使用した起動可能な JAR の HTTP 認証の有効化
CLI スクリプトを使用して、起動可能な JAR の HTTP 認証を有効にできます。このスクリプトは、セキュリティーレルムとセキュリティードメインをサーバーに追加します。
要件
X.Y.Z.Final-redhat-_BUILD_NUMBER
などの最新の Maven プラグインバージョンを確認している。Z と BUILD_NUMBER は、JBoss EAP XP 2.0.0 製品のライフサイクル中に進化できます。- X は、Maven プラグインのメジャーバージョンです。
- y は、Maven プラグインのマイナーバージョンです。
- z は、Maven プラグインのマイクロバージョンです。/ga/org/wildfly/plugins/wildfly-jar-maven-plugin のインデックス を参照してください。
-
2.0.X.GA-redhat-BUILD_NUMBER
などの最新の Galleon feature-pack バージョンを確認している。X は JBoss EAP XP 2 のマイクロバージョンで、BUILD_NUMBER は Galleon 機能パックのビルド番号。X と BUILD_NUMBER は、JBoss EAP XP 2.0.0 製品のライフサイクル中に進化できます。Index of /ga/org/jboss/eap/wildfly-galleon-pack のインデックス を参照してください。 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}
です。 -
Galleon 機能パックバージョンの場合は、
${jboss.xp.galleon.feature.pack.version}
です。
これらのプロパティーをプロジェクトで設定する必要があります。以下に例を示します。
<properties> <bootable.jar.maven.plugin.version>2.0.2.Final-redhat-00001</bootable.jar.maven.plugin.version> <jboss.xp.galleon.feature.pack.version>2.0.0.GA-redhat-00002</jboss.xp.galleon.feature.pack.version> </properties>
-
Maven プラグインバージョンの場合は、
手順
以下の内容を
pom.xml
ファイルの<build>
要素に追加します。最新バージョンの Maven プラグインと、org.jboss.eap:wildfly-galleon-pack
Galleon 機能パックの最新バージョンを指定する必要があります。以下に例を示します。<plugins> <plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-jar-maven-plugin</artifactId> <version>${bootable.jar.maven.plugin.version}</version> <configuration> <feature-pack-location>org.jboss.eap:wildfly-galleon-pack:${jboss.xp.galleon.feature.pack.version}</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 レイヤーが含まれていました。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 javax.servlet.annotation.HttpMethodConstraint; import javax.servlet.annotation.ServletSecurity; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.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.cli
CLI スクリプトを示しています。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
以下の
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
サーブレットを呼び出しますが、認証情報は指定しないでください。
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
関連情報
-
undertow
セキュリティードメインの HTTP 認証を有効にする方法は、サーバーセキュリティーの設定方法の CLI セキュリティーコマンドを使用したアプリケーションの HTTP 認証の有効化 を参照してください。