|
@ -6,6 +6,7 @@ import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
|
|
|
import com.yihu.jw.util.wechat.WeixinMessagePushUtils;
|
|
|
import com.yihu.jw.wechat.service.WxAccessTokenService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -18,6 +19,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.security.MessageDigest;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
/*
|
|
|
* Created by lyr on 2016/08/11.
|
|
@ -36,6 +40,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
private WxAccessTokenService wxAccessTokenService;
|
|
|
@Value("${wechat.id}")
|
|
|
private String wechatId;
|
|
|
private static String wechat_token ="xAeQrX7uOD0OusIZ5JUQzrLPYIQBlqbS" ;
|
|
|
|
|
|
/**
|
|
|
* 微信接口验证
|
|
@ -52,12 +57,50 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
String nonce = request.getParameter("nonce").toString();
|
|
|
String echostr = request.getParameter("echostr").toString();
|
|
|
|
|
|
// 验证成功,返回验证码
|
|
|
response.getWriter().print(echostr);
|
|
|
if (validate(signature, timestamp, nonce)) {
|
|
|
// 验证成功,返回验证码
|
|
|
response.getWriter().print(echostr);
|
|
|
} else {
|
|
|
// 验证失败
|
|
|
response.setStatus(401);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 服务器错误
|
|
|
response.setStatus(500);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信推送消息验证
|
|
|
*
|
|
|
* @param signature
|
|
|
* @param timestamp
|
|
|
* @param nonce
|
|
|
* @return
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
*/
|
|
|
private boolean validate(String signature, String timestamp, String nonce) throws NoSuchAlgorithmException {
|
|
|
String token = this.wechat_token;
|
|
|
// 字典序排序
|
|
|
String[] array = new String[]{token, timestamp, nonce};
|
|
|
Arrays.sort(array);
|
|
|
|
|
|
//连接字典序排序后字符串
|
|
|
String content = "";
|
|
|
for (String str : array) {
|
|
|
content += str;
|
|
|
}
|
|
|
|
|
|
// 解析
|
|
|
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
|
|
String decodeStr = "";
|
|
|
byte[] bytes = md.digest(content.getBytes());
|
|
|
decodeStr = byteToStr(bytes);
|
|
|
|
|
|
if (StringUtils.isNotEmpty(decodeStr) && decodeStr.equals(signature.toUpperCase())) {
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
/* *//*
|
|
@ -166,7 +209,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
*
|
|
|
* @param byteArray
|
|
|
* @return
|
|
|
*//*
|
|
|
*/
|
|
|
private static String byteToStr(byte[] byteArray) {
|
|
|
String strDigest = "";
|
|
|
for (int i = 0; i < byteArray.length; i++) {
|
|
@ -177,12 +220,12 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
|
|
|
|
|
|
|
|
|
*//*
|
|
|
/*
|
|
|
* 将字节转换为字符
|
|
|
*
|
|
|
* @param mByte
|
|
|
* @return
|
|
|
*//*
|
|
|
*/
|
|
|
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];
|
|
@ -190,6 +233,6 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
tempArr[1] = Digit[mByte & 0X0F];
|
|
|
String s = new String(tempArr);
|
|
|
return s;
|
|
|
}*/
|
|
|
}
|
|
|
|
|
|
}
|