第2章 シングルサインオンによる JBoss EAP にデプロイしたアプリケーションの保護
シングルサインオン (SSO) を使用してアプリケーションを保護し、Red Hat build of Keycloak などの SSO プロバイダーに認証を委譲できます。OpenID Connect (OIDC) または Security Assertion Markup Language v2 (SAML v2) のいずれかを SSO プロトコルとして使用できます。
SSO を使用してアプリケーションを保護するには、次の手順に従います。
- シングルサインオンで保護するサンプルアプリケーションの作成: この手順を使用して、SSO で保護するための単純な web-application を作成します。SSO で保護するアプリケーションがすでにある場合は、この手順をスキップしてください。
- Red Hat build of Keycloak でのレルムとユーザーの作成
OIDC または SAML をプロトコルとして使用して、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 にデプロイするアプリケーションの開発のスタートガイド を参照してください。
前提条件
- 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] ------------------------------------------------------------------------
次のステップ
2.1.2. Web アプリケーションの作成
ログインユーザーのプリンシパルから取得したユーザー名を返すサーブレットを含む Web アプリケーションを作成します。ログインしているユーザーがないと、サーブレットは「NO AUTHENTICATED USER」のテキストを返します。
この手順では、<application_home> は、アプリケーションの pom.xml
設定ファイルが含まれるディレクトリーを参照します。
前提条件
Maven プロジェクトを作成している。
詳細は、web-application 開発用の 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'
認証メカニズムが追加されないため、アプリケーションにアクセスできます。