From 4251d4960be2b2d352ada44e011d85c88c741b9a Mon Sep 17 00:00:00 2001 From: zjf <1799650030@qq.com> Date: Fri, 6 Dec 2024 18:43:15 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=8D=95=E7=82=B9-1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pjcloud-app-server-biz/pom.xml | 6 + .../AppUserAutoLoginController.java | 127 ++++++++++++++++-- .../core/constant/ServiceNameConstants.java | 13 ++ .../feign/RemoterThirdPartyAuthService.java | 47 +++++++ .../controller/ThirdPartyOAth2Cotroller.java | 39 ++++++ .../src/main/resources/application.yml | 4 +- .../resources/mapper/ThirdPartyDeptMapper.xml | 4 +- .../resources/mapper/ThirdPartyUserMapper.xml | 4 +- 8 files changed, 227 insertions(+), 17 deletions(-) create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-api/src/main/java/com/bsd/pjcloud/thirdparty/api/feign/RemoterThirdPartyAuthService.java create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java diff --git a/pjcloud-app-server/pjcloud-app-server-biz/pom.xml b/pjcloud-app-server/pjcloud-app-server-biz/pom.xml index e3f7e096a..b09ae7397 100644 --- a/pjcloud-app-server/pjcloud-app-server-biz/pom.xml +++ b/pjcloud-app-server/pjcloud-app-server-biz/pom.xml @@ -64,6 +64,12 @@ pjcloud-app-server-api 5.7.0 + + + com.bsd + pjcloud-thirdparty-api + 5.7.0 + com.bsd diff --git a/pjcloud-app-server/pjcloud-app-server-biz/src/main/java/com/bsd/pjcloud/app/controller/AppUserAutoLoginController.java b/pjcloud-app-server/pjcloud-app-server-biz/src/main/java/com/bsd/pjcloud/app/controller/AppUserAutoLoginController.java index 4e0cddcd4..38fbdc72d 100644 --- a/pjcloud-app-server/pjcloud-app-server-biz/src/main/java/com/bsd/pjcloud/app/controller/AppUserAutoLoginController.java +++ b/pjcloud-app-server/pjcloud-app-server-biz/src/main/java/com/bsd/pjcloud/app/controller/AppUserAutoLoginController.java @@ -20,18 +20,23 @@ package com.bsd.pjcloud.app.controller; import com.bsd.pjcloud.admin.api.dto.UserTokenDTO; import com.bsd.pjcloud.admin.api.feign.RemoteTokenService; +import com.bsd.pjcloud.admin.api.feign.RemoteUserService; import com.bsd.pjcloud.app.service.AppUserService; import com.bsd.pjcloud.common.core.util.R; import com.bsd.pjcloud.common.security.annotation.Inner; +import com.bsd.pjcloud.thirdparty.api.feign.RemoterThirdPartyAuthService; +import com.bsd.pjcloud.thirdparty.dto.ZZDUserDTO; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpHeaders; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.security.oauth2.core.OAuth2AccessToken; +import org.springframework.security.oauth2.core.OAuth2RefreshToken; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken; +import org.springframework.web.bind.annotation.*; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -53,26 +58,126 @@ public class AppUserAutoLoginController { private final RemoteTokenService remoteTokenService; + private final RemoteUserService remoteUserService; + + private final RemoterThirdPartyAuthService remoterThirdPartyAuthService; + + /* + * + * @param token 浙政钉前端获取的token + * @return + */ + @GetMapping("/zzdsso/{token}") + @Operation(summary = "浙政钉单点登录") + public R zzdsso(@PathVariable(name = "token") String token) { + + //获取数字政务token通过浙政钉前端获取的token,调试阶段先写死 +// String json = "{" + +// "\"username\": \"李宗相\"," + +// "\"organizationId\": 2," + +// "\"userId\": 1340," + +// "\"nickName\": \"李宗相\"," + +// "\"mobile\": \"18576496089\"," + +// "\"regionCode\": null," + +// "\"ext\": {" + +// "\"employeeCode\": \"GE_ae12bdf21598498eb48bf2cf2c45a3e1\"," + +// "\"organizationCode\": \"GO_63e77077d5e041789bbbe1d0d20bcf41\"," + +// "\"nickNameCn\": \"李宗相\"," + +// "\"openid\": \"1b51f0e11fc73b8adddcbaf7982daab6\"," + +// "\"realmId\": \"196729\"," + +// "\"mobile\": null" + +// "}" + +// "}"; + + +// ObjectMapper objectMapper = new ObjectMapper(); +// ZZDUserDTO zzdUserDTO; +// try { +// zzdUserDTO = objectMapper.readValue(json, ZZDUserDTO.class); +// } catch (JsonProcessingException e) { +// throw new RuntimeException(e); +// } + //暂时先注释,模拟一下就行 + R zzdsso = remoterThirdPartyAuthService.zzdsso(token); + ZZDUserDTO zzdUserDTO = (ZZDUserDTO) zzdsso.getData(); + ZZDUserDTO.Ext ext = zzdUserDTO.getExt(); + String employeeCode = ext.getEmployeeCode(); + String mobile = zzdUserDTO.getMobile(); + System.out.println(employeeCode); + System.out.println(mobile); + //两种方式,手机号+员工钉钉号,查到相同就视为同一个人,然后登录 + //先系统表里面查看是否有,在第三方数据库表里面看下是否有 + AdminUserRespDTO adminUserRespDTO =adminUserApi.getUserByMobileOrEmployeeCode(employeeCode,mobile); + if(adminUserRespDTO!=null){ + //登录成功 + return createTokenAfterLoginSuccess(adminUserRespDTO.getId(), zzdUserDTO.getUsername(), LoginLogTypeEnum.LOGIN_SSO); + } + log.info("用户不存在,尝试创建用户"); + CreateUserDto createUserDto = new CreateUserDto(); + createUserDto.setUsername(zzdUserDTO.getUsername()); + createUserDto.setMobile(mobile); + createUserDto.setNickname(zzdUserDTO.getNickName()); + createUserDto.setDingtalkEmployeeCode(employeeCode); + //todo 换成枚举 + //浙里办用户 + createUserDto.setUserType("1"); + AdminUserRespDTO adminUserRespDTONew=adminUserApi.createSysUser(createUserDto); + log.info("创建用户成功,用户id为:{}",adminUserRespDTONew.getId()); + return createTokenAfterLoginSuccess(adminUserRespDTONew.getId(), mobile, LoginLogTypeEnum.LOGIN_SSO); + } + + + /** + * 诗画sdk获取参数 + * + * @param + * @return + */ + @PostMapping("/shjnsso") + @Operation(summary = "诗画江南单点登录") + public R shjnsso(@RequestBody @Valid SHJNSSOReqVO shjnsso) { + ZZDUserDTO zzdUserDTO=ssoApi.shjnsso(shjnsso.getAccountId(),shjnsso.getSessionId()); + ZZDUserDTO.Ext ext = zzdUserDTO.getExt(); + String mobile = zzdUserDTO.getMobile(); + AdminUserRespDTO userByMobileOrEmployeeCode = adminUserApi.getUserByMobileOrEmployeeCode(null, mobile); + if(userByMobileOrEmployeeCode!=null){ + return createTokenAfterLoginSuccess(userByMobileOrEmployeeCode.getId(), mobile, LoginLogTypeEnum.LOGIN_SSO); + } + log.info("用户不存在,尝试创建用户"); + CreateUserDto createUserDto = new CreateUserDto(); + createUserDto.setUsername(zzdUserDTO.getUsername()); + createUserDto.setMobile(mobile); + createUserDto.setNickname(zzdUserDTO.getNickName()); + createUserDto.setAvatar(ext.getHeadImgUrl()); + //todo 换成枚举 + //诗画用户 + createUserDto.setUserType("2"); + AdminUserRespDTO adminUserRespDTONew=adminUserApi.createSysUser(createUserDto); + log.info("创建用户成功,用户id为:{}",adminUserRespDTONew.getId()); + return createTokenAfterLoginSuccess(adminUserRespDTONew.getId(), mobile, LoginLogTypeEnum.LOGIN_SSO); + } + + @Inner(value = false) @GetMapping("/getAuthToken/{token}") public R info(@PathVariable(name = "token") String token) { + + //调用第三方获取基础信息与数据库进行匹配 // 拼接请求参数 UserTokenDTO userTokenDTO = new UserTokenDTO(); userTokenDTO.setUsername("admin"); // 只需要用户名,就可以生成 token userTokenDTO.setAuthorities(List.of("sys_user_add")); // 权限列表的字符串,能调用的接口范围 // feign 调用 auth 模块生成 token -// R authenticationTokenR = remoteTokenService.generateToken(userTokenDTO); R authenticationTokenR = remoteTokenService.generateToken(userTokenDTO); - // token 信息 -// OAuth2AccessTokenAuthenticationToken data = authenticationTokenR.getData(); -// OAuth2AccessToken accessToken = data.getAccessToken(); -// // 刷新 token 信息 -// OAuth2RefreshToken refreshToken = data.getRefreshToken(); // // 客户端信息 // RegisteredClient registeredClient = data.getRegisteredClient(); Object accessToken = authenticationTokenR.getData().get("accessToken"); - return R.ok(accessToken); + Object refreshToken = authenticationTokenR.getData().get("refreshToken"); + Map stringObjectHashMap = new HashMap<>(); + stringObjectHashMap.put("accessToken", accessToken); + stringObjectHashMap.put("refreshToken", refreshToken); + return R.ok(stringObjectHashMap); } // // /** diff --git a/pjcloud-common/pjcloud-common-core/src/main/java/com/bsd/pjcloud/common/core/constant/ServiceNameConstants.java b/pjcloud-common/pjcloud-common-core/src/main/java/com/bsd/pjcloud/common/core/constant/ServiceNameConstants.java index 5c4702ba3..20bee8160 100644 --- a/pjcloud-common/pjcloud-common-core/src/main/java/com/bsd/pjcloud/common/core/constant/ServiceNameConstants.java +++ b/pjcloud-common/pjcloud-common-core/src/main/java/com/bsd/pjcloud/common/core/constant/ServiceNameConstants.java @@ -55,4 +55,17 @@ public interface ServiceNameConstants { */ String CODEGEN_SERVICE = "pjcloud-codegen"; + + /** + * 第三方服务 + */ + String THIRDPARTY_SERVICE = "pjcloud-thirdparty-biz"; + + + /** + * 智慧食堂服务 + */ + String CANTEEN_SERVICE = "pjcloud-canteen-biz"; + + } diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-api/src/main/java/com/bsd/pjcloud/thirdparty/api/feign/RemoterThirdPartyAuthService.java b/pjcloud-thirdparty/pjcloud-thirdparty-api/src/main/java/com/bsd/pjcloud/thirdparty/api/feign/RemoterThirdPartyAuthService.java new file mode 100644 index 000000000..7c9820d49 --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-api/src/main/java/com/bsd/pjcloud/thirdparty/api/feign/RemoterThirdPartyAuthService.java @@ -0,0 +1,47 @@ +/* + * + * Copyright (c) 2018-2025, pjcloud All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: pjcloud + * + */ + +package com.bsd.pjcloud.thirdparty.api.feign; +import com.bsd.pjcloud.common.core.constant.ServiceNameConstants; +import com.bsd.pjcloud.common.core.util.R; +import com.bsd.pjcloud.common.feign.annotation.NoToken; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @author pjcloud + * @date 2018/6/22 + */ +@FeignClient(contextId = "remoterThirdPartyAuthService", value = ServiceNameConstants.THIRDPARTY_SERVICE) +public interface RemoterThirdPartyAuthService { + + /** + * zzd单点 + * @param token + * @return + */ + @NoToken + @GetMapping("/thirdPartyOAth2/zzd/sso") + R zzdsso(@RequestParam("token") String token); + + + +} diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java new file mode 100644 index 000000000..965a29124 --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java @@ -0,0 +1,39 @@ +package com.bsd.pjcloud.thirdparty.controller; + + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.bsd.pjcloud.common.core.util.R; +import com.bsd.pjcloud.thirdparty.entity.ThirdPartyDeptEntity; +import com.bsd.pjcloud.thirdparty.service.ThirdPartyOAuth2Service; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springdoc.core.annotations.ParameterObject; +import org.springframework.http.HttpHeaders; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/thirdPartyOAth2" ) +@Tag(description = "thirdPartyOAth2" , name = "第三方单点接口" ) +@SecurityRequirement(name = HttpHeaders.AUTHORIZATION) +public class ThirdPartyOAth2Cotroller { + + private final ThirdPartyOAuth2Service thirdPartyOAuth2Service; + + + + @Operation(summary = "浙政钉token" ) + @GetMapping("/zzd/sso" ) + public R getThirdPartyDeptPage(@RequestParam("token")String token) { + + return R.ok(); + } +} diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/application.yml b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/application.yml index 08e83c1a9..dccc955a0 100644 --- a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/application.yml +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/application.yml @@ -1,5 +1,5 @@ server: - port: 6060 + port: 6061 spring: application: @@ -22,4 +22,4 @@ spring: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root - url: jdbc:mysql://pjcloud-mysql:3306/pjcloud_demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true + url: jdbc:mysql://pjcloud-mysql:3306/pjcloudx_thirdparty?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyDeptMapper.xml b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyDeptMapper.xml index 58e2ff404..698d4440f 100644 --- a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyDeptMapper.xml +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyDeptMapper.xml @@ -1,9 +1,9 @@ - + - + diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyUserMapper.xml b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyUserMapper.xml index 58839d951..b14a7b5f5 100644 --- a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyUserMapper.xml +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyUserMapper.xml @@ -1,9 +1,9 @@ - + - +