技術共有

Spring Boot プロジェクトにシングル サインオン ソリューションを統合する

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Spring Boot プロジェクトにシングル サインオン ソリューションを統合する

みなさん、こんにちは。私は Weizhuan Taoke System 3.0 の編集者です。冬にはロングジョンを着ないプログラマーでもありますが、寒い季節でも涼しくなければなりません。

最新のエンタープライズ アプリケーションでは、シングル サインオン (SSO) ソリューションは、ユーザーがシームレスにログインして複数のアプリケーションにアクセスできるようにするための重要なテクノロジです。この記事では、ユーザー エクスペリエンスとシステム セキュリティを向上させるために、Spring Boot プロジェクトにシングル サインオン ソリューションを統合する方法を詳しく紹介します。

1. シングル サインオン ソリューションを選択する

シングル サインオンを統合する前に、まず適切なシングル サインオン ソリューションを選択する必要があります。一般的なオプションには、OAuth2 ベースのソリューション (Spring Security OAuth または Spring Security + OAuth2 Client など)、および統合認証機関 (Keycloak など) の使用が含まれます。以下では、Spring Security + OAuth2 クライアントを例として使用して説明します。

2. OAuth2 クライアントの構成

Spring Boot プロジェクトでは、Spring Security と OAuth2 Client を設定することでシングルサインオン機能を実装できます。簡単な構成例を次に示します。

package cn.juwatech.taokua.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .oauth2Login()
                .defaultSuccessUrl("/dashboard")
                .and()
            .logout()
                .logoutSuccessUrl("/")
                .permitAll();
    }

    @Bean
    public ClientRegistrationRepository clientRegistrationRepository() {
        return new InMemoryClientRegistrationRepository(githubClientRegistration());
    }

    private ClientRegistration githubClientRegistration() {
        return ClientRegistration.withRegistrationId("github")
                .clientId("your-client-id")
                .clientSecret("your-client-secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUriTemplate("{baseUrl}/login/oauth2/code/{registrationId}")
                .scope("read:user")
                .authorizationUri("https://github.com/login/oauth/authorize")
                .tokenUri("https://github.com/login/oauth/access_token")
                .userInfoUri("https://api.github.com/user")
                .userNameAttributeName("id")
                .clientName("GitHub")
                .build();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

3. 統合認証サーバー

Keycloak などの統合された認証局の使用を選択した場合は、次の手順に従って統合できます。

  • Keycloakサーバーを構成し、レルムとクライアントを作成します。
  • Spring Boot プロジェクトで Keycloak アダプターを構成します。
  • Keycloak を認証プロバイダーとして使用するように Spring Security を構成します。

4. シングルサインオンの実装

プロジェクトで構成すると、ユーザーは任意のアプリケーションにアクセスし、資格情報を使用してログインできるようになります。システムは自動的にユーザーを認証局に誘導して認証と認可を取得した後、元のアプリケーションにリダイレクトします。

結論は

この記事では、Spring Boot プロジェクトにシングル サインオン ソリューションを統合する方法について説明し、OAuth2 に基づく簡単な構成例を示し、認証センターを統合するオプションについても言及します。これらの手順を通じて、開発者は安全かつ効率的なユーザー認証および認可機能を簡単に実装できます。

微稼ぎたおけシステム3.0の編集者が制作したものなので、品質の高いものであることは間違いありません。転載する場合は出典を明記してください。