私の連絡先情報
郵便メール:
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Spring Boot プロジェクトにシングル サインオン ソリューションを統合する
みなさん、こんにちは。私は Weizhuan Taoke System 3.0 の編集者です。冬にはロングジョンを着ないプログラマーでもありますが、寒い季節でも涼しくなければなりません。
最新のエンタープライズ アプリケーションでは、シングル サインオン (SSO) ソリューションは、ユーザーがシームレスにログインして複数のアプリケーションにアクセスできるようにするための重要なテクノロジです。この記事では、ユーザー エクスペリエンスとシステム セキュリティを向上させるために、Spring Boot プロジェクトにシングル サインオン ソリューションを統合する方法を詳しく紹介します。
シングル サインオンを統合する前に、まず適切なシングル サインオン ソリューションを選択する必要があります。一般的なオプションには、OAuth2 ベースのソリューション (Spring Security OAuth または Spring Security + OAuth2 Client など)、および統合認証機関 (Keycloak など) の使用が含まれます。以下では、Spring Security + 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();
}
}
Keycloak などの統合された認証局の使用を選択した場合は、次の手順に従って統合できます。
プロジェクトで構成すると、ユーザーは任意のアプリケーションにアクセスし、資格情報を使用してログインできるようになります。システムは自動的にユーザーを認証局に誘導して認証と認可を取得した後、元のアプリケーションにリダイレクトします。
この記事では、Spring Boot プロジェクトにシングル サインオン ソリューションを統合する方法について説明し、OAuth2 に基づく簡単な構成例を示し、認証センターを統合するオプションについても言及します。これらの手順を通じて、開発者は安全かつ効率的なユーザー認証および認可機能を簡単に実装できます。
微稼ぎたおけシステム3.0の編集者が制作したものなので、品質の高いものであることは間違いありません。転載する場合は出典を明記してください。