master
zyy 5 years ago
parent 44c0f440f8
commit 68a33f77f6

@ -1,16 +1,16 @@
package com.bsd.say.controller; package com.bsd.say.controller;
import com.bsd.say.config.RedisProperies; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsd.say.beans.AjaxResult;
import com.bsd.say.service.WxOpenServiceDemo; import com.bsd.say.service.WxOpenServiceDemo;
import com.bsd.say.service.impl.WeixinService; import com.bsd.say.service.impl.WeixinService;
import com.bsd.say.util.AESWithJCEUtils;
import com.bsd.say.util.HttpRequestUtils;
import com.bsd.say.util.LogUtils; import com.bsd.say.util.LogUtils;
import com.bsd.say.util.Xml2MapUtil;
import com.bsd.say.util.wechat.AesException; import com.bsd.say.util.wechat.AesException;
import com.bsd.say.util.wechat.WXBizMsgCrypt;
import com.sun.org.apache.bcel.internal.generic.NEW;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage; import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -18,21 +18,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Map;
@RestController @RestController
@RequestMapping("wechat") @RequestMapping("wechat")
@ -134,9 +125,28 @@ public class WechatController {
} }
@RequestMapping("test") @RequestMapping("test")
public void test(){ public void test() {
// Object o = redisTemplate.opsForValue().getOperations(); // Object o = redisTemplate.opsForValue().getOperations();
weixinService.refreshComponentAccessToken(); weixinService.refreshComponentAccessToken();
} }
@RequestMapping("autologin")
public AjaxResult autoLogin(@RequestParam String openId) throws IOException {
String result1 = HttpRequestUtils.sendGet("https://api.weq.me/wx/token.php?id=15969759463491&key=1234567890123456");
JSONObject result2 = JSONObject.parseObject(result1);
String result3 = result2.getString("access_token");
String pubkey = "1234567890123456";
String iv = "WJi7HTZQoh8eHjup";
String decode = AESWithJCEUtils.aesDecode(result3, pubkey, iv);
String resutl = HttpRequestUtils.sendGet("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + decode + "&openid=" + openId + "&lang=zh_CN");
JSONObject jsonObject = JSONObject.parseObject(resutl);
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.setData(jsonObject);
return ajaxResult;
}
} }

@ -0,0 +1,77 @@
package com.bsd.say.util;
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
public class AESWithJCEUtils {
/**
* TokenURL http://XXX
* @param args
*/
public static void main(String[] args) {
String pubkey = "1234567890123456";
String iv = "WJi7HTZQoh8eHjup";
String content ="pnHNKu5KCZibpf2iKU7YqYp4TXtDjaV/cvD1E1YlgLjXU9eDzClRa70AgEIvEuPo+A4F8ZwkWeWLjKhFVReywxNdGwXWZy9Hj7CVhTnmkyQ2wo0dLTY+IiC/HPdxPrpCeGuPRABpZjQ+S33VQSP1vywIoKEmTPGU2JSbu0tGiOsZYk3Lhq7vJ3TMljhq0k8R5j5yBYNMgd3Az9+7+LfIZw==";
// String encode = aesEncode(content, pubkey, iv);
// System.out.println(encode);
String decode = aesDecode(content, pubkey, iv);
System.out.println(decode);
}
/**
*
* @param content -
* @param pubKey - APIkey(16)
* @param iv - IV16
* @return
*/
public static String aesEncode(String content, String pubKey, String iv){
try{
Security.addProvider(new BouncyCastleProvider());
Key key = new SecretKeySpec(pubKey.getBytes(), "AES");
Cipher in = Cipher.getInstance("AES/CBC/PKCS5Padding");
in.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv.getBytes()));
byte[] enc = in.doFinal(content.getBytes());
String result = (new String(Base64.encode(enc))).replaceAll("\n", "");
return result;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
/**
*
* @param mima -
* @param pubKey - APIkey
* @param iv - IV16
* @return
*/
public static String aesDecode(String mima, String pubKey, String iv){
try{
Security.addProvider(new BouncyCastleProvider());
Key key = new SecretKeySpec(pubKey.getBytes(), "AES");
Cipher out = Cipher.getInstance("AES/CBC/PKCS5Padding");
out.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv.getBytes()));
byte[] dec = out.doFinal(Base64.decode(mima));
String result = new String(dec);
return result;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
}

@ -0,0 +1,145 @@
package com.bsd.say.util;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
/**
* ClassName: MessageUtil
* @Description:
* @author dapengniao
* @date 201637 10:05:04
*/
public class MessageUtil {
/**
*
*/
public static final String RESP_MESSAGE_TYPE_TEXT = "text";
/**
*
*/
public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
/**
*
*/
public static final String RESP_MESSAGE_TYPE_NEWS = "news";
/**
*
*/
public static final String REQ_MESSAGE_TYPE_TEXT = "text";
/**
*
*/
public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
/**
*
*/
public static final String REQ_MESSAGE_TYPE_LINK = "link";
/**
*
*/
public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
/**
*
*/
public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
/**
*
*/
public static final String REQ_MESSAGE_TYPE_EVENT = "event";
/**
* subscribe()
*/
public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
/**
* unsubscribe()
*/
public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
/**
* CLICK()
*/
public static final String EVENT_TYPE_CLICK = "CLICK";
/**
* @Description: XML
* @param @param request
* @param @return
* @param @throws Exception
* @author dapengniao
* @date 201637 10:04:02
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
@SuppressWarnings("unused")
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有xml节点的转换都增加CDATA标记
boolean cdata = true;
@SuppressWarnings("rawtypes")
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
}
Loading…
Cancel
Save