微信获取签名token

因为token是有时间限制的,所以要用调度 让调度框架每隔多长时间去访问一次,获得tokan


quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。本系统结合通过Spring来集成Quartz。
Quartz  下载地址 :

          http://grepcode.com/snapshot/repo1.maven.org/maven2/org.quartz-scheduler/quartz/1.7.3

首先下载包 :quartz-1.7.3.jar 

需要用到的jar包
json相关  spring相关  Struts相关  Log相关


把包放到 lib 里面。
当然 需要把applicationContext-quartz.xml 文件添加到 <import resource="classpath:applicationContext-quartz.xml" />

applicationContext-quartz.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:task="http://www.springframework.org/schema/task"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 定义jobDetail --> 
    <bean id="jobDetail" 
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
    <!--     目标类 --> 
        <property name="targetObject"> 
            <ref bean="getAccessToken" /> 
        </property> 
        <!-- 目标类中要执行的方法 ,这个参数是必须的--> 
        <property name="targetMethod"> 
              <value>execute</value> 
        </property> 
    </bean>
    <!--触发器是CornTrigger --> 
    <bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
       <property name="jobDetail" ref="jobDetail"/> 
       <!--每60分钟--> 
       <property name="cronExpression" value="0 0/59 * * * ?" />
    </bean>    
   
    <!-- 定义核心调度器 --> 
    <bean id="scheduler" lazy-init="false"   class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers"> 
        <ref bean="cornTrigger"/> 
      </property> 
    </bean>
   
    <bean id="getAccessToken" class="com.wms.core.utils.course.util.GetAccessToken" />  
</beans>



weixin.properties//配置文件

appid=***************//注意这里需要修改为自己测试号的
appsecret=******************//注意这里需要修改为自己测试号的







GetProperties .class //读取配置文件

package com.wms.core.utils.course.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
*
* @ClassName: GetProperties   
* @Description: TODO(这里用一句话描述这个类的作用)   
* @date 2015-9-10 下午2:46:01   
*
*/

