详解基于Spring Cloud几行配置完成单点登录开发

作者:冷冷gg 时间:2023-07-06 19:14:11 

单点登录概念

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。登录逻辑如上图

基于Spring 全家桶的实现

技术选型:

  1. Spring Boot

  2. Spring Cloud

  3. Spring Security oAuth2

客户端:

maven依赖


<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 注解

入口类配置@@EnableOAuth2Sso


@SpringBootApplication
public class PigSsoClientDemoApplication {

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

}

配置文件


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认证服务器

认证服务器配置


@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;
 }
}

来源:https://juejin.im/post/5a6e771e5188253dc3323b6b

标签:Spring,Cloud,单点登录
0
投稿

猜你喜欢

  • java二叉树面试题详解

    2021-06-13 08:40:23
  • Java中用enum结合testng实现数据驱动的方法示例

    2021-08-04 07:44:21
  • JAVA MyBatis入门学习过程记录

    2022-04-23 13:49:24
  • Android EasyBarrage实现轻量级弹幕效果

    2022-03-07 06:46:31
  • C#使用base64对字符串进行编码和解码的测试

    2022-09-21 23:08:59
  • C#特性(Attribute)

    2022-08-18 18:34:51
  • Android编程实现wifi扫描及连接的方法

    2022-11-21 21:11:36
  • C# Word 类库的深入理解

    2023-07-21 07:29:09
  • Android如何实现扫描和生成二维码

    2022-11-15 02:03:49
  • java遍历properties文件操作指南

    2023-11-23 02:43:45
  • AlertDialog点击按钮不消失的实现方法

    2023-12-12 07:11:16
  • Java对象转json JsonFormat注解

    2022-08-27 00:44:09
  • Java通过Fork/Join优化并行计算

    2023-01-27 21:28:36
  • 详解Java中LinkedHashMap

    2022-05-23 16:24:45
  • Java Spring详解如何配置数据源注解开发以及整合Junit

    2021-10-31 11:03:25
  • Android实现自定义圆形进度条

    2022-10-28 04:55:22
  • Spring Boot 日志配置方法(超详细)

    2021-09-06 19:08:52
  • springmvc后台基于@ModelAttribute获取表单提交的数据

    2023-08-05 12:29:35
  • C#中using的三种用法

    2022-08-04 05:10:59
  • 微服务链路追踪Spring Cloud Sleuth整合Zipkin解析

    2022-09-23 22:29:36
  • asp之家 软件编程 m.aspxhome.com