zhanghua
2024-11-04 085f7500992d6f6c83b60d1ce0bd8e87b12e8328
钉钉登录
4个文件已修改
120 ■■■■■ 已修改文件
ruoyi-admin/pom.xml 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java 98 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/src/main/resources/application-dev.yml 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-extend/ruoyi-powerjob-server/src/main/resources/application-dev.properties 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/pom.xml
@@ -87,6 +87,16 @@
            <artifactId>JustAuth</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dingtalk</artifactId>
            <version>2.1.61</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- skywalking 整合 logback -->
<!--        <dependency>-->
<!--            <groupId>org.apache.skywalking</groupId>-->
ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java
@@ -3,7 +3,21 @@
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
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.OapiUserGetuserinfoResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
import com.taobao.api.ApiException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -26,7 +40,6 @@
import org.dromara.system.domain.SysClient;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.bo.SysTenantBo;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.system.service.*;
@@ -39,6 +52,7 @@
import org.dromara.web.service.SysRegisterService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.aliyun.teaopenapi.models.Config;
import java.net.URL;
import java.util.List;
@@ -205,13 +219,91 @@
            .eq(SysUser::getUserName, user.getUsername())
            .eq(SysUser::getStatus, "0")
            .eq(SysUser::getDelFlag, "0"));
        if(!exists){
        if (!exists) {
            registerService.register(user);
        }
        LoginBody loginBody = AutoLoginUtil.create(user,clientId);
        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()
            //应用基础信息-应用信息的AppKey,请务必替换为开发的应用AppKey
            .setClientId("dingl5dxahaj3uzfug66")
            //应用基础信息-应用信息的AppSecret,,请务必替换为开发的应用AppSecret
            .setClientSecret("AGDu7NfzkverlMXq8CUDiy6EXx5jSL4v2p-Odz1mpXV5e4_K3kB1Acat0RftzBXC")
            .setCode(authCode)
            .setGrantType("authorization_code");
        GetUserTokenResponse getUserTokenResponse = client.getUserToken(getUserTokenRequest);
        //获取用户个人token
        String accessToken = getUserTokenResponse.getBody().getAccessToken();
        GetUserResponseBody userResponseBody = getUserinfoNoId(accessToken);
        String userId = getUserId(accessToken, userResponseBody.getUnionId());
        OapiV2UserGetResponse.UserGetResponse userGetResponse = getUserinfo(accessToken, userId);
        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;
        //获取用户个人信息,如需获取当前授权人的信息,unionId参数必须传me
        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);
        return rsp.getResult().getUserid();
    }
    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();
    }
}
ruoyi-admin/src/main/resources/application-dev.yml
@@ -43,9 +43,9 @@
          driverClassName: com.mysql.cj.jdbc.Driver
          # jdbc 所有参数配置参考 https://lionli.blog.csdn.net/article/details/122018562
          # rewriteBatchedStatements=true 批处理优化 大幅提升批量插入更新删除性能(对数据库有性能损耗 使用批量操作应考虑性能问题)
          url: jdbc:mysql://123.207.71.245:13306/qysp?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
          url: jdbc:mysql://101.35.247.188:3306/qysp?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
          username: root
          password: 234#Wersdf!
          password: ycl@202466
        # 从库数据源
        slave:
          lazy: true
@@ -96,9 +96,9 @@
spring.data:
  redis:
    # 地址
    host: 162.14.79.111
    host: 127.0.0.1
    # 端口,默认为6379
    port: 16379
    port: 6379
    password: 234#Wersdf!
    # 数据库索引
    database: 0
ruoyi-extend/ruoyi-powerjob-server/src/main/resources/application-dev.properties
@@ -2,9 +2,9 @@
####### Database properties(Configure according to the the environment) #######
spring.datasource.core.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.core.jdbc-url=jdbc:mysql://123.207.71.245:13306/qysp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.core.jdbc-url=jdbc:mysql://101.35.247.188:3306/qysp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.core.username=root
spring.datasource.core.password=234#Wersdf!
spring.datasource.core.password=ycl@202466
spring.datasource.core.maximum-pool-size=20
spring.datasource.core.minimum-idle=5