//需要配置跳转地址
String jumpUrl =request.getContextPath()+"/page/story/storyindex.jsp";
//以下为自动处理,不需要更改
String code = request.getParameter("code");
String rpath = PropertiesUtil.getProperty("yu")+request.getRequestURI();
String oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+PropertiesUtil.getProperty("appid")
+"&redirect_uri="+URLEncoder.encode(rpath,"UTF-8")+"&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
if(code==null){
response.sendRedirect(oauth2Url);
}else{
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+PropertiesUtil.getProperty("appid")
+"&secret="+PropertiesUtil.getProperty("secret")+"&code="+code+"&grant_type=authorization_code";
String openidResult = HttpUtil.get(url,true);
JSONObject fromObjectopenid = JSONObject.fromObject(openidResult.toString());
if(fromObjectopenid.toString().indexOf("openid")>-1){
String openid = fromObjectopenid.get("openid").toString();
session.setAttribute("openid",openid);
response.sendRedirect(jumpUrl);
}else{
response.sendRedirect(oauth2Url);
}
}
%>