使用钉钉扫码完成第三方登录

单点登录

1.钉钉扫码登录第三方网站文档:

https://developers.dingtalk.com/document/app/scan-qr-code-to-log-on-to-third-party-websites

2.钉钉开放平台创建登录应用

https://open-dev.dingtalk.com/#/loginMan

3.通过扫码登录获取用户信息

1. 获取appId及appSecret
2. 构造扫码登录页面
    使用钉钉提供的扫码登录页面
    https://oapi.dingtalk.com/connect/qrconnect?appid=appid&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=uri
3. 服务端通过临时授权码获取授权用户的个人信息
4. 根据unionid获取userid
5. 根据userid 获取用户详情(包括权限数据)
import com.alibaba.fastjson.JSONObject;
import com.chs.mydingtalk.model.auto.Access;
import com.chs.mydingtalk.service.IUserService;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest;
import com.dingtalk.api.request.OapiUserGetbyunionidRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse;
import com.dingtalk.api.response.OapiUserGetbyunionidResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: hjt
 * @date: 2021/2/24 9:13 下午
 * @description:
 */
@RestController
@RequestMapping("/")
public class LoginController {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private IUserService userService;

    /**
     * https://oapi.dingtalk.com/connect/qrconnect?appid=appid&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=uri
     */

    /**
     * 扫码登录获取授权用户的个人信息
     * openapi@dingtalk
     * @return
     * @throws Exception
     * ServiceResult<Map<String,Object>>
     * 2020-11-4
     */
    @RequestMapping(value = "getUserInfo",method = RequestMethod.GET)
    public Map<String, Object> getUserInfo(@RequestParam("code")String code) throws ApiException {
        String appId = ;
        String appSecret = ;
        String accessToken= getToken();

        // 通过临时授权码获取授权用户的个人信息
        DefaultDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
        OapiSnsGetuserinfoBycodeRequest reqBycodeRequest = new OapiSnsGetuserinfoBycodeRequest();
        // 通过扫描二维码,跳转指定的redirect_uri后,向url中追加的code临时授权码
        reqBycodeRequest.setTmpAuthCode(code);
        OapiSnsGetuserinfoBycodeResponse bycodeResponse = client2.execute(reqBycodeRequest, appId,appSecret);

        // 根据unionid获取userid
        String unionid = bycodeResponse.getUserInfo().getUnionid();
        DingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
        OapiUserGetbyunionidRequest reqGetbyunionidRequest = new OapiUserGetbyunionidRequest();
        reqGetbyunionidRequest.setUnionid(unionid);
        log.info("accessToken:{}", accessToken);
        OapiUserGetbyunionidResponse oapiUserGetbyunionidResponse = clientDingTalkClient.execute(reqGetbyunionidRequest,accessToken);
        log.info("oapiUserGetbyunionidResponse:{}", oapiUserGetbyunionidResponse.toString());
        String userid = oapiUserGetbyunionidResponse.getResult().getUserid();

        Map<String, Object> map = new HashMap<String,Object>();
        //获取权限
        List<Access> accessList = userService.getAccessByUserId(userid);
        log.info("accessList:{}", accessList);
        for (Access access : accessList) {
            if("管理权限".equals(access.getName())){
                map.put("accessList", accessList);
                return map;
            }
        }

        // 根据userId获取用户信息
        /*String userid = oapiUserGetbyunionidResponse.getResult().getUserid();
        DingTalkClient clientDingTalkClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
        OapiV2UserGetRequest reqGetRequest = new OapiV2UserGetRequest();
        reqGetRequest.setUserid(userid);
        reqGetRequest.setLanguage("zh_CN");
        OapiV2UserGetResponse rspGetResponse = clientDingTalkClient2.execute(reqGetRequest, accessToken);
        System.out.println(rspGetResponse.getBody());*/

        map.put("error", "没有权限");
        return map;
    }

    public String getToken(){
        String accessToken = null;
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest req = new OapiGettokenRequest();
            req.setAppkey();
            req.setAppsecret();
            req.setHttpMethod("GET");
            OapiGettokenResponse rsp = client.execute(req);
            JSONObject object = JSONObject.parseObject(rsp.getBody());
            accessToken = object.getString("access_token");
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return accessToken;
    }
}
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值