Based on Spring Cloud several line configuration to complete the development of single sign on

  • 2021-01-25 07:28:21
  • OfStack

Single sign-on concept

Single sign-on (Single Sign On), referred to as SSO, is currently one of the more popular enterprise business integration solutions. SSO is defined as allowing users to access all trusted applications in multiple applications with only one login. Login logic is shown in the figure above

Based on the realization of Spring family bucket

Technical selection:

Spring Boot Spring Cloud Spring Security oAuth2

Client:

maven rely on


<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.security.oauth</groupId>
  <artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-jwt</artifactId>
</dependency>

EnableOAuth2Sso annotations

Entry class configuration @@EnableOAuth2Sso


@SpringBootApplication
public class PigSsoClientDemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(PigSsoClientDemoApplication.class, args);
  }

}

The configuration file


security:
 oauth2:
  client:
   client-id: pig
   client-secret: pig
   user-authorization-uri: http://localhost:3000/oauth/authorize
   access-token-uri: http://localhost:3000/oauth/token
   scope: server
  resource:
   jwt:
    key-uri: http://localhost:3000/oauth/token_key
 sessions: never

SSO authentication server

Authentication server configuration


@Configuration
@Order(Integer.MIN_VALUE)
@EnableAuthorizationServer
public class PigAuthorizationConfig extends AuthorizationServerConfigurerAdapter {
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
        .withClient(authServerConfig.getClientId())
        .secret(authServerConfig.getClientSecret())
        .authorizedGrantTypes(SecurityConstants.REFRESH_TOKEN, SecurityConstants.PASSWORD,SecurityConstants.AUTHORIZATION_CODE)
        .scopes(authServerConfig.getScope());
  }

  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    endpoints
        .tokenStore(new RedisTokenStore(redisConnectionFactory))
        .accessTokenConverter(jwtAccessTokenConverter())
        .authenticationManager(authenticationManager)
        .exceptionTranslator(pigWebResponseExceptionTranslator)
        .reuseRefreshTokens(false)
        .userDetailsService(userDetailsService);
  }

  @Override
  public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security
        .allowFormAuthenticationForClients()
        .tokenKeyAccess("isAuthenticated()")
        .checkTokenAccess("permitAll()");
  }

  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }

  @Bean
  public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
    jwtAccessTokenConverter.setSigningKey(CommonConstant.SIGN_KEY);
    return jwtAccessTokenConverter;
  }
}


Related articles: