海外手机号钉钉免登二开案例
一、场景
集成钉钉的轻应用,境内用户可以实现免登跳转登录,境外用户手机号一致无法钉钉免登苍穹。
如:钉钉将境外号码852-857xx311拆分为区号+手机号2个字段返回,需升级苍穹至v6.0.2版本,如不升级,需二开免登插件解决。
二、免登插件二开示例
package kd.bos.login.thirdauth.app.dingding;
import kd.bos.dc.api.model.Account;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.login.thirdauth.app.AppAuthResult;
import kd.bos.login.thirdauth.app.ThirdAppAuthtication;
import kd.bos.login.thirdauth.app.UserType;
import kd.bos.login.utils.StringUtils;
import kd.bos.util.RevProxyUtil;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
public class DDAuthticationExt extends ThirdAppAuthtication {
private static final String ERRMSG = "errmsg";
private static Log logger = LogFactory.getLog(DDAuthticationExt.class);
@Override
public boolean isNeedHandle(HttpServletRequest request, Account currentCenter) {
String app_type = request.getParameter("apptype");
String code = request.getParameter("code");
//是否需要钉钉认证
return !StringUtils.isEmpty(app_type) && app_type.equalsIgnoreCase("dd") && !StringUtils.isEmpty(code);
}
@Override
public AppAuthResult appAuthtication(HttpServletRequest request, Account currentCenter) {
AppAuthResult result = new AppAuthResult();
result.setSucceed(false);
//钉钉类型
String app_type = request.getParameter("apptype");
if (StringUtils.isEmpty(app_type)) {
return result;
}
//钉钉身份标识
String code = request.getParameter("code");
if (StringUtils.isEmpty(code)) {
return result;
}
String tenant = RevProxyUtil.getTenantCode(request);
String reurnType = DDUtils.getLoginType(currentCenter,tenant);
String appkey = request.getParameter("appkey");
if (StringUtils.isEmpty(appkey)) {
logger.error("dd login: appkey is blank !");
return result;
}
String appsecret = request.getParameter("appsecret");
if (StringUtils.isEmpty(appsecret)) {
appsecret = StringUtils.getTrimedBlank(DDUtils.getAppSecret(appkey, currentCenter));
if (StringUtils.isEmpty(appsecret)) {
logger.error("dd login: appsecret is blank !");
return result;
}
}
//获取钉钉accessToken
String access_token = DDUtils.getAccess_token(appkey, appsecret,currentCenter);
if (StringUtils.isEmpty(access_token)) {
logger.info("dd:access_token is null !");
result.setErrorMessage("dd:access_token is null !");
return result;
}
logger.info("dd: code is : " + code);
//获取钉钉用户id
String userId = DDUtils.getUserId(access_token, code,currentCenter);
if (StringUtils.isEmpty(userId)) {
logger.info("dd:userId is null !");
result.setErrorMessage("userId is null !");
return result;
}
//获取钉钉用户信息
HashMap<String, Object> userMap = DDUtils.getUserInfo(access_token, userId,currentCenter);
if (null == userMap) {
logger.info("dd:an error occurred while getting userInfo");
result.setErrorMessage("dd:an error occurred while getting userInfo");
return result;
}
if (!"0".equals(userMap.get("errcode").toString())) {
logger.info("dd:an error occurred while getting userInfo " + userMap.get(ERRMSG));
result.setErrorMessage("dd:an error occurred while getting userInfo, " + userMap.get(ERRMSG));
return result;
}
//获取钉钉认证结果
return getAppAuthResult(reurnType,userMap,app_type);
}
/**
* 获取钉钉认证结果
* @param reurnType
* @param userMap
* @return
*/
private AppAuthResult getAppAuthResult(String reurnType, HashMap<String, Object> userMap,String appType) {
AppAuthResult result = new AppAuthResult();
result.setSucceed(true);
result.setAppType(appType);
if("email".equalsIgnoreCase(reurnType)){
//优先获取企业邮箱
String email= StringUtils.getStringValue(userMap.get("orgEmail"));
if(StringUtils.isEmpty(email)){
//获取个人邮箱
email= StringUtils.getStringValue(userMap.get("email"));
}
if (StringUtils.isEmpty(email)) {
logger.info("dd:user email is null ! " + userMap.get(ERRMSG));
result.setSucceed(false);
return result;
}
result.setUserFlag(email);
result.setUserType(UserType.EMAIL);
return result;
}
//获取手机号
String mobile = StringUtils.getStringValue(userMap.get("mobile"));
if (StringUtils.isEmpty(mobile)) {
logger.info("dd:user mobile is null ! " + userMap.get(ERRMSG));
result.setSucceed(false);
return result;
}
if(mobile.length() < 11){
mobile = StringUtils.getStringValue(userMap.get("county")) + “-”+ mobile;
}
result.setUserFlag(mobile);
result.setUserType(UserType.MOBILE_PHONE);
return result;
}
}
海外手机号钉钉免登二开案例
本文2024-09-23 00:34:05发表“云苍穹知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-cangqiong-140293.html