欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信开发之使用java获取签名signature

程序员文章站 2024-03-13 11:05:51
一、前言 微信接口调用验证最终需要用到的三个参数noncestr、timestamp、signature: 接下来将会给出获取这三个参数的详细代码 本文的环境e...

一、前言

微信接口调用验证最终需要用到的三个参数noncestr、timestamp、signature:

微信开发之使用java获取签名signature

接下来将会给出获取这三个参数的详细代码
本文的环境eclipse + maven
本文使用到的技术httpclient、json字符串转map、sha1加密

二、需要用到的jar包

maven依赖的包有:

1、httpclient包依赖

<dependency>
 <groupid>org.apache.httpcomponents</groupid>
 <artifactid>httpcore</artifactid>
 <version>4.4.3</version>
</dependency>
<dependency>
 <groupid>org.apache.httpcomponents</groupid>
 <artifactid>httpclient</artifactid>
 <version>4.5.1</version>
</dependency>

2、json转map相关包依赖

<dependency> 
 <groupid>net.sf.json-lib</groupid> 
 <artifactid>json-lib</artifactid> 
 <version>2.4</version> 
 <classifier>jdk15</classifier> 
</dependency>
<dependency>
 <groupid>xom</groupid>
 <artifactid>xom</artifactid>
 <version>1.2.5</version>
</dependency>

三、运行结果

微信开发之使用java获取签名signature

四、详细代码

package com.luo.util;

import java.io.ioexception;
import java.io.unsupportedencodingexception;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
import java.util.arraylist;
import java.util.hashmap;
import java.util.iterator;
import java.util.list;
import java.util.map;
import java.util.set;
import java.util.uuid;
import net.sf.json.jsonobject;
import org.apache.http.httpentity;
import org.apache.http.httpresponse;
import org.apache.http.namevaluepair;
import org.apache.http.parseexception;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.client.entity.urlencodedformentity;
import org.apache.http.client.methods.httpget;
import org.apache.http.client.methods.httppost;
import org.apache.http.client.methods.httpurirequest;
import org.apache.http.impl.client.defaulthttpclient;
import org.apache.http.message.basicnamevaluepair;
import org.apache.http.protocol.http;
import org.apache.http.util.entityutils;

public class httpxmlclient {

 public static string post(string url, map<string, string> params) {
 defaulthttpclient httpclient = new defaulthttpclient();
 string body = null;
 httppost post = postform(url, params);
 body = invoke(httpclient, post);
 httpclient.getconnectionmanager().shutdown();
 return body;
 }

 public static string get(string url) {
 defaulthttpclient httpclient = new defaulthttpclient();
 string body = null;
 httpget get = new httpget(url);
 body = invoke(httpclient, get);
 httpclient.getconnectionmanager().shutdown();
 return body;
 }

 private static string invoke(defaulthttpclient httpclient,
  httpurirequest httpost) {
 httpresponse response = sendrequest(httpclient, httpost);
 string body = paseresponse(response);
 return body;
 }

 private static string paseresponse(httpresponse response) {
 httpentity entity = response.getentity();
 string charset = entityutils.getcontentcharset(entity);
 string body = null;
 try {
  body = entityutils.tostring(entity);
 } catch (parseexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 }
 return body;
 }

 private static httpresponse sendrequest(defaulthttpclient httpclient,
  httpurirequest httpost) {
 httpresponse response = null;
 try {
  response = httpclient.execute(httpost);
 } catch (clientprotocolexception e) {
  e.printstacktrace();
 } catch (ioexception e) {
  e.printstacktrace();
 }
 return response;
 }

 private static httppost postform(string url, map<string, string> params) {

 httppost httpost = new httppost(url);
 list<namevaluepair> nvps = new arraylist<namevaluepair>();

 set<string> keyset = params.keyset();
 for (string key : keyset) {
  nvps.add(new basicnamevaluepair(key, params.get(key)));
 }

 try {
  httpost.setentity(new urlencodedformentity(nvps, http.utf_8));
 } catch (unsupportedencodingexception e) {
  e.printstacktrace();
 }

 return httpost;
 }

 public static void main(string[] args) {
 //获取access_token
 map<string, string> params = new hashmap<string, string>();
 params.put("corpid","wx5f24fa0db1819ea2");
 params.put("corpsecret","uqtwzf0bqtl2krhx0amekjpq8l0ao96lspsnfctoblrbuypo4dubhmn0_v2jhs-9");
 string xml = httpxmlclient.post("https://qyapi.weixin.qq.com/cgi-bin/gettoken",params);
 jsonobject jsonmap = jsonobject.fromobject(xml);
 map<string, string> map = new hashmap<string, string>();
 iterator<string> it = jsonmap.keys(); 
 while(it.hasnext()) { 
  string key = (string) it.next(); 
  string u = jsonmap.get(key).tostring();
  map.put(key, u); 
 }
 string access_token = map.get("access_token");
 system.out.println("access_token=" + access_token);

 //获取ticket
 params.put("access_token",access_token);
 xml = httpxmlclient.post("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket",params); 
 jsonmap = jsonobject.fromobject(xml);
 map = new hashmap<string, string>();
 it = jsonmap.keys(); 
 while(it.hasnext()) { 
  string key = (string) it.next(); 
  string u = jsonmap.get(key).tostring();
  map.put(key, u); 
 }
 string jsapi_ticket = map.get("ticket");
 system.out.println("jsapi_ticket=" + jsapi_ticket);

 //获取签名signature
 string noncestr = uuid.randomuuid().tostring();
 string timestamp = long.tostring(system.currenttimemillis() / 1000);
 string url="http://mp.weixin.qq.com";
 string str = "jsapi_ticket=" + jsapi_ticket +
  "&noncestr=" + noncestr +
  "&timestamp=" + timestamp +
  "&url=" + url;
 //sha1加密
 string signature = sha1(str);
 system.out.println("noncestr=" + noncestr);
 system.out.println("timestamp=" + timestamp);
 system.out.println("signature=" + signature);
 //最终获得调用微信js接口验证需要的三个参数noncestr、timestamp、signature
 }

 /** 
 * @author:罗国辉 
 * @date: 2015年12月17日 上午9:24:43 
 * @description: sha、sha1加密
 * @parameter: str:待加密字符串
 * @return: 加密串
 **/
 public static string sha1(string str) {
 try {
  messagedigest digest = java.security.messagedigest
   .getinstance("sha-1"); //如果是sha加密只需要将"sha-1"改成"sha"即可
  digest.update(str.getbytes());
  byte messagedigest[] = digest.digest();
  // create hex string
  stringbuffer hexstr = new stringbuffer();
  // 字节数组转换为 十六进制 数
  for (int i = 0; i < messagedigest.length; i++) {
  string shahex = integer.tohexstring(messagedigest[i] & 0xff);
  if (shahex.length() < 2) {
   hexstr.append(0);
  }
  hexstr.append(shahex);
  }
  return hexstr.tostring();

 } catch (nosuchalgorithmexception e) {
  e.printstacktrace();
 }
 return null;
 }
}

五、工程下载

更多精彩内容请点击《android微信开发教程汇总》,《java微信开发教程汇总》欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。