UDN-企业互联网技术人气社区

板块导航

浏览  : 1672
回复  : 3

[原生js] 微信开发之使用java获取签名signature

[复制链接]
西北的风的头像 楼主
发表于 2017-1-17 17:03:15 | 显示全部楼层 |阅读模式
  这篇文章主要为大家详细介绍了微信开发之使用java获取签名signature,感兴趣的小伙伴们可以参考一下

  一、前言
  
  微信接口调用验证最终需要用到的三个参数noncestr、timestamp、signature:
 
2016830135245854.jpg
 
  接下来将会给出获取这三个参数的详细代码
  
  本文的环境eclipse + maven
  
  本文使用到的技术HttpClient、Json字符串转map、sha1加密
  
  二、需要用到的jar包
  
  maven依赖的包有:
  
  1、HttpClient包依赖
  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpcore</artifactId>
  4. <version>4.4.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.httpcomponents</groupId>
  8. <artifactId>httpclient</artifactId>
  9. <version>4.5.1</version>
  10. </dependency>
复制代码

  2、json转map相关包依赖
  1. <dependency>  
  2.   <groupId>net.sf.json-lib</groupId>  
  3.   <artifactId>json-lib</artifactId>  
  4.   <version>2.4</version>
  5.   <classifier>jdk15</classifier>
  6. </dependency>
  7. <dependency>
  8.   <groupId>xom</groupId>
  9.   <artifactId>xom</artifactId>
  10.   <version>1.2.5</version>
  11. </dependency>
复制代码

  三、运行结果
