java实现钉钉扫码登录_java实现钉钉扫码登录

@Service

public class DingDingService {

/**

* 根据 授权码 获取用户信息

* @param code

* @return

* @throws ApiException

*/

public ControlClerk getLoginUser(String code) throws ApiException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {

String accessToken = this.getAccessToken();

String unionId = this.getUnionId(code);

String userIdByUnionId = this.getUserIdByUnionId(unionId, accessToken);

ControlClerk user = this.getUserByUserId(userIdByUnionId, accessToken);

return user;

}

/**

* 根据 userId 获取用户信息

* @param userId

* @param accessToken

* @return

* @throws ApiException

*/

public ControlClerk getUserByUserId(String userId,String accessToken) throws ApiException {

DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");

OapiUserGetRequest request = new OapiUserGetRequest();

request.setUserid(userId);

request.setHttpMethod("GET");

OapiUserGetResponse response = client.execute(request, accessToken);

if (response.getErrcode() !=0 ){

throw new RuntimeException(response.getErrmsg());

}

ControlClerk clerk = new ControlClerk();

clerk.setUserId(response.getUserid());

clerk.setUserName(response.getName());

return clerk;

}

/**

根据授权码获取 unionId

* @param tmpAuthCode

* @return

* @throws ApiException

*/

public String getUnionId(String tmpAuthCode) throws ApiException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {

String timestamp = String.valueOf(System.currentTimeMillis());

String signature = this.getSignature(timestamp);

DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature="+signature+"&timestamp="+timestamp+"&accessKey="+SysConfigCache.getString("DING_APP_ID"));

OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();

req.setTmpAuthCode(tmpAuthCode);

OapiSnsGetuserinfoBycodeResponse response = client.execute(req, SysConfigCache.getString("DING_APP_ID"), SysConfigCache.getString("DING_APP_SECRET2"));

if (response.getErrcode() !=0 ){

throw new RuntimeException(response.getErrmsg());

}

return response.getUserInfo().getUnionid();

}

/**

* 根据 unionId 获取userId

* @return

* @throws ApiException

*/

public String getUserIdByUnionId(String unionId,String accessToken) throws ApiException {

DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getUseridByUnionid");

OapiUserGetUseridByUnionidRequest request = new OapiUserGetUseridByUnionidRequest();

request.setUnionid(unionId);

request.setHttpMethod("GET");

OapiUserGetUseridByUnionidResponse response = client.execute(request, accessToken);

if (response.getErrcode() !=0 ){

throw new RuntimeException(response.getErrmsg());

}

return response.getUserid();

}

/**

* 获取 accessToken

* @return

* @throws ApiException

* @throws NoSuchAlgorithmException

* @throws InvalidKeyException

* @throws UnsupportedEncodingException

*/

public String getAccessToken() throws ApiException {

DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");

OapiGettokenRequest request = new OapiGettokenRequest();

request.setAppkey(SysConfigCache.getString("DING_APP_ID2"));

request.setAppsecret(SysConfigCache.getString("DING_APP_SECRET2"));

request.setHttpMethod("GET");

OapiGettokenResponse response = client.execute(request);

if (response.getErrcode() !=0 ){

throw new RuntimeException(response.getErrmsg());

}

return response.getAccessToken();

}

/**

* 签名算法

* @param timestamp

* @return

* @throws NoSuchAlgorithmException

* @throws UnsupportedEncodingException

* @throws InvalidKeyException

*/

public String getSignature(String timestamp) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {

// 根据timestamp, appSecret计算签名值

Mac mac = Mac.getInstance("HmacSHA256");

mac.init(new SecretKeySpec(SysConfigCache.getString("DING_APP_SECRET").getBytes("UTF-8"), "HmacSHA256"));

byte[] signatureBytes = mac.doFinal(timestamp.getBytes("UTF-8"));

String signature = new String(Base64.encodeBase64(signatureBytes));

String urlEncodeSignature = urlEncode(signature);

return urlEncodeSignature;

}

// encoding参数使用utf-8

public String urlEncode(String value) {

if (value == null) {

return "";

}

try {

String encoded = URLEncoder.encode(value,"utf-8");

return encoded.replace("+", "%20").replace("*", "%2A")

.replace("~", "%7E").replace("/", "%2F");

} catch (UnsupportedEncodingException e) {

throw new IllegalArgumentException("FailedToEncodeUri", e);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,要实现码功能,通常会使用开放平台提供的API和SDK。提供了Java SDK供开发者集成,以便于处理用户描二维码登录、获取临时权限等功能。以下是一般的步骤: 1. 注册开发者账号并创建应用:首先,你需要在开放平台上注册(https://open.dingtalk.com/)并创建一个新的应用,获取App Key和App Secret。 2. 添加依赖:在你的项目中添加SDK的依赖,如果是Maven,可以在pom.xml中添加如下代码: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>dingtalk-sdk</artifactId> <version>版本号</version> </dependency> ``` 3. 初始化SDK:在应用启动时,用App Key和App Secret初始化SDK,如: ```java DingTalkClient.init("your_app_key", "your_app_secret"); ``` 4. 生成码二维码:调用SDK中的方法生成二维码,例如: ```java QRCode qrCode = new QRCode(); qrCode.setCodeType(QRCode.CODE_TYPE_TEMP); qrCode.setTitle("登录"); qrCode.setExpiration(60); // 二维码有效时间(秒) String ticket = qrCode.getQRCodeTicket(); ``` 这将返回一个包含临时权限的二维码图片链接或二维码码串。 5. 用户码后,服务器会发送事件到你的回调地址,你可以通过监听这些事件来处理用户的登录请求。 6. 处理回调:在你的服务端,设置一个回调URL,当用户码后,会发送登录事件到这个地址,通过验证事件参数,验证用户身份。 相关问题: 1. 码的具体API调用是什么样子的? 2. 如何在Java中处理推送的登录事件? 3. 如何验证服务器传递的码事件参数?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值