对接钉钉登陆步骤

背景

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

流程

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

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

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

创建应用

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

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

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

我们需要修改二个地方

分享设置

这里设置我们的回调地址,获取钉钉回调回来的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);
    }
    

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

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

说明

钉钉对接的文档

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

效果截图

相关推荐
艾菜籽14 小时前
Spring Web MVC入门补充1
java·后端·spring·mvc
失散1314 小时前
分布式专题——44 ElasticSearch安装
java·分布式·elasticsearch·架构
MicrosoftReactor15 小时前
技术速递|使用 GitHub Copilot Agent 模式现代化 Java 项目的分步指南
java·github·copilot
似水এ᭄往昔15 小时前
【Linux】——基础指令(下)
linux·服务器
大锦终15 小时前
【Linux】udp网络程序
linux·运维·服务器·网络
ahauedu15 小时前
Spring Boot 2.7+ 中 RedisConnectionFactory Autowire 警告的深度解析
java·spring boot·后端
im_AMBER15 小时前
杂记 15
java·开发语言·算法
罗政15 小时前
【免费】轻量级服务器centos监控程序+内存+cpu+nginx+适合小型站长使用
服务器·nginx·centos
豆沙沙包?15 小时前
2025年--Lc182--sql(排序和分组)--Java版
java·数据库·sql