From f316cadfdd20c70aeb35c12995c0069efcead2b6 Mon Sep 17 00:00:00 2001 From: zhanghua <314079846@qq.com> Date: 星期五, 29 八月 2025 21:27:19 +0800 Subject: [PATCH] 代码配置修改 --- ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java | 201 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 197 insertions(+), 4 deletions(-) diff --git a/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java b/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java index 86452e5..640c8e5 100644 --- a/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java +++ b/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java @@ -3,6 +3,21 @@ import cn.dev33.satoken.annotation.SaIgnore; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders; +import com.aliyun.dingtalkcontact_1_0.models.GetUserResponseBody; +import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest; +import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse; +import com.aliyun.teautil.models.RuntimeOptions; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.dingtalk.api.DefaultDingTalkClient; +import com.dingtalk.api.DingTalkClient; +import com.dingtalk.api.request.*; +import com.dingtalk.api.response.OapiUserGetbyunionidResponse; +import com.dingtalk.api.response.OapiV2UserGetResponse; +import com.taobao.api.ApiException; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -18,28 +33,35 @@ import org.dromara.common.core.utils.MessageUtils; import org.dromara.common.core.utils.StreamUtils; import org.dromara.common.core.utils.StringUtils; +import org.dromara.common.redis.utils.RedisUtils; import org.dromara.common.social.config.properties.SocialLoginConfigProperties; import org.dromara.common.social.config.properties.SocialProperties; import org.dromara.common.social.utils.SocialUtils; import org.dromara.common.tenant.helper.TenantHelper; import org.dromara.system.domain.SysClient; +import org.dromara.system.domain.SysUser; import org.dromara.system.domain.bo.SysTenantBo; import org.dromara.system.domain.vo.SysTenantVo; -import org.dromara.system.service.ISysClientService; -import org.dromara.system.service.ISysConfigService; -import org.dromara.system.service.ISysSocialService; -import org.dromara.system.service.ISysTenantService; +import org.dromara.system.mapper.SysUserMapper; +import org.dromara.system.service.*; +import org.dromara.system.uitil.AutoLoginUtil; import org.dromara.web.domain.vo.LoginTenantVo; import org.dromara.web.domain.vo.LoginVo; import org.dromara.web.domain.vo.TenantListVo; import org.dromara.web.service.IAuthStrategy; import org.dromara.web.service.SysLoginService; import org.dromara.web.service.SysRegisterService; +import org.dromara.web.utils.RZTHttpUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import com.aliyun.teaopenapi.models.Config; import java.net.URL; +import java.time.Duration; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 璁よ瘉 @@ -61,6 +83,9 @@ private final ISysTenantService tenantService; private final ISysSocialService socialUserService; private final ISysClientService clientService; + private final ISysUserService userService; + private final SysUserMapper userMapper; + private final RZTHttpUtils rzthttpUtils; /** @@ -186,4 +211,172 @@ return R.ok(vo); } + /** + * 鑷姩娉ㄥ唽骞剁櫥褰� + */ +// @SaCheckPermission("system:user:auto") + @GetMapping(value = "auto") + public R<LoginVo> auto(RegisterBody user) { + AutoLoginUtil.padding(user); + String clientId = "e5cd7e4891bf95d1d19206ce24a7b32e"; + //pc + SysClient client = clientService.queryByClientId(clientId); + //妫�楠屾槸鍚﹀瓨鍦ㄨ处鍙� + boolean exists = userMapper.exists(new LambdaQueryWrapper<SysUser>() + .eq(SysUser::getUserName, user.getUsername()) + .eq(SysUser::getStatus, "0") + .eq(SysUser::getDelFlag, "0")); + if (!exists) { + registerService.register(user); + } + + LoginBody loginBody = AutoLoginUtil.create(user, clientId); +// 鐧诲綍 + return R.ok(IAuthStrategy.login(loginBody, client)); + } + + + public static com.aliyun.dingtalkoauth2_1_0.Client authClient() throws Exception { + Config config = new Config(); + config.protocol = "https"; + config.regionId = "central"; + return new com.aliyun.dingtalkoauth2_1_0.Client(config); + } + + /** + * 鑾峰彇鐢ㄦ埛token + * + * @param authCode + * @return + * @throws Exception + */ + //鎺ュ彛鍦板潃锛氭敞鎰�/auth涓庨拤閽夌櫥褰曚笌鍒嗕韩鐨勫洖璋冨煙鍚嶅湴鍧�涓�鑷� + @RequestMapping(value = "/dingdingLogin", method = RequestMethod.GET) + public String getAccessToken(@RequestParam(value = "authCode") String authCode, @RequestParam(value = "code") String code) throws Exception { + + com.aliyun.dingtalkoauth2_1_0.Client client = authClient(); + GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest() + + //搴旂敤鍩虹淇℃伅-搴旂敤淇℃伅鐨凙ppKey,璇峰姟蹇呮浛鎹负寮�鍙戠殑搴旂敤AppKey + .setClientId("dingl5dxahaj3uzfug66") + + //搴旂敤鍩虹淇℃伅-搴旂敤淇℃伅鐨凙ppSecret锛�,璇峰姟蹇呮浛鎹负寮�鍙戠殑搴旂敤AppSecret + .setClientSecret("AGDu7NfzkverlMXq8CUDiy6EXx5jSL4v2p-Odz1mpXV5e4_K3kB1Acat0RftzBXC") + .setCode(authCode) + .setGrantType("authorization_code"); + GetUserTokenResponse getUserTokenResponse = client.getUserToken(getUserTokenRequest); + System.out.println(getUserTokenResponse.getBody()); + if ("dingf4816bf92d85d84435c2f4657eb6378f".equals(getUserTokenResponse.getBody().getCorpId())) { + + //鑾峰彇鐢ㄦ埛涓汉token + String accessToken = getUserTokenResponse.getBody().getAccessToken(); + GetUserResponseBody userResponseBody = getUserinfoNoId(accessToken); + return userResponseBody.mobile; + } + + + return ""; + } + + public static com.aliyun.dingtalkcontact_1_0.Client contactClient() throws Exception { + Config config = new Config(); + config.protocol = "https"; + config.regionId = "central"; + return new com.aliyun.dingtalkcontact_1_0.Client(config); + } + + /** + * 鑾峰彇鐢ㄦ埛涓汉淇℃伅 + * + * @param accessToken + * @return + * @throws Exception + */ + public GetUserResponseBody getUserinfoNoId(String accessToken) throws Exception { + com.aliyun.dingtalkcontact_1_0.Client client = contactClient(); + GetUserHeaders getUserHeaders = new GetUserHeaders(); + getUserHeaders.xAcsDingtalkAccessToken = accessToken; + //鑾峰彇鐢ㄦ埛涓汉淇℃伅锛屽闇�鑾峰彇褰撳墠鎺堟潈浜虹殑淇℃伅锛寀nionId鍙傛暟蹇呴』浼爉e + GetUserResponseBody userResponse = client.getUserWithOptions("me", getUserHeaders, new RuntimeOptions()).getBody(); + + return userResponse; + } + + public String getUserId(String accessToken, String unionId) throws ApiException { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid"); + OapiUserGetbyunionidRequest req = new OapiUserGetbyunionidRequest(); + req.setUnionid(unionId); + OapiUserGetbyunionidResponse rsp = client.execute(req, accessToken); + if (rsp.isSuccess()) { + return rsp.getResult().getUserid(); + } + return null; + } + + public OapiV2UserGetResponse.UserGetResponse getUserinfo(String accessToken, String userId) throws Exception { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get"); + OapiV2UserGetRequest req = new OapiV2UserGetRequest(); + req.setUserid(userId); + req.setLanguage("en_US"); + OapiV2UserGetResponse rsp = client.execute(req, accessToken); + return rsp.getResult(); + } + + + final String redisKey = "RZT_TOKEN"; + + @RequestMapping(value = "/rztLogin", method = RequestMethod.GET) + public String getRztAccessToken(@RequestParam(value = "code") String code) { + try { + + log.info("code鍊硷細{}", code); + String accessToken = getRztToken(); + log.info("accessToken鍊硷細{}", accessToken); + if (StrUtil.isNotBlank(accessToken)) { + Map<String, String> hashMap = new HashMap<>(); + hashMap.put("code", code); + hashMap.put("token", accessToken); + String userJson = rzthttpUtils.sendGetRequest("/login/info", hashMap); + log.info("鐢ㄦ埛淇℃伅锛歿}", userJson); + JSONObject userObject = JSON.parseObject(userJson); + if (userObject.getIntValue("errcode") == 0) { + return userObject.getString("userid"); + } else { + log.error("鑾峰彇鐢ㄦ埛淇℃伅澶辫触锛歿}", userObject.getString("errmsg")); + } + } + + } catch (Exception e) { + log.error("鑾峰彇鐢ㄦ埛淇℃伅寮傚父锛歿}", e.getMessage()); + } + return ""; + } + + @Value("${rzt.corpId}") + private String corpId; + + @Value("${rzt.corpSecret}") + private String corpSecret; + + private String getRztToken() throws Exception { + String token = RedisUtils.getCacheObject(redisKey); + if (StrUtil.isEmpty(token)) { + + String accessJson = rzthttpUtils.sendGetRequest("/gettoken?corpId=" + corpId + "&corpsecret=" + corpSecret, null); + log.info("getRztToken accessJson鍊硷細{}", accessJson); + JSONObject jsonObject = JSON.parseObject(accessJson); + if (jsonObject.getIntValue("errcode") == 0) { + String accessToken = jsonObject.getString("access_token"); + Integer expiresIn = jsonObject.getIntValue("expires_in"); + RedisUtils.setCacheObject(redisKey, accessToken, Duration.ofSeconds(expiresIn)); + return accessToken; + } else { + log.error("鑾峰彇token澶辫触锛歿},{}", jsonObject.getIntValue("errcode"), jsonObject.getString("errmsg")); + return null; + } + + } else { + return token; + } + } } -- Gitblit v1.8.0