Spring Webアプリケーションにログインを追加する

1

Auth0を構成する

Auth0のサービスを利用するには、Auth0 Dashboadでセットアップしたアプリケーションが必要です。Auth0アプリケーションは、開発中のプロジェクトに対してどのように認証が動作して欲しいかを構成する場所です。

アプリケーションを構成する

インタラクティブセレクターを使ってAuth0アプリケーションを新規作成するか、統合したいプロジェクトを表す既存のアプリケーションを選択します。Auth0の全てのアプリケーションには英数字からなる一意のクライアントIDが割り当てられており、アプリケーションのコードがSDKを通じてAuth0 APIを呼び出す際に使用されます。

このクイックスタートを使って構成されたすべての設定は、Dashboardのアプリケーションに自動更新を行います。今後、アプリケーションの管理はDashboardで行えます。

代わりに完了済みの構成を見てみたい場合は、サンプルアプリケーションをご覧ください。

Callback URLを構成する

Callback URLとは、Auth0がユーザーを認証後にリダイレクトするアプリケーション内URLです。設定されていない場合、ユーザーはログイン後にアプリケーションに戻りません。

ログアウトURLを構成する

ログアウトURLとは、Auth0がユーザーをログアウト後にリダイレクトするアプリケーション内URLです。設定されていない場合、ユーザーはアプリケーションからログアウトできず、エラーを受け取ります。

2

Sprint Bootアプリケーションを構成する

Spring依存関係を追加する

Spring BootアプリケーションをAuth0と統合するには、Okta Spring Boot Starterをアプリケーションの依存関係に含めます。

Gradleを使用している場合は、以下のようにこれらの依存関係を含めることができます。

plugins {
id 'java'
id 'org.springframework.boot' version '3.1.4'
id 'io.spring.dependency-management' version '1.1.3'
}
implementation 'com.okta.spring:okta-spring-boot-starter:3.0.5'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'

Was this helpful?

/

Mavenを使用している場合:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.okta</groupId>
<artifactId>okta-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
</dependencies>

Was this helpful?

/

3

Spring Securityを構成する

Okta Spring Boot Starterでは、Auth0でアプリケーションを簡単に構成できます。以下のサンプルではapplication.ymlファイルを使用していますが、プロパティファイルや他のサポートされる表出化メカニズムを使用することもできます。

#src/main/resources/application.yml
okta:
oauth2:
issuer: https://{yourDomain}/

client-id: {yourClientId}

client-secret: {yourClientSecret}

#The sample and instructions above for the callback and logout URL configuration use port 3000.
#If you wish to use a different port, change this and be sure your callback and logout URLs are
#configured with the correct port.
server:
port: 3000

Was this helpful?

/

4

アプリケーションにログインを追加する

Auth0でユーザーログインを有効にするには、SecurityFilterChainを登録するクラスを作成し、@Configurationの注釈を追加します。

http.authorizeHttpRequests(authorize -> authorize
    .requestMatchers(&quot;/&quot;).permitAll()

    .anyRequest().authenticated()

);

Was this helpful?

/

5

表紙ページを追加する

Okta Spring Boot Starterは以前に定義したクライアント構成を使って、ユーザーがアプリケーションの/oauth2/authorization/oktaパスにアクセスしたときのログインを処理します。これを使用して、アプリケーションでログインリンクを作成することができます。

このページは、ユーザー認証時にユーザー属性を返します。テンプレートの/logoutリンクを使用して、ログアウト機能を実装します。

6

コントローラーを追加する

受信要求を処理するようにコントロールを作成します。このコントローラはindex.htmlページをレンダリングします。ユーザー認証時に、アプリケーションはユーザーのプロファイル情報の属性を取得し、ページをレンダリングします。

checkpoint.header

ログインリンクをクリックすると、アプリケーションによってAuth0ユニバーサルログインページにリダイレクトされ、ユーザー名とパスワードまたはソーシャルプロバイダーを使ってログインまたはサインアップできるようになったことを確認します。

null

7

アプリケーションにログアウトを追加する

アプリケーションにログインできるようになったら、ログアウトする方法が必要です。デフォルトで、ログアウトが有効になると、Spring Securityはユーザーをアプリケーションからログアウトし、セッションを消去します。Auth0から正常にログアウトするには、LogoutHandlerでユーザーをAuth0ログアウトエンドポイントhttps://{yourDomain}/v2/logout)にリダイレクトした直後に、アプリケーションにリダイレクトします。

SecurityConfigクラスでLogoutHandlerを指定すると、Auth0ログアウトエンドポイントにリダイレクトされ、ログアウトハンドラを追加するようにHttpSecurityを構成します。

checkpoint.header

ログアウトリンクをクリックすると、「Settings(設定)」にある[Allowed Logout URLs(許可されているログアウトURL)]のいずれかに指定されたアドレスにリダイレクトされ、アプリケーションにログインされなくなります。

Next Steps

Excellent work! If you made it this far, you should now have login, logout, and user profile information running in your application.

This concludes our quickstart tutorial, but there is so much more to explore. To learn more about what you can do with Auth0, check out:

Did it work?

Any suggestion or typo?

Edit on GitHub
Sign Up

Sign up for an or to your existing account to integrate directly with your own tenant.