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+"×tamp="+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);

}

}

}

你可能感兴趣的:(java实现钉钉扫码登录)