このトピックでは、Spring Boot フレームワークを使用する Java アプリケーションを Identity as a Service (IDaaS) に接続する方法について説明します。
接続の仕組みと呼び出しプロセスについて詳しくは、「自作アプリケーションを IDaaS に接続して SSO を実現する」をご参照ください。
IDaaS は、OpenID Connect (OIDC) 認可コードグラントタイプを使用して、自作アプリケーションへの接続をサポートしています。このグラントタイプは OAuth 2.0 認可コードグラントタイプと下位互換性があるため、OAuth ツールキット spring-boot-starter-oauth2-client を使用して SSO を実装できます。
このツールキットは、OIDC 認可コードフローにおけるすべての認可呼び出しと id_token 解析のコードをカプセル化しています。
1. ツールキットをインポートする
次の例は、依存関係 spring-boot-starter-oauth2-client を pom.xml ファイルに追加する方法を示しています。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
</parent>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<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>
</dependencies>2. IDaaS 情報を構成する
次の例は、application.properties ファイルで client_id、client_secret、および issuer を構成する方法を示しています。
spring.security.oauth2.client.registration.aliyunidaas.client-id=app_***
spring.security.oauth2.client.registration.aliyunidaas.client-secret=CS***
spring.security.oauth2.client.provider.aliyunidaas.issuer-uri=<issuer>上記の情報は、アプリケーションの作成後に取得できます。詳細については、「自作アプリケーションを IDaaS に接続して SSO を実現する」をご参照ください。
3. OAuth 認証を必須にするように Spring Security を構成する
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login();
}
}4. ユーザー情報を取得する
ツールキットは、すべての認可呼び出しと id_token 検証を自動的に完了します。
@AuthenticationPrincipal OAuth2User user をインジェクトすることで、ユーザー情報を取得できます。
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@GetMapping("/user")
public OAuth2User user(@AuthenticationPrincipal OAuth2User user) {
return user;
}
}/user エンドポイントでユーザー情報を出力できます。

ユーザー情報を使用してユーザーをログインさせ、SSO プロセスを完了します。
プロキシの背後にあるアプリケーションのデプロイ
Alibaba Cloud Server Load Balancer (SLB) や NGINX などのプロキシの背後にアプリケーションをデプロイする場合、テスト中に次の問題が発生する可能性があります。
ユーザーが https://www.example.com にアクセスします。
ユーザーが OIDC に基づいてアプリケーションにログインするときに生成される
redirect_uriが http://127.0.0.1:8080/oauth2/authorization/aliyunidaas に変更されます。ログインリクエストは失敗します。
問題を解決するには、次の手順を実行して、Spring Boot が redirect_uri を生成するロジックを変更する必要があります。
Spring Boot に次の設定項目を追加します。
server.forward-headers-strategy = NATIVEAlibaba Cloud SLB を使用する場合は、次の図に示すように、SLB の詳細設定で「X-Forwarded-Proto ヘッダーを使用してリスナープロトコルを取得する」を選択する必要があります。

SLB とアプリケーションの間に NGINX プロキシを使用する場合は、プロキシで次のパラメーターを構成する必要があります。
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;NGINX プロキシを直接使用して HTTPS 経由でサービスを提供する場合は、次のパラメーターを構成する必要があります。
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;他のプロキシサービスまたはソフトウェアを使用する場合は、構成を確認する必要があります。正しい HTTP ヘッダー Host と X-forwarded-Proto が Spring Boot に送信されるリクエストで指定されていることを確認してください。
参照:
https://tools.ietf.org/html/rfc7239
https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/