package com.mingshine.cloudguardian.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mingshine.cloudguardian.service.WXService;
import com.mingshine.cloudguardian.utils.common.CheckoutUtil;
import com.mingshine.cloudguardian.utils.wx.MessageUtil;
import com.mingshine.cloudguardian.utils.wx.NetUtil;
@RestController
@RequestMapping("/wechat")
public class WeChatController {
private Logger log = LoggerFactory.getLogger(MyDeviceController.class);
private static final String APP_ID = "wx3dd909f863ce675c";
private static final String APP_SECRET = "954f34bedfbe6227a14275cf9eaf2ea1";
private static final String URL_LOGIN = "http://192.168.1.100:8888/";
@Resource
private WXService mWXService;
/**
* 微信消息接收和token验证
*
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/weChatToken")
public void weChat(HttpServletRequest request, HttpServletResponse response)
throws IOException {
// 将请求、响应的编码均设置为UTF-8(防止中文乱码)
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
boolean isGet = request.getMethod().toLowerCase().equals("get");
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 {
PrintWriter print = response.getWriter();
print.write(echostr);
print.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// xml请求解析
try {
Map<String, String> requestMap = MessageUtil.xml2Map(request);
mWXService.handler(response, requestMap);
System.out.println(JSON.toJSONString(requestMap));
} catch (Exception e) {
e.printStackTrace();
}
}
}
@RequestMapping("/getOAuth")
public String getOAuth(HttpServletRequest request,
HttpServletResponse response) {
String code = request.getParameter("code");
JSONObject oauth2TokenJson = getOauth2Token(APP_ID, APP_SECRET, code);
log.info(oauth2TokenJson.toJSONString());
log.info(oauth2TokenJson.getString("openid"));
try {
response.sendRedirect(URL_LOGIN);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取网页授权凭证
*
* @param appId
* 公众账号的唯一标识
* @param appSecret
* 公众账号的密钥
* @param code
* @return
*/
private JSONObject getOauth2Token(String appId, String appSecret,
String code) {
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
requestUrl = requestUrl.replace("APPID", appId);
requestUrl = requestUrl.replace("SECRET", appSecret);
requestUrl = requestUrl.replace("CODE", code);
String respStr = NetUtil.get(requestUrl);
return JSON.parseObject(respStr.trim());
}
}
获取openid 连接
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3dd909f863ce675c&redirect_uri=http%3a%2f%2fzhaoyunwsx.xicp.net%2f%2fcloud-guardian%2fwechat%2fgetOAuth&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect