123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- package com.fdkankan.pay.wx.sdk;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.WriterException;
- import com.google.zxing.common.BitMatrix;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import javax.crypto.Mac;
- import javax.crypto.spec.SecretKeySpec;
- import javax.xml.XMLConstants;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayInputStream;
- import java.io.InputStream;
- import java.io.StringWriter;
- import java.security.MessageDigest;
- import java.util.*;
- public class WXPayUtil {
- /**
- * XML格式字符串转换为Map
- *
- * @param strXML XML字符串
- * @return XML数据转换后的Map
- * @throws Exception
- */
- public static Map<String, String> xmlToMap(String strXML) throws Exception {
- Map<String, String> data = new HashMap<String, String>();
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- // 禁用XML外部实体注入
- /**
- * XXE
- * XML 外部实体注入漏洞(XML External Entity Injection,简称 XXE),
- * 是一种容易被忽视,但危害巨大的漏洞。它可以利用 XML 外部实体加载注入,
- * 执行不可预控的代码,可导致读取任意文件、执行系统命令、探测内网端口、攻击内网网站等危害。
- */
- /**
- * 原理:
- * 通常,我们在使用微信支付时,商家会有一个通知的 URL 来接收异步支付结果。
- * 然而,微信在 JAVA 版本的 SDK 存在一个 XXE 漏洞来处理这个结果。
- * 由此攻击者可以向通知的 URL 中构建恶意的回调数据,以便根据需要窃取商家服务器上的任意信息。
- * 一旦攻击者获得商家的关键安全密钥(md5-key 和merchant-Id),
- * 那么他们可以通过发送伪造的信息来欺骗商家而无需付费购买任意商品。
- */
- documentBuilderFactory.setExpandEntityReferences(false);
- documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
- DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
- InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
- org.w3c.dom.Document doc = documentBuilder.parse(stream);
- doc.getDocumentElement().normalize();
- NodeList nodeList = doc.getDocumentElement().getChildNodes();
- for (int idx=0; idx<nodeList.getLength(); ++idx) {
- Node node = nodeList.item(idx);
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- org.w3c.dom.Element element = (org.w3c.dom.Element) node;
- data.put(element.getNodeName(), element.getTextContent());
- }
- }
- try {
- stream.close();
- }
- catch (Exception ex) {
- }
- return data;
- }
- /**
- * 将Map转换为XML格式的字符串
- *
- * @param data Map类型数据
- * @return XML格式的字符串
- * @throws Exception
- */
- public static String mapToXml(Map<String, String> data) throws Exception {
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
- org.w3c.dom.Document document = documentBuilder.newDocument();
- org.w3c.dom.Element root = document.createElement("xml");
- document.appendChild(root);
- for (String key: data.keySet()) {
- String value = data.get(key);
- if (value == null) {
- value = "";
- }
- value = value.trim();
- org.w3c.dom.Element filed = document.createElement(key);
- filed.appendChild(document.createTextNode(value));
- root.appendChild(filed);
- }
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- DOMSource source = new DOMSource(document);
- transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- transformer.transform(source, result);
- String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
- try {
- writer.close();
- }
- catch (Exception ex) {
- }
- return output;
- }
- /**
- * 生成带有 sign 的 XML 格式字符串
- *
- * @param data Map类型数据
- * @param key API密钥
- * @return 含有sign字段的XML
- */
- public static String generateSignedXml(final Map<String, String> data, String key) throws Exception {
- return generateSignedXml(data, key, WXPayConstants.SignType.MD5);
- }
- /**
- * 生成带有 sign 的 XML 格式字符串
- *
- * @param data Map类型数据
- * @param key API密钥
- * @param signType 签名类型
- * @return 含有sign字段的XML
- */
- public static String generateSignedXml(final Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
- String sign = generateSignature(data, key, signType);
- data.put(WXPayConstants.FIELD_SIGN, sign);
- return mapToXml(data);
- }
- /**
- * 判断签名是否正确
- *
- * @param xmlStr XML格式数据
- * @param key API密钥
- * @return 签名是否正确
- * @throws Exception
- */
- public static boolean isSignatureValid(String xmlStr, String key) throws Exception {
- Map<String, String> data = xmlToMap(xmlStr);
- if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
- return false;
- }
- String sign = data.get(WXPayConstants.FIELD_SIGN);
- return generateSignature(data, key).equals(sign);
- }
- /**
- * 判断签名是否正确,必须包含sign字段,否则返回false。使用MD5签名。
- *
- * @param data Map类型数据
- * @param key API密钥
- * @return 签名是否正确
- * @throws Exception
- */
- public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception {
- return isSignatureValid(data, key, WXPayConstants.SignType.MD5);
- }
- /**
- * 判断签名是否正确,必须包含sign字段,否则返回false。
- *
- * @param data Map类型数据
- * @param key API密钥
- * @param signType 签名方式
- * @return 签名是否正确
- * @throws Exception
- */
- public static boolean isSignatureValid(Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
- if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
- return false;
- }
- String sign = data.get(WXPayConstants.FIELD_SIGN);
- return generateSignature(data, key, signType).equals(sign);
- }
- /**
- * 生成签名
- *
- * @param data 待签名数据
- * @param key API密钥
- * @return 签名
- */
- public static String generateSignature(final Map<String, String> data, String key) throws Exception {
- return generateSignature(data, key, WXPayConstants.SignType.MD5);
- }
- /**
- * 生成签名. 注意,若含有sign_type字段,必须和signType参数保持一致。
- *
- * @param data 待签名数据
- * @param key API密钥
- * @param signType 签名方式
- * @return 签名
- */
- public static String generateSignature(final Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
- Set<String> keySet = data.keySet();
- String[] keyArray = keySet.toArray(new String[keySet.size()]);
- Arrays.sort(keyArray);
- StringBuilder sb = new StringBuilder();
- for (String k : keyArray) {
- if (k.equals(WXPayConstants.FIELD_SIGN)) {
- continue;
- }
- if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
- sb.append(k).append("=").append(data.get(k).trim()).append("&");
- }
- sb.append("key=").append(key);
- if (WXPayConstants.SignType.MD5.equals(signType)) {
- return MD5(sb.toString()).toUpperCase();
- }
- else if (WXPayConstants.SignType.HMACSHA256.equals(signType)) {
- return HMACSHA256(sb.toString(), key);
- }
- else {
- throw new Exception(String.format("Invalid sign_type: %s", signType));
- }
- }
- /**
- * 获取随机字符串 Nonce Str
- *
- * @return String 随机字符串
- */
- public static String generateNonceStr() {
- return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
- }
- /**
- * 生成 MD5
- *
- * @param data 待处理数据
- * @return MD5结果
- */
- public static String MD5(String data) throws Exception {
- MessageDigest md = MessageDigest.getInstance("MD5");
- byte[] array = md.digest(data.getBytes("UTF-8"));
- StringBuilder sb = new StringBuilder();
- for (byte item : array) {
- sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
- }
- return sb.toString().toUpperCase();
- }
- /**
- * 生成 HMACSHA256
- * @param data 待处理数据
- * @param key 密钥
- * @return 加密结果
- * @throws Exception
- */
- public static String HMACSHA256(String data, String key) throws Exception {
- Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
- SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
- sha256_HMAC.init(secret_key);
- byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
- StringBuilder sb = new StringBuilder();
- for (byte item : array) {
- sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
- }
- return sb.toString().toUpperCase();
- }
- /**
- * 根据url生成二位图片对象
- *
- * @param codeUrl
- * @return
- * @throws WriterException
- */
- public static BufferedImage getQRCodeImge(String codeUrl) throws WriterException {
- int width = 300;
- //根据url生成二维码
- MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
- // 设置二维码参数
- Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- BitMatrix bitMatrix = multiFormatWriter.encode(codeUrl, BarcodeFormat.QR_CODE, width, width, hints);
- BufferedImage image = new BufferedImage(width, width, 1);
- for(int x = 0; x < width; ++x) {
- for(int y = 0; y < width; ++y) {
- image.setRGB(x, y, bitMatrix.get(x, y) ? -16777216 : -1);
- }
- }
- return image;
- }
- }
|