下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

SpringCloud--扩展JustAuth多租户微信扫码登录

作者:匿名     来源: 编程语言点击数:2043发布时间: 2022-06-12 14:41:52

标签: JustAuthSpring

大神带你学编程,欢迎选课

  目前很多平台都提供了单点登录授权服务器功能,比如我们经常用到的QQ登录、微信登录、新浪微博登录、支付宝登录等。如果我们自己的系统需要调用第三方登录,那么我们就需要实现单点登录客户端,然后跟需要对接的平台调试登录SDK。JustAuth是第三方授权登录的工具类库,对接了国外内数十家第三方登录的SDK,我们在需要实现第三方登录时,只需要集成JustAuth工具包,然后配置即可实现第三方登录,省去了需要对接不同SDK的麻烦。

  JustAuth官方提供了多种入门指南,集成使用非常方便。但是如果要贴合我们自有开发框架的业务需求,还是需要进行整合优化。下面根据我们的系统需求,从两方面进行整合:一是支持多租户功能,二是和自有系统的用户进行匹配。

  一、JustAuth多租户系统配置

  GitEgg多租户功能实现介绍

  GitEgg框架支持多租户功能,从多租户的实现来讲,目前大多数平台都是在登录界面输入租户的标识来确定属于哪个租户,这种方式简单有效,但是对于用户来讲体验不是很好。我们更希望的多租户功能是能够让用户无感知,且每个租户有自己不同的界面展示。  

  GitEgg在实现多租户功能时,考虑到同一域名可以设置多个子域名,每个子域名可对应不同的租户。所以,对于多租户的识别方式,首先是根据浏览器当前访问的域名或IP地址和系统配置的多租户域名或IP地址信息进行自动识别,如果是域名或IP地址存在多个,或者未找到相关配置时,才会由用户自己选择属于哪个租户。

  自定义JustAuth配置文件信息到数据库和缓存

  在JustAuth的官方Demo中,SpringBoot集成JustAuth是将第三方授权信息配置在yml配置文件中的,对于单租户系统来说,可以这样配置。但是,对于多租户系统,我们需要考虑多种情况:一种是整个多租户系统使用同一套第三方授权,授权之后再由用户选择绑定到具体的租户;另外一种是每个租户配置自己的第三方授权,更具差异化。  

  出于功能完整性的考虑,我们两种情况都实现,当租户不配置自有的第三方登录参数时,使用的是系统默认自带的第三方登录参数。当租户配置了自有的第三方登录参数时,就是使用租户自己的第三方授权服务器。我们将JustAuth原本配置在yml配置文件中的第三方授权服务器信息配置在数据库中,并增加多租户标识,这样在不同租户调用第三方登录时就是相互隔离的。

  1、JustAuth配置信息表字段设计

  首先我们通过JustAuth官方Demojustauth-spring-boot-starter-demo 了解到JustAuth主要的配置参数为:

  JustAuth功能启用开关。

  自定义第三方登录的配置信息。

  内置默认第三方登录的配置信息。

  Http请求代理的配置信息。

  缓存的配置信息。

  复制

  1. justauth:

  2. # JustAuth功能启用开关

  3. enabled: true

  4. # 自定义第三方登录的配置信息

  5. extend:

  6. enum-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendSource

  7. config:

  8. TEST:

  9. request-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendTestRequest

  10. client-id: xxxxxx

  11. client-secret: xxxxxxxx

  12. redirect-uri: http://oauth.xkcoding.com/demo/oauth/test/callback

  13. MYGITLAB:

  14. request-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendMyGitlabRequest

  15. client-id: xxxxxx

  16. client-secret: xxxxxxxx

  17. redirect-uri: http://localhost:8443/oauth/mygitlab/callback

  18. # 内置默认第三方登录的配置信息

  19. type:

  20. GOOGLE:

  21. client-id: xxxxxx

  22. client-secret: xxxxxxxx

  23. redirect-uri: http://localhost:8443/oauth/google/callback

  24. ignore-check-state: false

  25. scopes:

  26. - profile

  27. - email

  28. - openid

  29. # Http请求代理的配置信息

  30. http-config:

  31. timeout: 30000

  32. proxy:

  33. GOOGLE:

  34. type: HTTP

  35. hostname: 127.0.0.1

  36. port: 10080

  37. MYGITLAB:

  38. type: HTTP

  39. hostname: 127.0.0.1

  40. port: 10080

  41. # 缓存的配置信息

  42. cache:

  43. type: default

  44. prefix: 'demo::'

  45. timeout: 1h

  在对配置文件存储格式进行设计时,结合对多租户系统的需求分析,我们需要选择哪些配置是系统公共配置,哪些是租户自己的配置。比如自定义第三方登录的enum-class这个是需要由系统开发的,是整个多租户系统的功能,这种可以看做是通用配置,但是在这里,考虑到后续JustAuth系统升级,我们不打算破坏原先配置文件的结构,所以我们仍选择各租户隔离配置。  

  我们将JustAuth配置信息拆分为两张表存储,一张是配置JustAuth开关、自定义第三方登录配置类、缓存配置、Http超时配置等信息的表(t_just_auth_config),这些配置信息的同一特点是与第三方登录系统无关,不因第三方登录系统的改变而改变;还有一张表是配置第三方登录相关的参数、Http代理请求表(t_just_auth_source)。租户和t_just_auth_config为一对一关系,和t_just_auth_source为一对多关系。

  t_just_auth_config(租户第三方登录功能配置表)表定义:

  复制

  1. SET NAMES utf8mb4;

  2. SET FOREIGN_KEY_CHECKS = 0;

  3. -- ----------------------------

  4. -- Table structure for t_just_auth_config

  5. -- ----------------------------

  6. DROP TABLE IF EXISTS `t_just_auth_config`;

  7. CREATE TABLE `t_just_auth_config` (

  8. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',

  9. `tenant_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '租户id',

  10. `enabled` tinyint(1) NULL DEFAULT NULL COMMENT 'JustAuth开关',

  11. `enum_class` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '自定义扩展第三方登录的配置类',

  12. `http_timeout` bigint(20) NULL DEFAULT NULL COMMENT 'Http请求的超时时间',

  13. `cache_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '缓存类型',

  14. `cache_prefix` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '缓存前缀',

  15. `cache_timeout` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '缓存超时时间',

  16. `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',

  17. `creator` bigint(20) NULL DEFAULT NULL COMMENT '创建者',

  18. `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',

  19. `operator` bigint(20) NULL DEFAULT NULL COMMENT '更新者',

  20. `del_flag` tinyint(2) NULL DEFAULT 0 COMMENT '是否删除',

  21. PRIMARY KEY (`id`) USING BTREE

  22. ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '租户第三方登录功能配置表' ROW_FORMAT = DYNAMIC;

  23.

      24. SET FOREIGN_KEY_CHECKS = 1;1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.

  t_just_auth_sourc(租户第三方登录信息配置表)表定义:

  复制

  1. SET NAMES utf8mb4;

  2. SET FOREIGN_KEY_CHECKS = 0;

  3. -- ----------------------------

  4. -- Table structure for t_just_auth_source

  5. -- ----------------------------

  6. DROP TABLE IF EXISTS `t_just_auth_source`;

  7. CREATE TABLE `t_just_auth_source` (

  8. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',

  9. `tenant_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '租户id',

  10. `source_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方登录的名称',

  11. `source_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方登录类型:默认default 自定义custom',

  12. `request_class` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '自定义第三方登录的请求Class',

  13. `client_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '客户端id:对应各平台的appKey',

  14. `client_secret` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '客户端Secret:对应各平台的appSecret',

  15. `redirect_uri` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '登录成功后的回调地址',

  16. `alipay_public_key` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '支付宝公钥:当选择支付宝登录时,该值可用',

  17. `union_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '是否需要申请unionid,目前只针对qq登录',

  18. `stack_overflow_key` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'Stack Overflow Key',

  19. `agent_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '企业微信,授权方的网页应用ID',

  20. `user_type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '企业微信第三方授权用户类型,member|admin',

  21. `domain_prefix` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '域名前缀 使用 Coding 登录和 Okta 登录时,需要传该值。',

  22. `ignore_check_state` tinyint(1) NOT NULL DEFAULT 0 COMMENT '忽略校验code state}参数,默认不开启。',

  23. `scopes` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '支持自定义授权平台的 scope 内容',

  24. `device_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '设备ID, 设备唯一标识ID',

  25. `client_os_type` int(11) NULL DEFAULT NULL COMMENT '喜马拉雅:客户端操作系统类型,1-iOS系统,2-Android系统,3-Web',

  26. `pack_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '喜马拉雅:客户端包名',

  27. `pkce` tinyint(1) NULL DEFAULT NULL COMMENT ' 是否开启 PKCE 模式,该配置仅用于支持 PKCE 模式的平台,针对无服务应用,不推荐使用隐式授权,推荐使用 PKCE 模式',

  28. `auth_server_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'Okta 授权服务器的 ID, 默认为 default。',

  29. `ignore_check_redirect_uri` tinyint(1) NOT NULL DEFAULT 0 COMMENT '忽略校验 {@code redirectUri} 参数,默认不开启。',

  30. `proxy_type` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'Http代理类型',

  31. `proxy_host_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'Http代理Host',

  32. `proxy_port` int(11) NULL DEFAULT NULL COMMENT 'Http代理Port',

  33. `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',

  34. `creator` bigint(20) NULL DEFAULT NULL COMMENT '创建者',

  35. `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',

  36. `operator` bigint(20) NULL DEFAULT NULL COMMENT '更新者',

  37. `del_flag` tinyint(2) NULL DEFAULT 0 COMMENT '是否删除',

  38. PRIMARY KEY (`id`) USING BTREE

  39. ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '租户第三方登录信息配置表' ROW_FORMAT = DYNAMIC;

  40. SET FOREIGN_KEY_CHECKS = 1;

  2、使用GitEgg代码生成工具生成JustAuth配置信息的CRUD代码

  我们将JustAuth配置信息管理的相关代码和JustAuth实现业务逻辑的代码分开,配置信息我们在系统启动时加载到Redis缓存,JustAuth在调用时,直接调用Redis缓存中的配置。  

  前面讲过如何通过数据库表设计生成CRUD的前后端代码,这里不再赘述,生成好的后台代码我们放在gitegg-service-extension工程下,和短信、文件存储等的配置放到同一工程下,作为框架的扩展功能。

  基础配置:

  第三方列表:

  3、代码生成之后,需要做初始化缓存处理,即在第三方配置服务启动的时候,将多租户的配置信息初始化到Redis缓存中。

  初始化的CommandLineRunner类 InitExtensionCacheRunner.java。

  复制

  1. /**

  2. * 容器启动完成加载资源权限数据到缓存

  3. * @author GitEgg

  4. */

  5. @Slf4j

  6. @RequiredArgsConstructor(onConstructor_ = @Autowired)

  7. @Component

  8. public class InitExtensionCacheRunner implements CommandLineRunner {

  9. private final IJustAuthConfigService justAuthConfigService;

  10. private final IJustAuthSourceService justAuthSourceService;

  11. @Override

  12. public void run(String... args) {

  13. log.info("InitExtensionCacheRunner running");

  14. // 初始化第三方登录主配置

  15. justAuthConfigService.initJustAuthConfigList();

  16. // 初始化第三方登录 第三方配置

  17. justAuthSourceService.initJustAuthSourceList();

  18. }

  19. }

  第三方登录主配置初始化方法。

  复制

  1. /**

  2. * 初始化配置表列表

  3. * @return

  4. */

  5. @Override

  6. public void initJustAuthConfigList() {

  7. QueryJustAuthConfigDTO queryJustAuthConfigDTO = new QueryJustAuthConfigDTO();

  8. queryJustAuthConfigDTO.setStatus(GitEggConstant.ENABLE);

  9. List justAuthSourceInfoList = justAuthConfigMapper.initJustAuthConfigList(queryJustAuthConfigDTO);

  10.

      11. // 判断是否开启了租户模式,如果开启了,那么角色权限需要按租户进行分类存储

  12. if (enable) {

  13. Map> authSourceListMap =

  14. justAuthSourceInfoList.stream().collect(Collectors.groupingBy(JustAuthConfigDTO::getTenantId));

  15. authSourceListMap.forEach((key, value) -> {

  16. String redisKey = AuthConstant.SOCIAL_TENANT_CONFIG_KEY + key;

  17. redisTemplate.delete(redisKey);

  18. addJustAuthConfig(redisKey, value);

  19. });

      20.

  21. } else {

  22. redisTemplate.delete(AuthConstant.SOCIAL_CONFIG_KEY);

  23. addJustAuthConfig(AuthConstant.SOCIAL_CONFIG_KEY, justAuthSourceInfoList);

  24. }

  25. }

  26. private void addJustAuthConfig(String key, List configList) {

  27. Map authConfigMap = new TreeMap<>();

  28. Optional.ofNullable(configList).orElse(new ArrayList<>()).forEach(config -> {

  29. try {

  30. authConfigMap.put(config.getTenantId().toString(), JsonUtils.objToJson(config));

  31. redisTemplate.opsForHash().putAll(key, authConfigMap);

  32. } catch (Exception e) {

  33. log.error("初始化第三方登录失败:{}" , e);

  34. }

  35. });

  36. }

  第三方登录参数配置初始化方法。

  复制

  1. /**

  2. * 初始化配置表列表

  3. * @return

  4. */

  5. @Override

  6. public void initJustAuthSourceList() {

  7. QueryJustAuthSourceDTO queryJustAuthSourceDTO = new QueryJustAuthSourceDTO();

  8. queryJustAuthSourceDTO.setStatus(GitEggConstant.ENABLE);

  9. List justAuthSourceInfoList = justAuthSourceMapper.initJustAuthSourceList(queryJustAuthSourceDTO);

  10.

      11. // 判断是否开启了租户模式,如果开启了,那么角色权限需要按租户进行分类存储

  12. if (enable) {

  13. Map> authSourceListMap =

  14. justAuthSourceInfoList.stream().collect(Collectors.groupingBy(JustAuthSourceDTO::getTenantId));

  15. authSourceListMap.forEach((key, value) -> {

  16. String redisKey = AuthConstant.SOCIAL_TENANT_SOURCE_KEY + key;

  17. redisTemplate.delete(redisKey);

  18. addJustAuthSource(redisKey, value);

  19. });

  20. } else {

  21. redisTemplate.delete(AuthConstant.SOCIAL_SOURCE_KEY);

  22. addJustAuthSource(AuthConstant.SOCIAL_SOURCE_KEY, justAuthSourceInfoList);

  23. }

  24. }

  25. private void addJustAuthSource(String key, List sourceList) {

  26. Map authConfigMap = new TreeMap<>();

  27. Optional.ofNullable(sourceList).orElse(new ArrayList<>()).forEach(source -> {

  28. try {

  29. authConfigMap.put(source.getSourceName(), JsonUtils.objToJson(source));

  30. redisTemplate.opsForHash().putAll(key, authConfigMap);

  31. } catch (Exception e) {

  32. log.error("初始化第三方登录失败:{}" , e);

  33. }

  34. });

  35. }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.

  4、引入JustAuth相关依赖jar包

  在gitegg-platform-bom工程中引入JustAuth包和版本,JustAuth提供了SpringBoot集成版本justAuth-spring-security-starter,如果简单使用,可以直接引用SpringBoot集成版本,我们这里因为需要做相应的定制修改,所以引入JustAuth基础工具包。

  复制

  1. ······

  2. <!-- JustAuth第三方登录 -->

  3. <just.auth.version>1.16.5</just.auth.version>

  4. <!-- JustAuth SpringBoot集成 -->

  5. <just.auth.spring.version>1.4.0</just.auth.spring.version>

  6. ······

  7. <!--JustAuth第三方登录-->

  8. <dependency>

  9. <groupId>me.zhyd.oauth</groupId>

  10. <artifactId>JustAuth</artifactId>

  11. <version>${just.auth.version}</version>

      12. </dependency>

  13. <!--JustAuth SpringBoot集成-->

  14. <dependency>

      15. <groupIdcom>.xkcoding.justauth</groupId>

  16. <artifactId>justauth-spring-boot-starter</artifactId>

  17. <version>${just.auth.spring.version}</version>

  18. </dependency>

  19. ······

  新建gitegg-platform-justauth工程,用于实现公共自定义代码,并在pom.xml中引入需要的jar包。

  复制

  1. <dependency>

  2. <!-- gitegg Spring Boot自定义及扩展 -->

  3. <dependency>

  4. <groupId>com.gitegg.platform</groupId>

  5. <artifactId>gitegg-platform-boot</artifactId>

  6. </dependency>

  7. <!--JustAuth第三方登录-->

  8. <dependency>

  9. <groupI>dme.zhyd.oauth</groupId>

  10. <artifactId>JustAuth</artifactId>

  11. </dependency>

  12. <!--JustAuth SpringBoot集成-->

  13. <dependency>

  14. <groupI>com.xkcoding.justauth</groupI>

  15. <artifactId>justauth-spring-boot-starter</artifactId>

  16. <!-- 不使用JustAuth默认版本-->

  17. <exclusions>

  18. <exclusions>

  19. <groupI>me.zhyd.oauth</groupI>

  20. <artifactId>JustAuth</artifactId>

  21. </exclusion>

  22. <exclusion>

  23. <groupI>org.springframework.boot</groupI>

  24. <artifactId>spring-boot-starter-data-redis</artifactId>

  25. </exclusion>

  26. <exclusion>

  27. <groupI>org.springframework.boot</groupI>

  28. <artifactId>spring-boot-autoconfigure</artifactId>

  29. </exclusion>

  30. <exclusion>

  31. <groupI>org.springframework.boot</groupI>

  32. <artifactId>spring-boot-configuration-processor</artifactId>

  33. </exclusion>

  34. </exclusion>

  35. </dependency>

  36. </dependency>

  5、自定义实现获取和实例化多租户第三方登录配置的AuthRequest工厂类GitEggAuthRequestFactory.java

  复制

  1. /**

  2. * GitEggAuthRequestFactory工厂类

  3. *

  4. * @author GitEgg

  5. */

  6. @Slf4j

  7. @RequiredArgsConstructor

  8. public class GitEggAuthRequestFactory {

  9. private final RedisTemplate redisTemplate;

  10. private final AuthRequestFactory authRequestFactory;

  11. private final JustAuthProperties justAuthProperties;

  12. /**

  13. * 是否开启租户模式

  14. */

  15. @Value("${tenant.enable}")

  16. private Boolean enable;

  17. public GitEggAuthRequestFactory(AuthRequestFactory authRequestFactory, RedisTemplate redisTemplate, JustAuthProperties justAuthProperties) {

  18. this.authRequestFactory = authRequestFactory;

  19. this.redisTemplate = redisTemplate;

  20. this.justAuthProperties = justAuthProperties;

  21. }

  22. /**

  23. * 返回当前Oauth列表

  24. *

  25. * @return Oauth列表

  26. */

  27. public List oauthList() {

  28. // 合并

  29. return authRequestFactory.oauthList();

  30. }

  31. /**

  32. * 返回AuthRequest对象

  33. *

  34. * @param source {@link AuthSource}

  35. * @return {@link AuthRequest}

  36. */

  37. public AuthRequest get(String source) {

  38.

      39. if (StrUtil.isBlank(source)) {

  40. throw new AuthException(AuthResponseStatus.NO_AUTH_SOURCE);

  41. }

  42. // 组装多租户的缓存配置key

  43. String authConfigKey = AuthConstant.SOCIAL_TENANT_CONFIG_KEY;

  44. if (enable) {

  45. authConfigKey += GitEggAuthUtils.getTenantId();

  46. } else {

  47. authConfigKey = AuthConstant.SOCIAL_CONFIG_KEY;

  48. }

  49. // 获取主配置,每个租户只有一个主配置

  50. String sourceConfigStr = (String) redisTemplate.opsForHash().get(authConfigKey, GitEggAuthUtils.getTenantId());

  51. AuthConfig authConfig = null;

  52. JustAuthSource justAuthSource = null;

  53. AuthRequest tenantIdAuthRequest = null;

  54. if (!StringUtils.isEmpty(sourceConfigStr))

  55. {

  56. try {

  57. // 转为系统配置对象

  58. JustAuthConfig justAuthConfig = JsonUtils.jsonToPojo(sourceConfigStr, JustAuthConfig.class);

  59. // 判断该配置是否开启了第三方登录

  60. if (justAuthConfig.getEnabled())

  61. {

  62. // 根据配置生成StateCache

  63. CacheProperties cacheProperties = new CacheProperties();

  64. if (!StringUtils.isEmpty(justAuthConfig.getCacheType())

  65. && !StringUtils.isEmpty(justAuthConfig.getCachePrefix())

  66. && null != justAuthConfig.getCacheTimeout())

  67. {

  68. cacheProperties.setType(CacheProperties.CacheType.valueOf(justAuthConfig.getCacheType().toUpperCase()));

  69. cacheProperties.setPrefix(justAuthConfig.getCachePrefix());

  70. cacheProperties.setTimeout(Duration.ofMinutes(justAuthConfig.getCacheTimeout()));

  71. }

  72. else

  73. {

  74. cacheProperties = justAuthProperties.getCache();

  75. }

  76. GitEggRedisStateCache gitEggRedisStateCache =

  77. new GitEggRedisStateCache(redisTemplate, cacheProperties, enable);

     78. 

  79. // 组装多租户的第三方配置信息key

  80. String authSourceKey = AuthConstant.SOCIAL_TENANT_SOURCE_KEY;

  81. if (enable) {

  82. authSourceKey += GitEggAuthUtils.getTenantId();

  83. } else {

  84. authSourceKey = AuthConstant.SOCIAL_SOURCE_KEY;

  85. }

  86. // 获取具体的第三方配置信息

  87. String sourceAuthStr = (String)redisTemplate.opsForHash().get(authSourceKey, source.toUpperCase());

  88. if (!StringUtils.isEmpty(sourceAuthStr))

  89. {

  90. // 转为系统配置对象

  91. justAuthSource = JsonUtils.jsonToPojo(sourceAuthStr, JustAuthSource.class);

  92. authConfig = BeanCopierUtils.copyByClass(justAuthSource, AuthConfig.class);

  93. // 组装scopes,因为系统配置的是逗号分割的字符串

  94. if (!StringUtils.isEmpty(justAuthSource.getScopes()))

  95. {

  96. String[] scopes = justAuthSource.getScopes().split(StrUtil.COMMA);

  97. authConfig.setScopes(Arrays.asList(scopes));

  98. }

  99. // 设置proxy

  100. if (StrUtil.isAllNotEmpty(justAuthSource.getProxyType(), justAuthSource.getProxyHostName())

  101. && null != justAuthSource.getProxyPort())

  102. {

  103. JustAuthProperties.JustAuthProxyConfig proxyConfig = new JustAuthProperties.JustAuthProxyConfig();

  104. proxyConfig.setType(justAuthSource.getProxyType());

  105. proxyConfig.setHostname(justAuthSource.getProxyHostName());

  106. proxyConfig.setPort(justAuthSource.getProxyPort());

  107. if (null != proxyConfig) {

  108. HttpConfig httpConfig = HttpConfig.builder().timeout(justAuthSource.getProxyPort()).proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort()))).build();

  109. if (null != justAuthConfig.getHttpTimeout())

  110. {

  111. httpConfig.setTimeout(justAuthConfig.getHttpTimeout());

  112. }

  113. authConfig.setHttpConfig(httpConfig);

  114. }

  115. }

  116. // 组装好配置后,从配置生成request,判断是默认的第三方登录还是自定义第三方登录

  117. if (SourceTypeEnum.DEFAULT.key.equals(justAuthSource.getSourceType()))

  118. {

  119. tenantIdAuthRequest = this.getDefaultRequest(source, authConfig, gitEggRedisStateCache);

  120. }

  121. else if (!StringUtils.isEmpty(justAuthConfig.getEnumClass()) && SourceTypeEnum.CUSTOM.key.equals(justAuthSource.getSourceType()))

  122. {

  123. try {

  124. Class enumConfigClass = Class.forName(justAuthConfig.getEnumClass());

  125. tenantIdAuthRequest = this.getExtendRequest(enumConfigClass, source, (ExtendProperties.ExtendRequestConfig) authConfig, gitEggRedisStateCache);

  126. } catch (ClassNotFoundException e) {

  127. log.error("初始化自定义第三方登录时发生异常:{}", e);

  128. }

  129. }

  130. }

  131. }

  132. } catch (Exception e) {

  133. log.error("获取第三方登录时发生异常:{}", e);

  134. }

  135. }

  136. if (null == tenantIdAuthRequest)

  137. {

  138. tenantIdAuthRequest = authRequestFactory.get(source);

  139. }

  140. return tenantIdAuthRequest;

  141. }

  142. /**

  143. * 获取单个的request

  144. * @param source

  145. * @return

  146. */

  147. private AuthRequest getDefaultRequest(String source, AuthConfig authConfig, GitEggRedisStateCache gitEggRedisStateCache) {

  148. AuthDefaultSource authDefaultSource;

  149. try {

  150. authDefaultSource = EnumUtil.fromString(AuthDefaultSource.class, source.toUpperCase());

  151. } catch (IllegalArgumentException var4) {

  152. return null;

  153. }

  154. // 从缓存获取租户单独配置

  155. switch(authDefaultSource) {

  156. case GITHUB:

  157. return new AuthGithubRequest(authConfig, gitEggRedisStateCache);

  158. case WEIBO:

  159. return new AuthWeiboRequest(authConfig, gitEggRedisStateCache);

  160. case GITEE:

  161. return new AuthGiteeRequest(authConfig, gitEggRedisStateCache);

  162. case DINGTALK:

  163. return new AuthDingTalkRequest(authConfig, gitEggRedisStateCache);

  164. case DINGTALK_ACCOUNT:

  165. return new AuthDingTalkAccountRequest(authConfig, gitEggRedisStateCache);

  166. case BAIDU:

  167. return new AuthBaiduRequest(authConfig, gitEggRedisStateCache);

  168. case CSDN:

  169. return new AuthCsdnRequest(authConfig, gitEggRedisStateCache);

  170. case CODING:

  171. return new AuthCodingRequest(authConfig, gitEggRedisStateCache);

  172. case OSCHINA:

  173. return new AuthOschinaRequest(authConfig, gitEggRedisStateCache);

  174. case ALIPAY:

  175. return new AuthAlipayRequest(authConfig, gitEggRedisStateCache);

  176. case QQ:

  177. return new AuthQqRequest(authConfig, gitEggRedisStateCache);

  178. case WECHAT_OPEN:

  179. return new AuthWeChatOpenRequest(authConfig, gitEggRedisStateCache);

  180. case WECHAT_MP:

  181. return new AuthWeChatMpRequest(authConfig, gitEggRedisStateCache);

  182. case WECHAT_ENTERPRISE:

  183. return new AuthWeChatEnterpriseQrcodeRequest(authConfig, gitEggRedisStateCache);

  184. case WECHAT_ENTERPRISE_WEB:

  185. return new AuthWeChatEnterpriseWebRequest(authConfig, gitEggRedisStateCache);

  186. case TAOBAO:

  187. return new AuthTaobaoRequest(authConfig, gitEggRedisStateCache);

  188. case GOOGLE:

  189. return new AuthGoogleRequest(authConfig, gitEggRedisStateCache);

  190. case FACEBOOK:

  191. return new AuthFacebookRequest(authConfig, gitEggRedisStateCache);

  192. case DOUYIN:

  193. return new AuthDouyinRequest(authConfig, gitEggRedisStateCache);

  194. case LINKEDIN:

  195. return new AuthLinkedinRequest(authConfig, gitEggRedisStateCache);

  196. case MICROSOFT:

  197. return new AuthMicrosoftRequest(authConfig, gitEggRedisStateCache);

  198. case MI:

  199. return new AuthMiRequest(authConfig, gitEggRedisStateCache);

  200. case TOUTIAO:

  201. return new AuthToutiaoRequest(authConfig, gitEggRedisStateCache);

  202. case TEAMBITION:

  203. return new AuthTeambitionRequest(authConfig, gitEggRedisStateCache);

  204. case RENREN:

  205. return new AuthRenrenRequest(authConfig, gitEggRedisStateCache);

  206. case PINTEREST:

  207. return new AuthPinterestRequest(authConfig, gitEggRedisStateCache);

  208. case STACK_OVERFLOW:

  209. return new AuthStackOverflowRequest(authConfig, gitEggRedisStateCache);

  210. case HUAWEI:

  211. return new AuthHuaweiRequest(authConfig, gitEggRedisStateCache);

  212. case GITLAB:

  213. return new AuthGitlabRequest(authConfig, gitEggRedisStateCache);

  214. case KUJIALE:

  215. return new AuthKujialeRequest(authConfig, gitEggRedisStateCache);

  216. case ELEME:

  217. return new AuthElemeRequest(authConfig, gitEggRedisStateCache);

  218. case MEITUAN:

  219. return new AuthMeituanRequest(authConfig, gitEggRedisStateCache);

  220. case TWITTER:

  221. return new AuthTwitterRequest(authConfig, gitEggRedisStateCache);

  222. case FEISHU:

  223. return new AuthFeishuRequest(authConfig, gitEggRedisStateCache);

  224. case JD:

  225. return new AuthJdRequest(authConfig, gitEggRedisStateCache);

  226. case ALIYUN:

  227. return new AuthAliyunRequest(authConfig, gitEggRedisStateCache);

  228. case XMLY:

  229. return new AuthXmlyRequest(authConfig, gitEggRedisStateCache);

  230. case AMAZON:

  231. return new AuthAmazonRequest(authConfig, gitEggRedisStateCache);

  232. case SLACK:

  233. return new AuthSlackRequest(authConfig, gitEggRedisStateCache);

  234. case LINE:

  235. return new AuthLineRequest(authConfig, gitEggRedisStateCache);

  236. case OKTA:

  237. return new AuthOktaRequest(authConfig, gitEggRedisStateCache);

  238. default:

  239. return null;

  240. }

  241. }

  242. private AuthRequest getExtendRequest(Class clazz, String source, ExtendProperties.ExtendRequestConfig extendRequestConfig, GitEggRedisStateCache gitEggRedisStateCache) {

  243. String upperSource = source.toUpperCase();

  244. try {

  245. EnumUtil.fromString(clazz, upperSource);

  246. } catch (IllegalArgumentException var8) {

  247. return null;

  248. }

  249. if (extendRequestConfig != null) {

  250. Class requestClass = extendRequestConfig.getRequestClass();

  251. if (requestClass != null) {

  252. return (AuthRequest) ReflectUtil.newInstance(requestClass, new Object[]{extendRequestConfig, gitEggRedisStateCache});

  253. }

  254. }

  255. return null;

  256. }

  257. }

  6、登录后注册或绑定用户

  实现了第三方登录功能,我们自己的系统也需要做相应的用户匹配,通过OAuth2协议我们可以了解到,单点登录成功后可以获取第三方系统的用户信息,当然,具体获取到第三方用户的哪些信息是由第三方系统决定的。所以目前大多数系统平台再第三方登录成功之后,都会显示用户注册或绑定页面,将第三方用户和自有系统平台用户进行绑定。那么在下一次第三方登录成功之后,就会自动匹配到自有系统的用户,进一步的获取到该用户在自有系统的权限、菜单等。

  JustAuth官方提供的账户整合流程图:

  我们通常的第三方登录业务流程是点击登录,获取到第三方授权时,会去查询自有系统数据是否有匹配的用户,如果有,则自动登录到后台,如果没有,则跳转到账号绑定或者注册页面,进行账户绑定或者注册。我们将此业务流程放到gitegg-oauth微服务中去实现,新建SocialController类:

  复制

  1. /**

  2. * 第三方登录

  3. * @author GitEgg

  4. */

  5. @Slf4j

  6. @RestController

  7. @RequestMapping("/social")

  8. @RequiredArgsConstructor(onConstructor_ = @Autowired)

  9. public class SocialController {

  10. private final GitEggAuthRequestFactory factory;

  11. private final IJustAuthFeign justAuthFeign;

  12. private final IUserFeign userFeign;

  13. private final ISmsFeign smsFeign

  14. @Value("${system.secret-key}")

  15. private String secretKey;

  16. @Value("${system.secret-key-salt}")

  17. private String secretKeySalt;

  18. private final RedisTemplate redisTemplate;

  19. /**

  20. * 密码最大尝试次数

  21. */

  22. @Value("${system.maxTryTimes}")

  23. private int maxTryTimes;

  24. /**

  25. * 锁定时间,单位 秒

  26. */

  27. @Value("${system.maxTryTimes}")

  28. private long maxLockTime;

  29. /**

  30. * 第三方登录缓存时间,单位 秒

  31. */

  32. @Value("${system.socialLoginExpiration}")

  33. private long socialLoginExpiration;

  34.

      35. @GetMapping

  36. public List list() {

  37. return factory.oauthList();

  38. }

  39. /**

  40. * 获取到对应类型的登录url

  41. * @param type

  42. * @return

  43. */

  44. @GetMapping("/login/{type}")

  45. public Result login(@PathVariable String type) {

  46. AuthRequest authRequest = factory.get(type);

  47. return Result.data(authRequest.authorize(AuthStateUtils.createState()));

  48. }

  49. /**

  50. * 保存或更新用户数据,并进行判断是否进行注册或绑定

  51. * @param type

  52. * @param callback

  53. * @return

  54. */

  55. @RequestMapping("/{type}/callback")

  56. public Result login(@PathVariable String type, AuthCallback callback) {

  57. AuthRequest authRequest = factory.get(type);

  58. AuthResponse response = authRequest.login(callback);

  59. if (response.ok())

  60. {

  61. AuthUser authUser = (AuthUser) response.getData();

  62. JustAuthSocialInfoDTO justAuthSocialInfoDTO = BeanCopierUtils.copyByClass(authUser, JustAuthSocialInfoDTO.class);

  63. BeanCopierUtils.copyByObject(authUser.getToken(), justAuthSocialInfoDTO);

  64. // 获取到第三方用户信息后,先进行保存或更新

  65. Result<Object> createResult = justAuthFeign.userCreateOrUpdate(justAuthSocialInfoDTO);

  66. if(createResult.isSuccess() && null != createResult.getData())

  67. {

  68. Long socialId = Long.parseLong((String)createResult.getData());

  69. // 判断此第三方用户是否被绑定到系统用户

      70. Result<Object>bindResult = justAuthFeign.userBindQuery(socialId);

  71. // 这里需要处理返回消息,前端需要根据返回是否已经绑定好的消息来判断

  72. // 将socialId进行加密返回

  73. DES des = new DES(Mode.CTS, Padding.PKCS5Padding, secretKey.getBytes(), secretKeySalt.getBytes());

  74. // 这里将source+uuid通过des加密作为key返回到前台

  75. String socialKey = authUser.getSource() + StrPool.UNDERLINE + authUser.getUuid();

  76. // 将socialKey放入缓存,默认有效期2个小时,如果2个小时未完成验证,那么操作失效,重新获取,在system:socialLoginExpiration配置

  77. redisTemplate.opsForValue().set(AuthConstant.SOCIAL_VALIDATION_PREFIX + socialKey, createResult.getData(), socialLoginExpiration,

  78. TimeUnit.SECONDS);

  79. String desSocialKey = des.encryptHex(socialKey);

  80. bindResult.setData(desSocialKey);

  81. // 这里返回的成功是请求成功,里面放置的result是是否有绑定用户的成功

  82. return Result.data(bindResult);

  83. }

  84. return Result.error("获取第三方用户绑定信息失败");

  85. }

  86. else

  87. {

  88. throw new BusinessException(response.getMsg());

  89. }

  90. }

  91. /**

  92. * 绑定用户手机号

  93. * 这里不走手机号登录的流程,因为如果手机号不存在那么可以直接创建一个用户并进行绑定

  94. */

  95. @PostMapping("/bind/mobile")

  96. @ApiOperation(value = "绑定用户手机号")

  97. public Result bindMobile(@Valid @RequestBody SocialBindMobileDTO socialBind) {

  98. Result smsResult = smsFeign.checkSmsVerificationCode(socialBind.getSmsCode(), socialBind.getPhoneNumber(), socialBind.getCode());

  99. // 判断短信验证是否成功

  100. if (smsResult.isSuccess() && null != smsResult.getData() && (Boolean)smsResult.getData()) {

  101. // 解密前端传来的socialId

  102. DES des = new DES(Mode.CTS, Padding.PKCS5Padding, secretKey.getBytes(), secretKeySalt.getBytes());

  103. String desSocialKey = des.decryptStr(socialBind.getSocialKey());

  104.

      105. // 将socialKey放入缓存,默认有效期2个小时,如果2个小时未完成验证,那么操作失效,重新获取,在system:socialLoginExpiration配置

  106. String desSocialId = (String)redisTemplate.opsForValue().get(AuthConstant.SOCIAL_VALIDATION_PREFIX + desSocialKey);

      107. 

  108. // 查询第三方用户信息

  109. Result justAuthInfoResult = justAuthFeign.querySocialInfo(Long.valueOf(desSocialId));

      110. 

  111. if (null == justAuthInfoResult || !justAuthInfoResult.isSuccess() || null == justAuthInfoResult.getData())

  112. {

  113. throw new BusinessException("未查询到第三方用户信息,请返回到登录页重试");

  114. }

  115. JustAuthSocialInfoDTO justAuthSocialInfoDTO = BeanUtil.copyProperties(justAuthInfoResult.getData(), JustAuthSocialInfoDTO.class);

  116. // 查询用户是否存在,如果存在,那么直接调用绑定接口

  117. Result<Object>result = userFeign.queryUserByPhone(socialBind.getPhoneNumber());

  118. Long userId;

  119. // 判断返回信息

  120. if (null != result && result.isSuccess() && null != result.getData()) {

  121. GitEggUser gitEggUser = BeanUtil.copyProperties(result.getData(), GitEggUser.class);

  122. userId = gitEggUser.getId();

  123. }

  124. else

  125. {

  126. // 如果用户不存在,那么调用新建用户接口,并绑定

  127. UserAddDTO userAdd = new UserAddDTO();

  128. userAdd.setAccount(socialBind.getPhoneNumber());

  129. userAdd.setMobile(socialBind.getPhoneNumber());

  130. userAdd.setNickname(justAuthSocialInfoDTO.getNickname());

  131. userAdd.setPassword(StringUtils.isEmpty(justAuthSocialInfoDTO.getUnionId()) ? justAuthSocialInfoDTO.getUuid() : justAuthSocialInfoDTO.getUnionId());

  132. userAdd.setStatus(GitEggConstant.UserStatus.ENABLE);

  133. userAdd.setAvatar(justAuthSocialInfoDTO.getAvatar());

  134. userAdd.setEmail(justAuthSocialInfoDTO.getEmail());

  135. userAdd.setStreet(justAuthSocialInfoDTO.getLocation());

  136. userAdd.setComments(justAuthSocialInfoDTO.getRemark());

  137. Result<?>resultUserAdd = userFeign.userAdd(userAdd);

  138. if (null != resultUserAdd && resultUserAdd.isSuccess() && null != resultUserAdd.getData())

  139. {

  140. userId = Long.parseLong((String) resultUserAdd.getData());

  141. }

  142. else

  143 {

  144. // 如果添加失败,则返回失败信息

  145. return resultUserAdd;

  146. }

  147. }

  148. // 执行绑定操作

  149. return justAuthFeign.userBind(Long.valueOf(desSocialId), userId);

  150. }

  151. return smsResult;

  152. }

  153. /**

  154. * 绑定账号

  155. * 这里只有绑定操作,没有创建用户操作

  156. */

  157. @PostMapping("/bind/account")

  158. @ApiOperation(value = "绑定用户账号")

  159. public<?>Result bindAccount(@Valid @RequestBody SocialBindAccountDTO socialBind) {

  160. // 查询用户是否存在,如果存在,那么直接调用绑定接口

  161. Result<?>result = userFeign.queryUserByAccount(socialBind.getUsername());

  162. // 判断返回信息

  163. if (null != result && result.isSuccess() && null != result.getData()) {

  164. GitEggUser gitEggUser = BeanUtil.copyProperties(result.getData(), GitEggUser.class);

  165. // 必须添加次数验证,和登录一样,超过最大验证次数那么直接锁定账户

  166. // 从Redis获取账号密码错误次数

  167. Object lockTimes = redisTemplate.boundValueOps(AuthConstant.LOCK_ACCOUNT_PREFIX + gitEggUser.getId()).get();

  168. // 判断账号密码输入错误几次,如果输入错误多次,则锁定账号

  169. if(null != lockTimes && (int)lockTimes >= maxTryTimes){

  170. throw new BusinessException("密码尝试次数过多,请使用其他方式绑定");

  171. }

  172. PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

  173. String password = AuthConstant.BCRYPT + gitEggUser.getAccount() + DigestUtils.md5DigestAsHex(socialBind.getPassword().getBytes());

  174. // 验证账号密码是否正确

  175. if ( passwordEncoder.matches(password, gitEggUser.getPassword()))

  176. {

  177. // 解密前端传来的socialId

  178. DES des = new DES(Mode.CTS, Padding.PKCS5Padding, secretKey.getBytes(), secretKeySalt.getBytes());

  179. String desSocialKey = des.decryptStr(socialBind.getSocialKey());

  180. // 将socialKey放入缓存,默认有效期2个小时,如果2个小时未完成验证,那么操作失效,重新获取,在system:socialLoginExpiration配置

  181. String desSocialId = (String)redisTemplate.opsForValue().get(AuthConstant.SOCIAL_VALIDATION_PREFIX + desSocialKey);

  182.

      183. // 执行绑定操作

  184. return justAuthFeign.userBind(Long.valueOf(desSocialId), gitEggUser.getId());

  185. }

  186. else

  187. {

  188. // 增加锁定次数

  189. redisTemplate.boundValueOps(AuthConstant.LOCK_ACCOUNT_PREFIX + gitEggUser.getId()).increment(GitEggConstant.Number.ONE);

  190. redisTemplate.expire(AuthConstant.LOCK_ACCOUNT_PREFIX +gitEggUser.getId(), maxLockTime , TimeUnit.SECONDS);

  191. throw new BusinessException("账号或密码错误");

  192. }

  193. }

  194. else

  195. {

  196. throw new BusinessException("账号不存在");

  197. }

  198. }

      199. 

  200. }

  7、所有的配置和绑定注册功能实现之后,我们还需要实现关键的一步,就是自定义实现OAuth2的第三方登录模式SocialTokenGranter,在第三方授权之后,通过此模式进行登录,自定义实现之后,记得t_oauth_client_details表需增加social授权。 

  SocialTokenGranter.java。

  1. /**

  2. * 第三方登录模式

  3. * @author GitEgg

  4. */

  5. public class SocialTokenGranter extends AbstractTokenGranter {

  6. private static final String GRANT_TYPE = "social";

  7. private final AuthenticationManager authenticationManager;

  8. private UserDetailsService userDetailsService;

  9. private IJustAuthFeign justAuthFeign;

  10. private RedisTemplate redisTemplate;

  11. private String captchaType;

  12. private String secretKey;

  13. private String secretKeySalt;

  14. public SocialTokenGranter(AuthenticationManager authenticationManager,

  15. AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService,

  16. OAuth2RequestFactory requestFactory, RedisTemplate redisTemplate, IJustAuthFeign justAuthFeign,

  17. UserDetailsService userDetailsService, String captchaType, String secretKey, String secretKeySalt) {

  18. this(authenticationManager, tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);

  19. this.redisTemplate = redisTemplate;

  20. this.captchaType = captchaType;

  21. this.secretKey = secretKey;

  22. this.secretKeySalt = secretKeySalt;

  23. this.justAuthFeign = justAuthFeign;

  24. this.userDetailsService = userDetailsService;

  25. }

  26. protected SocialTokenGranter(AuthenticationManager authenticationManager,

  27. AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService,

  28. OAuth2RequestFactory requestFactory, String grantType) {

  29. super(tokenServices, clientDetailsService, requestFactory, grantType);

  30. this.authenticationManager = authenticationManager;

  31. }

  32. @Override

  33. protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {

  34. Map<string, string=""> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters());

  35. String socialKey = parameters.get(TokenConstant.SOCIAL_KEY);

  36. // Protect from downstream leaks of password

  37. parameters.remove(TokenConstant.SOCIAL_KEY);

  38. // 校验socialId

  39. String socialId;

  40. try {

  41. // 将socialId进行加密返回

  42. DES des = new DES(Mode.CTS, Padding.PKCS5Padding, secretKey.getBytes(), secretKeySalt.getBytes());

  43. String desSocialKey = des.decryptStr(socialKey);

  44. // 获取缓存中的key

  45. socialId = (String) redisTemplate.opsForValue().get(AuthConstant.SOCIAL_VALIDATION_PREFIX + desSocialKey);

  46. }

  47. catch (Exception e)

  48. {

  49. throw new InvalidGrantException("第三方登录验证已失效,请返回登录页重新操作");

  50. }

  51. if (StringUtils.isEmpty(socialId))

  52. {

  53. throw new InvalidGrantException("第三方登录验证已失效,请返回登录页重新操作");

  54. }

  55. // 校验userId

  56. String userId;

  57. try {

  58. Result socialResult = justAuthFeign.userBindQuery(Long.parseLong(socialId));

  59. if (null == socialResult || StringUtils.isEmpty(socialResult.getData())) {

  60. throw new InvalidGrantException("操作失败,请返回登录页重新操作");

  61. }

  62. userId = (String) socialResult.getData();

  63. }

  64. catch (Exception e)

  65. {

  66. throw new InvalidGrantException("操作失败,请返回登录页重新操作");

  67. }

  68. if (StringUtils.isEmpty(userId))

  69. {

  70. throw new InvalidGrantException("操作失败,请返回登录页重新操作");

  71. }

  72. // 这里是通过用户id查询用户信息

  73. UserDetails userDetails = this.userDetailsService.loadUserByUsername(userId);

  74. Authentication userAuth = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());

  75. ((AbstractAuthenticationToken)userAuth).setDetails(parameters);

  76. OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);

  77. return new OAuth2Authentication(storedOAuth2Request, userAuth);

  78. }

  79. }

  8、后台处理完成之后,前端VUE也需要做回调处理

  因为是前后端分离的项目,我们这里需要将第三方回调接口配置在vue页面,前端页面根据账户信息判断是直接登录还是进行绑定或者注册等操作。新建SocialCallback.vue用于处理前端第三方登录授权后的回调操作。SocialCallback.vue。

  复制

  1. <template>

  2. <div>

  3. </div>

  4. </template>

  5. <script>

  6. import { socialLoginCallback } from '@/api/login'

  7. import { mapActions } from 'vuex'

  8. export default {

  9. name: 'SocialCallback',

  10. created () {

  11. this.$loading.show({ tip: '登录中......' })

  12. const query = this.$route.query

  13. const socialType = this.$route.params.socialType

  14. this.socialCallback(socialType, query)

  15. },

  16. methods: {

  17. ...mapActions(['Login']),

  18. getUrlKey: function (name) {

  19. // eslint-disable-next-line no-sparse-arrays

  20. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.opener.location.href) || [, ''])[1].replace(/\\+/g, '%20')) || null

  21. },

  22. socialCallback (socialType, parameter) {

  23. const that = this

  24. socialLoginCallback(socialType, parameter).then(res => {

  25. that.$loading.hide()

  26. const bindResult = res.data

  27. if (bindResult && bindResult !== '') {

  28. if (bindResult.success && bindResult.data) {

  29. // 授权后发现已绑定,那么直接调用第三方登录

  30. this.socialLogin(bindResult.data)

  31. } else if (bindResult.code === 601) {

  32. // 授权后没有绑定则跳转到绑定界面

  33. that.$router.push({ name: 'socialBind', query: { redirect: this.getUrlKey('redirect'), key: bindResult.data } })

  34. } else if (bindResult.code === 602) {

  35. // 该账号已绑定多个账号,请联系系统管理员,或者到个人中心解绑

  36. this.$notification['error']({

  37. message: '错误',

  38. description: ((res.response || {}).data || {}).message || '该账号已绑定多个账号,请联系系统管理员,或者到个人中心解绑',

  39. duration: 4

  40. })

  41. } else {

  42. // 提示获取第三方登录失败

  43. this.$notification['error']({

  44. message: '错误',

  45. description: '第三方登录失败,请稍后再试',

  46. duration: 4

  47. })

  48. }

  49. } else {

  50. // 提示获取第三方登录失败

  51. this.$notification['error']({

  52. message: '错误',

  53. description: '第三方登录失败,请稍后再试',

  54. duration: 4

  55. })

  56. }

  57. })

  58. },

  59. // 第三方登录后回调

  60. socialLogin (key) {

  61. const { Login } = this

  62. // 执行登录操作

  63. const loginParams = {

  64. grant_type: 'social',

  65. social_key: key

  66. }

  67. this.$loading.show({ tip: '登录中......' })

  68. Login(loginParams)

  69. .then((res) => this.loginSuccess(res))

  70. .catch(err => this.loginError(err))

  71. .finally(() => {

  72. this.$loading.hide()

  73. if (this.getUrlKey('redirect')) {

  74. window.opener.location.href = window.opener.location.origin + this.getUrlKey('redirect')

  75. } else {

  76. window.opener.location.reload()

  77. }

  78. window.close()

  79. })

  80. },

  81. loginSuccess (res) {

  82. this.$notification['success']({

  83. message: '提示',

  84. description: '第三方登录成功',

  85. duration: 4

  86. })

  87. },

  88. loginError (err) {

  89. this.$notification['error']({

  90. message: '错误',

  91. description: ((err.response || {}).data || {}).message || '请求出现错误,请稍后再试',

  92. duration: 4

  93. })

  94. }

  95. }

  96. }

  97. </script>

  98. <style>

  99. </style>

  二、登录和绑定测试

  JustAuth官方提供了详细的第三方登录的使用指南,按照其介绍,到需要的第三方网站申请,然后进行配置即可,这里只展示GitHub的登录测试步骤。

  按照官方提供的注册申请步骤,获取到GitHub的client-id和client-secret并配置回调地址redirect-uri。

  Nacos配置。

  复制

  1. client-id: 59ced49784f3cebfb208

  2. client-secret: 807f52cc33a1aae07f97521b5501adc6f36375c8

  3. redirect-uri: http://192.168.0.2:8000/social/github/callback

  4. ignore-check-state: false

  或者使用多租户系统配置 ,每个租户仅允许有一个主配置2、登录页添加Github登录链接。

  复制

  1. <div class="user-login-other">

  2. <span>{{ $t('user.login.sign-in-with') }} </span>

  3. <a @click="openSocialLogin('wechat_open')">

  4. <a-icon class="item-icon"

  5. type="wechat"> </a-icon>

  6. </a>

  7. <a @click="openSocialLogin('qq')">

  8. <a-icon class="item-icon"

  9. type="qq">  </a-icon>

  10. </a>

  11. <a @click="openSocialLogin('github')">

  12. <a-icon class="item-icon"

  13. type="github"> </a-icon>

  14. </a>

  15. <a @click="openSocialLogin('dingtalk')">

  16. <a-icon class="item-icon"

  17. type="dingding"> </a-icon>

  18. </a>

  19. <a class="register"

  20. @click="openRegister"

  21. >{{ $t('user.login.signup') }}

  22. </a>

  23. </div>

   

    >>>>>>点击进入编程语言专题

赞(11)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程