検索

第2章 シングルサインオンによる JBoss EAP にデプロイしたアプリケーションの保護

download PDF

シングルサインオン (SSO) を使用してアプリケーションを保護し、Red Hat build of Keycloak などの SSO プロバイダーに認証を委譲できます。OpenID Connect (OIDC) または Security Assertion Markup Language v2 (SAML v2) のいずれかを SSO プロトコルとして使用できます。

SSO を使用してアプリケーションを保護するには、次の手順に従います。

2.1. シングルサインオンで保護するサンプルアプリケーションの作成

web-application を作成して JBoss EAP にデプロイし、OpenID Connect (OIDC) または Security Assertion Mark-up Language (SAML) を使用してシングルサインオン (SSO) で保護します。

注記

以下の手順は例としてのみ提供されています。保護する必要があるアプリケーションがすでにある場合は、以下の手順をスキップして、Red Hat build of Keycloak でのレルムとユーザーの作成 に直接進むことができます。

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

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'

    認証メカニズムが追加されないため、アプリケーションにアクセスできます。

Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

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

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

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

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

会社概要

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

© 2024 Red Hat, Inc.