对接钉钉登陆步骤

背景

复制代码
之前事情较少的时候,帮公司写过一个系统,
这个系统的话主管有要求要对接钉钉登陆。
话不多说我们直接开干。

流程

先进入开发者平台点击开发者后台

没有组织的 我们先在手机上先创建一个组织

创建完成后,就可以看到这个组织了

创建应用

这里我们按照步骤创建一个应用

我们选择钉钉应用。 其他的大家可以自己摸索

创建好之后,点击蓝色的应用的地方

我们需要修改二个地方

分享设置

这里设置我们的回调地址,获取钉钉回调回来的code

安全设置

AppKey 和AppSecret

凭证和基础信息

这个需要看一下的,到时候后端需要使用的。

部分代码实现

java 复制代码
 /**
     * 跳转到登录页面 默认使用钉钉的
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(@RequestParam(value = "authCode",required = false)String authCode) {
        log.info("/login===>authCode==>>{}:",authCode );
        com.aliyun.dingtalkoauth2_1_0.Client client = null;
        try {
            if(!StringUtils.isEmpty(authCode)) {
                client = authClient();
                GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()

                        //应用基础信息-应用信息的AppKey,请务必替换为开发的应用AppKey
                        .setClientId(dingDingProperties.getAppKey())

                        //应用基础信息-应用信息的AppSecret,,请务必替换为开发的应用AppSecret
                        .setClientSecret(dingDingProperties.getAppSecret())
                        .setCode(authCode)
                        .setGrantType("authorization_code");
                GetUserTokenResponse getUserTokenResponse = client.getUserToken(getUserTokenRequest);
                //获取用户个人token
                String accessToken = getUserTokenResponse.getBody().getAccessToken();
                GetUserResponseBody userInfo = getUserInfo(accessToken);
                Subject currentUser = ShiroKit.getSubject();
                String username = PinYinUtils.getPingYin(userInfo.getNick());
                EasyUsernameToken token = new EasyUsernameToken(username);

                currentUser.login(token);

                ShiroUser shiroUser = ShiroKit.getUser();
                if (shiroUser != null) {
                    super.getSession().setAttribute("shiroUser", shiroUser);
                    super.getSession().setAttribute("username", shiroUser.getAccount());
                    userService.setAvatar(shiroUser.getId(),userInfo.getAvatarUrl());
                    LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), HttpKit.getIp()));
                    ShiroKit.getSession().setAttribute("sessionFlag", true);
                    return REDIRECT + "/";
                }
            }
        } catch (Exception e) {
            log.error("error -->>msg==" +e.getMessage()+"==>>>"+e.getLocalizedMessage());
        }
 
        return "/login.html";
    }
  /**
     * 获取用户个人信息
     * @param accessToken
     * @return
     * @throws Exception
     */
    public GetUserResponseBody getUserInfo(String accessToken) throws Exception {
        com.aliyun.dingtalkcontact_1_0.Client client = contactClient();
        GetUserHeaders getUserHeaders = new GetUserHeaders();
        getUserHeaders.xAcsDingtalkAccessToken = accessToken;
        //获取用户个人信息
        return client.getUserWithOptions("me", getUserHeaders, new RuntimeOptions()).getBody();
    }


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

这里的代码的话,大家可以稍微看一下就可以了。 具体的情况的话 大家可以看钉钉的对接文档。

因为我使用的是旧版,目前已经出来了新版,但我还没有弄,为了不误导大家,大家看官方文档就可以了。

说明

钉钉对接的文档

如果又不懂的地方的话,可以留言。看到了我会回复的。

效果截图

相关推荐
gugucoding16 小时前
55. 【Java】Maven:项目构建的“管家”
java·maven
郑州光合科技余经理17 小时前
海外版多语言团购系统架构:主数据互通与核销边界
java·开发语言·前端·后端·系统架构·php·ai编程
Jesse_EC18 小时前
为什么我设置的 contentLength 到了消费端变成了 0?
java
vipxieliang18 小时前
ValidX 的 Date 和 DateTime vs JPA 的 Temporal 对比
java·后端
杜touch18 小时前
Spring框架的AnnotationConfigApplicationContext类启动的流程
java
XUEYUAN521218 小时前
Cloudflare 防护机制深度剖析与跨境数据采集工程化实践
服务器·网络·网络协议·tcp/ip·http
青瓦梦滋18 小时前
IP/MAC帧/ARP协议
运维·服务器·网络·网络协议·tcp/ip
AI人工智能+电脑小能手19 小时前
大白话说Java设计模式-26-策略模式(业务实战篇)
java·spring·设计模式·策略模式·支付系统·算法切换
不是光头 强19 小时前
Java 后端 AI 技术选型与学习路线
java·人工智能·学习