2.2. セキュリティードメインを使用したアプリケーションユーザーの認証と認可
セキュリティーレルムを参照するセキュリティードメインを使用して、アプリケーションユーザーを認証および認可します。アプリケーションを開発するための手順は、例としてのみ提供されています。
2.2.1. 簡単な Web アプリケーションの開発 リンクのコピーリンクがクリップボードにコピーされました!
簡単な Web アプリケーションを作成して、セキュリティーレルムの設定例に従うことができます。
以下の手順は例としてのみ提供されています。保護する必要があるアプリケーションがすでにある場合は、以下の手順をスキップして、アプリケーションへの認証と認可の追加 に直接進むことができます。
2.2.1.1. web-application 開発用の Maven プロジェクトの作成 リンクのコピーリンクがクリップボードにコピーされました!
web-application を作成するには、必要な依存関係とディレクトリー構造で Maven プロジェクトを作成します。
以下の手順は一例として提示されています。実稼働環境では使用しないでください。JBoss EAP のアプリケーション作成に関する詳細は、JBoss EAP にデプロイするアプリケーションの開発のスタートガイド を参照してください。
前提条件
- Maven がインストールされている。詳細は、Downloading Apache Maven を参照してください。
手順
mvnコマンドを使用して Maven プロジェクトを設定します。このコマンドは、プロジェクトのディレクトリー構造とpom.xml設定ファイルを作成します。構文
$ mvn archetype:generate \ -DgroupId=${group-to-which-your-application-belongs} \ -DartifactId=${name-of-your-application} \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-webapp \ -DinteractiveMode=false例
$ mvn archetype:generate \ -DgroupId=com.example.app \ -DartifactId=simple-webapp-example \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-webapp \ -DinteractiveMode=falseアプリケーションのルートディレクトリーに移動します。
構文
$ cd <name-of-your-application>例
$ cd simple-webapp-example生成された
pom.xmlファイルの内容を、以下のテキストに置き換えます。<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.app</groupId> <artifactId>simple-webapp-example</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>simple-webapp-example Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <version.maven.war.plugin>3.4.0</version.maven.war.plugin> </properties> <dependencies> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>${version.maven.war.plugin}</version> </plugin> <plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>4.2.2.Final</version> </plugin> </plugins> </build> </project>
検証
アプリケーションのルートディレクトリーで、次のコマンドを入力します。
$ mvn install次のような出力が得られます。
... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.795 s [INFO] Finished at: 2022-04-28T17:39:48+05:30 [INFO] ------------------------------------------------------------------------
web-application を作成できるようになりました。
2.2.1.2. Web アプリケーションの作成 リンクのコピーリンクがクリップボードにコピーされました!
ログインユーザーのプリンシパルから取得したユーザー名を返すサーブレットを含む Web アプリケーションを作成します。ログインしているユーザーがないと、サーブレットは「NO AUTHENTICATED USER」のテキストを返します。
この手順では、<application_home> は、アプリケーションの pom.xml 設定ファイルが含まれるディレクトリーを参照します。
前提条件
Maven プロジェクトを作成している。
詳細は、Web アプリケーション開発用の Maven プロジェクトの作成 を参照してください。
- JBoss EAP が実行中である。
手順
Java ファイルを保存するディレクトリーを作成します。
構文
$ mkdir -p src/main/java/<path_based_on_artifactID>例
$ mkdir -p src/main/java/com/example/app新しいディレクトリーに移動します。
構文
$ cd src/main/java/<path_based_on_artifactID>例
$ cd src/main/java/com/example/app以下の内容で
SecuredServlet.javaファイルを作成します。package com.example.app; import java.io.IOException; import java.io.PrintWriter; import java.security.Principal; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * A simple secured HTTP servlet. It returns the user name of obtained * from the logged-in user's Principal. If there is no logged-in user, * it returns the text "NO AUTHENTICATED USER". */ @WebServlet("/secured") public class SecuredServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try (PrintWriter writer = resp.getWriter()) { writer.println("<html>"); writer.println(" <head><title>Secured Servlet</title></head>"); writer.println(" <body>"); writer.println(" <h1>Secured Servlet</h1>"); writer.println(" <p>"); writer.print(" Current Principal '"); Principal user = req.getUserPrincipal(); writer.print(user != null ? user.getName() : "NO AUTHENTICATED USER"); writer.print("'"); writer.println(" </p>"); writer.println(" </body>"); writer.println("</html>"); } } }アプリケーションのルートディレクトリーで、次のコマンドを使用してアプリケーションをコンパイルします。
$ mvn package ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.015 s [INFO] Finished at: 2022-04-28T17:48:53+05:30 [INFO] ------------------------------------------------------------------------アプリケーションをデプロイします。
$ mvn wildfly:deploy
検証
ブラウザーで
http://localhost:8080/simple-webapp-example/securedに移動します。以下のメッセージが表示されます。
Secured Servlet Current Principal 'NO AUTHENTICATED USER'認証メカニズムが追加されないため、アプリケーションにアクセスできます。
これで、セキュリティードメインを使用してこのアプリケーションを保護し、認証されたユーザーのみがアクセスできるようになります。
2.2.2. アプリケーションへの認証と認可の追加 リンクのコピーリンクがクリップボードにコピーされました!
Web アプリケーションに認証と認可を追加して、セキュリティードメインを使用してアプリケーションを保護できます。認証と認可を追加した後に Web アプリケーションにアクセスするには、ユーザーはログイン認証情報を入力する必要があります。
前提条件
- セキュリティーレルムを参照するセキュリティードメインを作成しました。
- JBoss EAP にアプリケーションをデプロイしました。
- JBoss EAP が実行中である。
手順
undertow subsystemでapplication-security-domainを設定します。構文
/subsystem=undertow/application-security-domain=<application_security_domain_name>:add(security-domain=<security_domain_name>)例
/subsystem=undertow/application-security-domain=exampleApplicationSecurityDomain:add(security-domain=exampleSecurityDomain) {"outcome" => "success"}アプリケーションの
web.xmlを設定して、アプリケーションリソースを保護します。構文
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <!-- Define the security constraints for the application resources. Specify the URL pattern for which a challenge is --> <security-constraint> <web-resource-collection> <web-resource-name><!-- Name of the resources to protect --></web-resource-name> <url-pattern> <!-- The URL to protect --></url-pattern> </web-resource-collection> <!-- Define the role that can access the protected resource --> <auth-constraint> <role-name> <!-- Role name as defined in the security domain --></role-name> <!-- To disable authentication you can use the wildcard * To authenticate but allow any role, use the wildcard **. --> </auth-constraint> </security-constraint> <login-config> <auth-method> <!-- The authentication method to use. Can be: BASIC CLIENT-CERT DIGEST FORM SPNEGO --> </auth-method> <realm-name><!-- The name of realm to send in the challenge --></realm-name> </login-config> </web-app>例
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <!-- Define the security constraints for the application resources. Specify the URL pattern for which a challenge is --> <security-constraint> <web-resource-collection> <web-resource-name>all</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <!-- Define the role that can access the protected resource --> <auth-constraint> <role-name>Admin</role-name> <!-- To disable authentication you can use the wildcard * To authenticate but allow any role, use the wildcard **. --> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>exampleSecurityRealm</realm-name> </login-config> </web-app>注記別の
auth-methodを使用できます。アプリケーションで
jboss-web.xmlファイルを作成するか、undertowサブシステムでデフォルトのセキュリティードメインを設定することにより、セキュリティードメインを使用するようにアプリケーションを設定します。application-security-domainを参照するアプリケーションのWEB-INFディレクトリーにjboss-web.xmlファイルを作成します。構文
<jboss-web> <security-domain> <!-- The security domain to associate with the application --></security-domain> </jboss-web>例
<jboss-web> <security-domain>exampleApplicationSecurityDomain</security-domain> </jboss-web>アプリケーションの
undertowサブシステムでデフォルトのセキュリティードメインを設定します。構文
/subsystem=undertow:write-attribute(name=default-security-domain,value=<application_security_domain_to_use>)例
/subsystem=undertow:write-attribute(name=default-security-domain,value=exampleApplicationSecurityDomain) { "outcome" => "success", "response-headers" => { "operation-requires-reload" => true, "process-state" => "reload-required" } }
サーバーをリロードします。
reload
検証
アプリケーションのルートディレクトリーで、次のコマンドを使用してアプリケーションをコンパイルします。
$ mvn package ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.015 s [INFO] Finished at: 2022-04-28T17:48:53+05:30 [INFO] ------------------------------------------------------------------------アプリケーションをデプロイします。
$ mvn wildfly:deploy-
ブラウザーで
http://localhost:8080/simple-webapp-example/securedに移動します。アプリケーションにアクセスするには認証が必要であることを確認するログインプロンプトが表示されます。
これで、アプリケーションはセキュリティードメインで保護され、ユーザーは認証後にのみログインできます。さらに、指定されたロールを持つユーザーのみがアプリケーションにアクセスできます。