WeixinSignUtils.java 5.1 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
package com.boco.nbd.wios.wx.weixin;

import com.boco.nbd.wios.manage.util.SpringContextUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @author yong
 */
@Component
public class WeixinSignUtils {

    private static String weixinAppId;

    @Value("${weixin.appId}")
    public void setWeixinAppId(String appId){
        WeixinSignUtils.weixinAppId = appId;
    }

    private static String weixinAppSecret;

    @Value("${weixin.appSecret}")
    public void setWeixinAppSecret(String appSecret){
        WeixinSignUtils.weixinAppSecret = appSecret;
    }

    private static String h5Url;

    @Value("${url.h5}")
    public void setH5Url(String h5Url){
        WeixinSignUtils.h5Url = h5Url;
    }
	
	private final static String token = "cams_wios";
	
	/**
	 * 获取当前url
     * 当前网页的URL,不包含#及其后面部分
	 * @return
	 */
	private static String getUrl(){
        HttpServletRequest request = SpringContextUtil.getRequest();
        String url = request.getRequestURL().toString();
        if(StringUtils.isNotBlank(h5Url)) {
            url=h5Url+"/";
        }
        
        String queryString = request.getQueryString();
        if(StringUtils.isNotEmpty(queryString)){
        	return url +"?"+queryString;
        }
        return url;
    }
	
	/**
	 * 获取随机码
	 * @return
	 */
	private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }
	
	/**
	 * 获取时间戳
	 * @return
	 */
	private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
	
    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;
    }
    
    public static Map<String, String> sign(String jsapi_ticket, String url) {
        Map<String, String> ret = new HashMap<>(8);
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String str;
        String signature = "";
 
        //注意这里参数名必须全部小写,且必须有序
        str = "jsapi_ticket=" + jsapi_ticket +
                  "&noncestr=" + nonce_str +
                  "&timestamp=" + timestamp +
                  "&url=" + url;
 
        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(str.getBytes(StandardCharsets.UTF_8));
            signature = byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException 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;
    }
    
    public static boolean checkSignature(String signature, String timestamp, String nonce) {  
        String[] arr = new String[] { token, timestamp, nonce };  
        // 将token、timestamp、nonce三个参数进行字典序排序  
        Arrays.sort(arr);  
        StringBuilder content = new StringBuilder();  
        for (int i = 0; i < arr.length; i++) {  
            content.append(arr[i]);  
        }  
        MessageDigest md = null;  
        String tmpStr = null;  
  
        try {  
            md = MessageDigest.getInstance("SHA-1");  
            // 将三个参数字符串拼接成一个字符串进行sha1加密  
            byte[] digest = md.digest(content.toString().getBytes());  
            tmpStr = byteToStr(digest);  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        }  
  
        content = null;  
        // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信  
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;  
    }
    
    private static String byteToStr(byte[] byteArray) {  
        String strDigest = "";  
        for (int i = 0; i < byteArray.length; i++) {  
            strDigest += byteToHexStr(byteArray[i]);  
        }  
        return strDigest;  
    }  
    
    private static String byteToHexStr(byte mByte) {  
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };  
        char[] tempArr = new char[2];  
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];  
        tempArr[1] = Digit[mByte & 0X0F];  
  
        String s = new String(tempArr);  
        return s;  
    }
    
    
    public static Map<String, String> getParam(){
    	String ticket=WeixinHelper.getJsapiTicket(weixinAppId, weixinAppSecret);
    	String url=getUrl();
    	Map<String, String> result=sign(ticket, url);
    	
    	result.put("appId", weixinAppId);
    	
    	return result;
    }
}