/**
* 微信消息接收和token验证
* @param model
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/ownerCheck")
public void ownerCheck(Model model, HttpServletRequest request,HttpServletResponse response) throws IOException {
System.out.println(111);
boolean isGet = request.getMethod().toLowerCase().equals("get");
PrintWriter print;
if (isGet) {
// 微信加密签名
String signature = request.getParameter("signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (signature != null && CheckoutUtil.checkSignature(signature, timestamp, nonce)) {
try {
print = response.getWriter();
print.write(echostr);
print.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CheckoutUtil {
// 与接口配置信息中的Token要一致
private static String token = "handsomeKing";
/**
* 验证签名
*
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static boolean checkSignature(String signature, String timestamp, String nonce) {
String[] arr = new String[] { token, timestamp, nonce };
// 将token、timestamp、nonce三个参数进行字典序排序
// Arrays.sort(arr);
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;
}
/**
* 将字节数组转换为十六进制字符串
*
* @param byteArray
* @return
*/
private static String byteToStr(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
/**
* 将字节转换为十六进制字符串
*
* @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];
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
public static void sort(String a[]) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[j].compareTo(a[i]) < 0) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
/**
* 第一步:用户同意授权,获取code(引导关注者打开如下页面:)
* 获取 code、state
*/
public static String getStartURLToGetCode() {
String takenUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
takenUrl= takenUrl.replace("APPID", Param.APPID);
takenUrl= takenUrl.replace("REDIRECT_URI", URL.encode(Param.REDIRECT_URI));
//FIXME : snsapi_userinfo
takenUrl= takenUrl.replace("SCOPE", "snsapi_userinfo");
System.out.println(takenUrl);
return takenUrl;
}
/**
* 获取access_token、openid
* 第二步:通过code获取access_token
* @param code url = "https://api.weixin.qq.com/sns/oauth2/access_token
* ?appid=APPID
* &secret=SECRET
* &code=CODE
* &grant_type=authorization_code"
* */
public static OAuthInfo getAccess_token(String code){
String authUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code ";
authUrl= authUrl.replace("APPID", Param.APPID);
authUrl = authUrl.replace("SECRET", Param.SECRET);
authUrl = authUrl.replace("CODE", code);
String jsonString = HTTPRequestUtil.sendPost(authUrl,"");
System.out.println("jsonString: " + jsonString);
OAuthInfo auth = null;
try {
auth = (OAuthInfo) JacksonUtil.parseJSONToObject(OAuthInfo.class, jsonString);
} catch (Exception e) {
e.printStackTrace();
}
return auth;
}
/**
* 微信引导页进入的方法
* @return
*/
@RequestMapping("/loginByWeiXin")
public String loginByWeiXin(HttpServletRequest request, Map<String, Object> map) {
// 微信接口自带 2 个参数
String code = request.getParameter("code");
String state = request.getParameter("state");
System.out.println("code = " + code + ", state = " + state);
if(code != null && !"".equals(code)) {
// 授权成功, 微信获取用户openID
OAuthInfo authInfo = WeiXinUtil.getAccess_token(code);
String openid = authInfo.getOpenid();
String access_token = authInfo.getAccess_token();
if(access_token == null) {
// Code 使用过 异常
System.out.println("Code 使用过 异常.....");
return "redirect:" + WeiXinUtil.getStartURLToGetCode();
}
// 数据库中查询微信号是否绑定平台账号
SysUser sysUser = weiXinService.getUserByWeiXinID(openid);
if(sysUser == null) {
String randomStr = StringUtil.getRandomString(50);
request.getSession().setAttribute(openid, randomStr);
// 尚未绑定账号
System.out.println("尚未绑定账号.....");
return "redirect:/index.jsp?openid=" + openid + "&state=" + randomStr;
}
userController.doSomeLoginWorkToHomePage(sysUser.getMcid(), map);
// 登录成功
return "homePage";
}
// 未授权
return "redirect:" + WeiXinUtil.getStartURLToGetCode();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有