2016830135303757.jpg
  
  四、详细代码
  1. package com.luo.util;

  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.UUID;
  13. import net.sf.json.JSONObject;
  14. import org.apache.http.HttpEntity;
  15. import org.apache.http.HttpResponse;
  16. import org.apache.http.NameValuePair;
  17. import org.apache.http.ParseException;
  18. import org.apache.http.client.ClientProtocolException;
  19. import org.apache.http.client.entity.UrlEncodedFormEntity;
  20. import org.apache.http.client.methods.HttpGet;
  21. import org.apache.http.client.methods.HttpPost;
  22. import org.apache.http.client.methods.HttpUriRequest;
  23. import org.apache.http.impl.client.DefaultHttpClient;
  24. import org.apache.http.message.BasicNameValuePair;
  25. import org.apache.http.protocol.HTTP;
  26. import org.apache.http.util.EntityUtils;

  27. public class HttpXmlClient {

  28.   public static String post(String url, Map<String, String> params) {
  29.     DefaultHttpClient httpclient = new DefaultHttpClient();
  30.     String body = null;
  31.     HttpPost post = postForm(url, params);
  32.     body = invoke(httpclient, post);
  33.     httpclient.getConnectionManager().shutdown();
  34.     return body;
  35.   }

  36.   public static String get(String url) {
  37.     DefaultHttpClient httpclient = new DefaultHttpClient();
  38.     String body = null;
  39.     HttpGet get = new HttpGet(url);
  40.     body = invoke(httpclient, get);
  41.     httpclient.getConnectionManager().shutdown();
  42.     return body;
  43.   }

  44.   private static String invoke(DefaultHttpClient httpclient,
  45.       HttpUriRequest httpost) {
  46.     HttpResponse response = sendRequest(httpclient, httpost);
  47.     String body = paseResponse(response);
  48.     return body;
  49.   }

  50.   private static String paseResponse(HttpResponse response) {
  51.     HttpEntity entity = response.getEntity();
  52.     String charset = EntityUtils.getContentCharSet(entity);
  53.     String body = null;
  54.     try {
  55.       body = EntityUtils.toString(entity);
  56.     } catch (ParseException e) {
  57.       e.printStackTrace();
  58.     } catch (IOException e) {
  59.       e.printStackTrace();
  60.     }
  61.     return body;
  62.   }

  63.   private static HttpResponse sendRequest(DefaultHttpClient httpclient,
  64.       HttpUriRequest httpost) {
  65.     HttpResponse response = null;
  66.     try {
  67.       response = httpclient.execute(httpost);
  68.     } catch (ClientProtocolException e) {
  69.       e.printStackTrace();
  70.     } catch (IOException e) {
  71.       e.printStackTrace();
  72.     }
  73.     return response;
  74.   }

  75.   private static HttpPost postForm(String url, Map<String, String> params) {

  76.     HttpPost httpost = new HttpPost(url);
  77.     List<NameValuePair> nvps = new ArrayList<NameValuePair>();

  78.     Set<String> keySet = params.keySet();
  79.     for (String key : keySet) {
  80.       nvps.add(new BasicNameValuePair(key, params.get(key)));
  81.     }

  82.     try {
  83.       httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  84.     } catch (UnsupportedEncodingException e) {
  85.       e.printStackTrace();
  86.     }

  87.     return httpost;
  88.   }

  89.   public static void main(String[] args) {
  90.     //获取access_token
  91.     Map<String, String> params = new HashMap<String, String>();
  92.     params.put("corpid","wx5f24fa0db1819ea2");
  93.     params.put("corpsecret","uQtWzF0bQtl2KRHX0amekjpq8L0aO96LSpSNfctOBLRbuYPO4DUBhMn0_v2jHS-9");
  94.     String xml = HttpXmlClient.post("https://qyAPI.weixin.qq.com/cgi-bin/gettoken",params);
  95.     JSONObject jsonMap = JSONObject.fromObject(xml);
  96.     Map<String, String> map = new HashMap<String, String>();
  97.     Iterator<String> it = jsonMap.keys();
  98.     while(it.hasNext()) {
  99.       String key = (String) it.next();
  100.       String u = jsonMap.get(key).toString();
  101.       map.put(key, u);
  102.     }
  103.     String access_token = map.get("access_token");
  104.     System.out.println("access_token=" + access_token);

  105.     //获取ticket
  106.     params.put("access_token",access_token);
  107.     xml = HttpXmlClient.post("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket",params);
  108.     jsonMap = JSONObject.fromObject(xml);
  109.     map = new HashMap<String, String>();
  110.     it = jsonMap.keys();
  111.     while(it.hasNext()) {
  112.       String key = (String) it.next();
  113.       String u = jsonMap.get(key).toString();
  114.       map.put(key, u);
  115.     }
  116.     String jsapi_ticket = map.get("ticket");
  117.     System.out.println("jsapi_ticket=" + jsapi_ticket);

  118.     //获取签名signature
  119.     String noncestr = UUID.randomUUID().toString();
  120.     String timestamp = Long.toString(System.currentTimeMillis() / 1000);
  121.     String url="http://mp.weixin.qq.com";
  122.     String str = "jsapi_ticket=" + jsapi_ticket +
  123.         "&noncestr=" + noncestr +
  124.         "×tamp=" + timestamp +
  125.         "&url=" + url;
  126.     //sha1加密
  127.     String signature = SHA1(str);
  128.     System.out.println("noncestr=" + noncestr);
  129.     System.out.println("timestamp=" + timestamp);
  130.     System.out.println("signature=" + signature);
  131.     //最终获得调用微信js接口验证需要的三个参数noncestr、timestamp、signature
  132.   }

  133.     /**
  134.    * @author:罗国辉
  135.    * @date: 2015年12月17日 上午9:24:43
  136.    * @description: SHA、SHA1加密
  137.    * @parameter:  str:待加密字符串
  138.    * @return: 加密串
  139.   **/
  140.   public static String SHA1(String str) {
  141.     try {
  142.       MessageDigest digest = java.security.MessageDigest
  143.           .getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可
  144.       digest.update(str.getBytes());
  145.       byte messageDigest[] = digest.digest();
  146.       // Create Hex String
  147.       StringBuffer hexStr = new StringBuffer();
  148.       // 字节数组转换为 十六进制 数
  149.       for (int i = 0; i < messageDigest.length; i++) {
  150.         String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
  151.         if (shaHex.length() < 2) {
  152.           hexStr.append(0);
  153.         }
  154.         hexStr.append(shaHex);
  155.       }
  156.       return hexStr.toString();

  157.     } catch (NoSuchAlgorithmException e) {
  158.       e.printStackTrace();
  159.     }
  160.     return null;
  161.   }
  162. }
复制代码

  五、工程下载
  
  微信获取签名工程: http://xiazai.jb51.net/201608/yuanma/javaproject(jb51.net).rar
  
  以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关帖子

发表于 2017-1-17 17:03:45 | 显示全部楼层
我是被标题吸引进来的
使用道具 举报

回复

发表于 2017-1-21 04:42:49 | 显示全部楼层
LZ是闲人,天天发帖,坚定完毕
使用道具 举报

回复

发表于 2017-8-24 11:25:40 | 显示全部楼层
我们也是做微信开发的,深有同感
使用道具 举报

回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们
联系我们
  • 电话:010-86393388
  • 邮件:udn@yonyou.com
  • 地址:北京市海淀区北清路68号
移动客户端下载
关注我们
  • 微信公众号:yonyouudn
  • 扫描右侧二维码关注我们
  • 专注企业互联网的技术社区
版权所有:用友网络科技股份有限公司82041 京ICP备05007539号-11 京公网网备安1101080209224 Powered by Discuz!
快速回复 返回列表 返回顶部