Technology Sharing

Integrate single sign-on solution in Spring Boot project

2024-07-12

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

Integrate single sign-on solution in Spring Boot project

Hello everyone, I am the editor of Weizhuan Taobao Affiliate System 3.0, and I am also a programmer who doesn’t wear thermal underwear in winter and wants to be graceful even when it’s cold!

In modern enterprise applications, Single Sign-On (SSO) solutions are key technologies to ensure that users can log in and access multiple applications seamlessly. This article will detail how to integrate a single sign-on solution into a Spring Boot project to improve user experience and system security.

1. Choose a single sign-on solution

Before integrating single sign-on, you first need to choose a suitable single sign-on solution. Common choices include solutions based on OAuth2 (such as Spring Security OAuth or Spring Security + OAuth2 Client), and using an integrated authentication center (such as Keycloak). The following takes Spring Security + OAuth2 Client as an example.

2. Configure OAuth2 Client

In the Spring Boot project, we can implement single sign-on by configuring Spring Security and OAuth2 Client. The following is a simple configuration example:

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. Integrated authentication server

If you choose to use an integrated authentication center, such as Keycloak, you can integrate it by following the steps below:

  • Configure Keycloak Server and create Realm and Client;
  • Configure Keycloak Adapter in Spring Boot project;
  • Configure Spring Security to use Keycloak as the authentication provider.

4. Implement single sign-on

Once configured in the project, users can log in using their credentials by accessing any application. The system will automatically guide the user to the authentication center for authentication, obtain authorization, and then redirect back to the original application.

in conclusion

This article introduces how to integrate a single sign-on solution in a Spring Boot project, provides a simple example configuration based on OAuth2, and mentions the choice of integrated authentication center. Through these steps, developers can easily implement secure and efficient user authentication and authorization functions.

Weizhuan Taoke System 3.0 is produced by the editor and is a high-quality product. Please indicate the source when reprinting!