2.2. セキュリティードメインを使用したアプリケーションユーザーの認証と認可


セキュリティーレルムを参照するセキュリティードメインを使用して、アプリケーションユーザーを認証および認可します。アプリケーションを開発するための手順は、例としてのみ提供されています。

2.2.1. 簡単な Web アプリケーションの開発

簡単な Web アプリケーションを作成して、セキュリティーレルムの設定例に従うことができます。

注記

以下の手順は例としてのみ提供されています。保護する必要があるアプリケーションがすでにある場合は、以下の手順をスキップして、アプリケーションへの認証と認可の追加 に直接進むことができます。

2.2.1.1. web-application 開発用の Maven プロジェクトの作成

web-application を作成するには、必要な依存関係とディレクトリー構造で Maven プロジェクトを作成します。

重要

以下の手順は一例として提示されています。実稼働環境では使用しないでください。JBoss EAP のアプリケーション作成に関する詳細は、JBoss EAP にデプロイするアプリケーションの開発のスタートガイド を参照してください。

前提条件

手順

  1. 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

  2. アプリケーションのルートディレクトリーに移動します。

    構文

    $ cd <name-of-your-application>

    $ cd simple-webapp-example

  3. 生成された 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 設定ファイルが含まれるディレクトリーを参照します。

前提条件

手順

  1. Java ファイルを保存するディレクトリーを作成します。

    構文

    $ mkdir -p src/main/java/<path_based_on_artifactID>

    $ mkdir -p src/main/java/com/example/app

  2. 新しいディレクトリーに移動します。

    構文

    $ cd src/main/java/<path_based_on_artifactID>

    $ cd src/main/java/com/example/app

  3. 以下の内容で 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>");
            }
        }
    
    }
  4. アプリケーションのルートディレクトリーで、次のコマンドを使用してアプリケーションをコンパイルします。

    $ mvn package
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.015 s
    [INFO] Finished at: 2022-04-28T17:48:53+05:30
    [INFO] ------------------------------------------------------------------------
  5. アプリケーションをデプロイします。

    $ 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 が実行中である。

手順

  1. undertow subsystemapplication-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"}

  2. アプリケーションの 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 を使用できます。

  3. アプリケーションで 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"
          }
      }

  4. サーバーをリロードします。

    reload

検証

  1. アプリケーションのルートディレクトリーで、次のコマンドを使用してアプリケーションをコンパイルします。

    $ mvn package
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.015 s
    [INFO] Finished at: 2022-04-28T17:48:53+05:30
    [INFO] ------------------------------------------------------------------------
  2. アプリケーションをデプロイします。

    $ mvn wildfly:deploy
  3. ブラウザーで http://localhost:8080/simple-webapp-example/secured に移動します。アプリケーションにアクセスするには認証が必要であることを確認するログインプロンプトが表示されます。

これで、アプリケーションはセキュリティードメインで保護され、ユーザーは認証後にのみログインできます。さらに、指定されたロールを持つユーザーのみがアプリケーションにアクセスできます。

Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

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

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

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

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

会社概要

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

Theme

© 2026 Red Hat
トップに戻る