public  class  GetProperties {
// 读取资源文件,并处理中文乱码
    public static Properties readPropertiesFileObj(String filename) {
        Properties properties = new OrderedProperties();
        try {
            InputStream in = GetProperties.class.getClassLoader()
                        .getResourceAsStream(filename);
            BufferedReader bf = new BufferedReader(new InputStreamReader(in, "utf-8"));
            properties.load(bf);
            in.close(); // 关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }
   
    public static Properties getKey() {
    String readfile = "weixin.properties";
    Properties pro = readPropertiesFileObj(readfile);
    return pro;
    }
}




//调度器执行的方法
package com.wms.core.utils.course.util;


import java.util.Properties;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;

import com.wms.core.utils.course.pojo.Token;
import com.wms.wechat.wx.accessToken.Ticket;
/**
*
* @ClassName: GetAccessToken   
* @Description: TODO(这里用一句话描述这个类的作用)  
* @date 2015-9-10 下午2:46:13   
*
*/
public class GetAccessToken implements InitializingBean, ServletContextAware{
private static Logger log = LoggerFactory.getLogger(GetAccessToken.class);
  // 第三方用户唯一凭证
    public static String appid = "";
    // 第三方用户唯一凭证密钥
    public static String appsecret = "";
    public static Token accessToken = null; 
    public static Ticket ticket = null; 
   //把要执行的操作,  重点都在这个方法中
    public void execute() { 
       try {
Properties  pro = GetProperties.getKey();
   accessToken = CommonUtil.getToken(pro.getProperty("appid"), pro.getProperty("appsecret"));
   ticket =CommonUtil.getticket(accessToken.getAccessToken());
} catch (Exception e) {
e.printStackTrace();
}
      
       System.out.println("當前accessToken為::"+accessToken.getAccessToken());
       System.out.println("當前ticket為::"+ticket.getTicket());
    }
@Override
public void setServletContext(ServletContext arg0) {
System.out.println("初始化時加載!");
execute();
}
@Override
public void afterPropertiesSet() throws Exception {
}

}

CommonUtil.class

package com.wms.core.utils.course.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.wms.core.utils.common.ObjectUtils;
import com.wms.core.utils.course.pojo.Token;
import com.wms.wechat.wx.accessToken.Ticket;

/**
* 通用工具类
*
* @author liu
*/
public class CommonUtil {
private static Logger log = LoggerFactory.getLogger(CommonUtil.class);

// 凭证获取(GET)
public final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
// 凭证获取(GET)
public final static String ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
/**
* 发送https请求
*
* @param requestUrl
*            请求地址
* @param requestMethod
*            请求方式(GET、POST)
* @param outputStr
*            提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static JSONObject httpsRequest(String requestUrl,
String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();

URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);

conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);

// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}

// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}

// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
log.error("连接超时:{}", ce);
} catch (Exception e) {
log.error("https请求异常:{}", e);
}
return jsonObject;
}

/**
* 获取接口访问凭证
*
* @param appid
*            凭证
* @param appsecret
*            密钥
* @return
*/
public static Token getToken(String appid, String appsecret) {
Token token = null;
String requestUrl = token_url.replace("APPID", appid).replace(
"APPSECRET", appsecret);
// 发起GET请求获取凭证
JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);

if (null != jsonObject) {
try {
token = new Token();
token.setAccessToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
token = null;
// 获取token失败
log.error("获取token失败 errcode:{} errmsg:{}",
jsonObject.getInt("errcode"),
jsonObject.getString("errmsg"));
}
}
return token;
}

/**
* URL编码(utf-8)
*
* @param source
* @return
*/
public static String urlEncodeUTF8(String source) {
String result = source;
try {
result = java.net.URLEncoder.encode(source, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}

/**
* 根据内容类型判断文件扩展名
*
* @param contentType
*            内容类型
* @return
*/
public static String getFileExt(String contentType) {
String fileExt = "";
if ("image/jpeg".equals(contentType))
fileExt = ".jpg";
else if ("image/png".equals(contentType))
fileExt = ".png";
else if ("audio/mpeg".equals(contentType))
fileExt = ".mp3";
else if ("audio/amr".equals(contentType))
fileExt = ".amr";
else if ("video/mp4".equals(contentType))
fileExt = ".mp4";
else if ("video/mpeg4".equals(contentType))
fileExt = ".mp4";
return fileExt;
}

/**
* 将xml字符转换成可以读取属性的map,仅限一级元素内容的读取,不考虑属性
*
* @param xml
* @return
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(String xml) {
Map<String, String> result = new HashMap<String, String>();
Document document = null;
try {
document = DocumentHelper.parseText(xml);
} catch (DocumentException e) {
e.printStackTrace();
}
if (!ObjectUtils.isEmpty(document)) {
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();

// 遍历所有子节点
for (Element e : elementList) {
result.put(e.getName(), StringUtils.isEmpty(e.getText()) ? ""
: e.getText());
}
}
return result;
}
/**
* 获取接口访问凭证
*
* @param appid
*            凭证
* @param appsecret
*            密钥
* @return
*/
public static Ticket getticket(String access_token ) {
Ticket ticket =new Ticket();
String requestUrl = ticket_url.replace("ACCESS_TOKEN", access_token);
// 发起GET请求获取凭证
JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);

if (null != jsonObject) {
try {
ticket.setTicket(jsonObject.getString("ticket"));
ticket.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
ticket = null;
// 获取token失败
log.error("获取token失败 errcode:{} errmsg:{}",
jsonObject.getInt("errcode"),
jsonObject.getString("errmsg"));
}
}
return ticket;
}
}

SHA1算法对获得的临时票据进行加密

Sign.class


package com.wms.mfz_wx;
import java.util.UUID;
import java.util.Map;
import java.util.HashMap;
import java.util.Formatter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException; 

public class Sign {
  /*  public static void main(String[] args) {
        String jsapi_ticket = "jsapi_ticket";

        // 注意 URL 一定要动态获取,不能 hardcode
        String url = "http://example.com";
        Map<String, String> ret = sign(jsapi_ticket, url);
        for (Map.Entry entry : ret.entrySet()) {
            System.out.println(entry.getKey() + ", " + entry.getValue());
        }
    };  */

    public static Map<String, String> sign(String jsapi_ticket, String url) {
        Map<String, String> ret = new HashMap<String, String>();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        String signature = "";

        //注意这里参数名必须全部小写,且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                  "&noncestr=" + nonce_str +
                  "&timestamp=" + timestamp +
                  "&url=" + url;
        System.out.println(string1);

        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);

        return ret;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }

    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
}
tick 的domain类 用于存放数据使用

Ticket.class

package com.wms.wechat.wx.accessToken;

public class Ticket {
  // 获取到的凭证 
    private String ticket; 
    // 凭证有效时间,单位:秒 
    private int expiresIn;
/**
  * @return the ticket
  */
public String getTicket() {
  return ticket;
}
/**
  * @param ticket the ticket to set
  */
public void setTicket(String ticket) {
  this.ticket = ticket;
}
/**
  * @return the expiresIn
  */
public int getExpiresIn() {
  return expiresIn;
}
/**
  * @param expiresIn the expiresIn to set
  */
public void setExpiresIn(int expiresIn) {
  this.expiresIn = expiresIn;

  
}

Token.class

package com.wms.core.utils.course.pojo;

/**
* 凭证
*
* @author liu
*/
public class Token {
// 接口访问凭证
private String accessToken;
// 凭证有效期,单位:秒
private int expiresIn;

public String getAccessToken() {
  return accessToken;
}

public void setAccessToken(String accessToken) {
  this.accessToken = accessToken;
}

public int getExpiresIn() {
  return expiresIn;
}

public void setExpiresIn(int expiresIn) {
  this.expiresIn = expiresIn;
}
}



action中使用








猜你喜欢

转载自1164720173.iteye.com/blog/2270286