您好,欢迎光临本网站![请登录][注册会员]  
文件名称: package com.yn.mh.ding;
  所属分类: Java
  开发工具:
  文件大小: 6kb
  下载次数: 0
  上传时间: 2018-04-29
  提 供 者: sinat_3*******
 详细说明: package com.yn.mh.ding; import com.alibaba.fastjson.JSONObject; import com.dingtalk.oapi.lib.aes.DingTalkJsApiSingnature; import com.dingtalk.open.client.ServiceFactory; import com.dingtalk.open.client.api.model.corp.JsapiTicket; import com.dingtalk.open.client.api.service.corp.CorpConnectionService; import com.dingtalk.open.client.api.service.corp.JsapiService; import com.yn.core.util.PropertiesUtil; import javax.servlet.http.HttpServletRequest; import java.net.URLDecoder; /** * AccessToken和jsticket的获取封装 */ public class A uthHelper { // 调整到1小时50分钟 public static final long cacheTime = 1000 * 60 * 55 * 2; /* * 在此方法中,为了避免频繁获取access_token, * 在距离上一次获取access_token时间在两个小时之内的情况, * 将直接从持久化存储中读取access_token * * 因为access_token和jsapi_ticket的过期时间都是7200秒 * 所以在获取access_token的同时也去获取了jsapi_ticket * 注:jsapi_ticket是在前端页面JSAPI做权限验证配置的时候需要使用的 * 具体信息请查看开发者文档--权限验证配置 */ private static final String GET_ACCESSTOKEN_URL="https://oapi.dingtalk.com/gettoken?corpid="+PropertiesUtil.getProperty("CORP_ID")+"&corpsecret;="+PropertiesUtil.getProperty("CORP_SECRET"); public static String getAccessToken() throws OApiException { //获取access_token的接口地址,有效期为7200秒 //1.获取请求url //2.发起GET请求,获取返回结果 JSONObject jsonObject=HttpHelper.httpGet(GET_ACCESSTOKEN_URL); //3.解析结果,获取accessToken String accessToken=""; if (null != jsonObject) { accessToken=jsonObject.getString("access_token"); //4.错误消息处理 if (0 != jsonObject.getInteger("errcode")) { int errCode = jsonObject.getInteger("errcode"); String errMsg = jsonObject.getString("errmsg"); try { throw new Exception("error code:"+errCode+", error message:"+errMsg); } catch (Exception e) { e.printStackTrace(); } } } return accessToken; } /** * 获取JSTicket, 用于js的签名计算 * 正常的情况下,jsapi_ticket的有效期为7200秒,所以开发者需要在某个地方设计一个定时器,定期去更新jsapi_ticket */ public static String getJsapiTicket(String accessToken) throws OApiException { JSONObject jsTicketValue = (JSONObject) FileUtils.getValue("jsticket", Env.CORP_ID); long curTime = System.currentTimeMillis(); String jsTicket = ""; if (jsTicketValue == null || curTime - jsTicketValue.getLong("begin_time") >= cacheTime) { ServiceFactory serviceFactory; try { serviceFactory = ServiceFactory.getInstance(); JsapiService jsapiService = serviceFactory.getOpenService(JsapiService.class); JsapiTicket JsapiTicket = jsapiService.getJsapiTicket(accessToken, "jsapi"); jsTicket = JsapiTicket.getTicket(); JSONObject jsonTicket = new JSONObject(); JSONObject jsontemp = new JSONObject(); jsontemp.clear(); jsontemp.put("ticket", jsTicket); jsontemp.put("begin_time", curTime); jsonTicket.put(Env.CORP_ID, jsontemp); FileUtils.write2File(jsonTicket, "jsticket"); } catch (Exception e) { e.printStackTrace(); } return jsTicket; } else { return jsTicketValue.getString("ticket"); } } public static String sign(String ticket, String nonceStr, long timeStamp, String url) throws OApiException { try { return DingTalkJsApiSingnature.getJsApiSingnature(url, nonceStr, timeStamp, ticket); } catch (Exception ex) { throw new OApiException(0, ex.getMessage()); } } /** * 计算当前请求的jsapi的签名数据
*

* 如果签名数据是通过ajax异步请求的话,签名计算中的url必须是给用户展示页面的url * * @param request * @return */ public static String getConfig(HttpServletRequest request) { String urlString = request.getRequestURL().toString(); String queryString = request.getQueryString(); String queryStringEncode = null; String url; if (queryString != null) { queryStringEncode = URLDecoder.decode(queryString); url = urlString + "?" + queryStringEncode; } else { url = urlString; } String nonceStr = "abcdefg"; long timeStamp = System.currentTimeMillis() / 1000; String signedUrl = url; String accessToken = null; String ticket = null; String signature = null; String agentid = null; try { accessToken = AuthHelper.getAccessToken(); ticket = AuthHelper.getJsapiTicket(accessToken); signature = AuthHelper.sign(ticket, nonceStr, timeStamp, signedUrl); agentid = ""; } catch (OApiException e) { e.printStackTrace(); } String configValue = "{jsticket:'" + ticket + "',signature:'" + signature + "',nonceStr:'" + nonceStr + "',timeStamp:'" + timeStamp + "',corpId:'" + Env.CORP_ID + "',agentid:'" + agentid + "'}"; System.out.println(configValue); return configValue; } public static String getSsoToken() throws OApiException { String url = "https://oapi.dingtalk.com/sso/gettoken?corpid=" + Env.CORP_ID + "&corpsecret;=" + Env.SSO_Secret; JSONObject response = HttpHelper.httpGet(url); String ssoToken; if (response.containsKey("access_token")) { ssoToken = response.getString("access_token"); } else { throw new OApiException("Sso_token"); } return ssoToken; } } ...展开收缩

(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 钉钉
 输入关键字,在本站1000多万海量源码库中尽情搜索: