3,156
社区成员




private static final String APPID = "??";
private static final String SCOPE = "snsapi_base";
private static String REDIRECT_URI = "https://b3d6bc4a.ngrok.io/WeChat/getOpenid.do";
private static String URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession session = request.getSession();
String openid = (String) request.getSession().getAttribute("openid");
if (openid == null) {
REDIRECT_URI = UrlUtil.getURLEncoderString(REDIRECT_URI);
URL = URL.replace("APPID", APPID).replace("REDIRECT_URI", REDIRECT_URI).replace("SCOPE", SCOPE);
session.setAttribute("reqPath", request.getServletPath());
response.sendRedirect(URL);
session.setAttribute("URL", URL);
return false;
} else {
return true;
}
}
private static final String APPID = "??";
private static final String SECRET = "??";
private static String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
@RequestMapping("/getOpenid")
public String getOpenid(HttpServletRequest request, HttpServletResponse response) {
String path = (String) request.getSession().getAttribute("reqPath");
// 用户同意授权后,能获取到code
String code = request.getParameter("code");
// 获取网页授权access_token
JSONObject object = getOauth2AccessToken(APPID, SECRET, code);
request.getSession().setAttribute("openid", object.getString("openid"));
return "redirect:" + path;
}
public static JSONObject getOauth2AccessToken(String appId, String appSecret, String code) {
// 拼接请求地址
requestUrl = requestUrl.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
// 获取网页授权凭证
JSONObject jsonObject = null;
try {
jsonObject = HttpUtil.getJsonForGet(requestUrl);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
private static final String APPID = "??";
private static final String SECRET = "??";
private String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
@RequestMapping("/getOpenid")
public String getOpenid(HttpServletRequest request, HttpServletResponse response) {
String path = (String) request.getSession().getAttribute("reqPath");
// 用户同意授权后,能获取到code
String code = request.getParameter("code");
// 获取网页授权access_token
JSONObject object = getOauth2AccessToken(APPID, SECRET, code);
request.getSession().setAttribute("openid", object.getString("openid"));
return "redirect:" + path;
}
public JSONObject getOauth2AccessToken(String appId, String appSecret, String code) {
// 拼接请求地址
requestUrl = requestUrl.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
// 获取网页授权凭证
JSONObject jsonObject = null;
try {
jsonObject = HttpUtil.getJsonForGet(requestUrl);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}