发表于: 2019-11-13 21:38:08

0 1073


今天完成的事情:

1、完成微信授权登录


@RequestMapping("/a/u/wxlogin")
public String wxlogin() {
System.out.println("第一步进来了");
/**第一步:用户同意授权,获取code*/
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
"&redirect_uri="+http+
"&response_type=code"+
"&scope=snsapi_userinfo"+
"&state=STATE#wechat_redirect";
System.out.println(url);
   return "redirect:"+url;
}

@RequestMapping("/a/u/wxcallback")
public String wxcallback(String code, ModelMap map, HttpServletRequest request) throws IOException {
/**第二步:通过code换取网页授权access_token*/
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid +
"&secret=" + appsecret +
"&code="+code +
"&grant_type=authorization_code";
System.out.println("url:"+url);
code = request.getParameter("code");
System.out.println("code为:"+code);
JSONObject jsonObject = HttpClientUtil.doGet(url);
String openID = jsonObject.getString("openid");

String access_Token = jsonObject.getString("access_token");
System.out.println(jsonObject);

/**第四步:拉取用户信息(需scope为 snsapi_userinfo)*/
url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_Token +
"&openid=" + openID +
"&lang=zh_CN";
JSONObject userInfoJson = HttpClientUtil.doGet(url);
System.out.println("UserInfo:" + userInfoJson);
   long l = System.currentTimeMillis();

// 1种情况, 是基于微信授权的账号做为我们本系统的账号体系来使用
   // map.put("userinfo", userInfoJson);
   // return "/home";

   // 2种情况: 我们是有账号体系的,微信帐号做来一个关联,来关联我们的账号体系
User user = userMapper.getOpenID(openID);
   if (user == null) {
user = new User();
user.setOpenID(openID);
user.setName((String)userInfoJson.get("nickname"));
user.setImage((String)userInfoJson.get("headimgurl"));
user.setSex((Integer) userInfoJson.get("sex"));
user.setStatus(1);
user.setCreateAt(l);
user.setUpdateAt(l);
userMapper.insert(user);
} else {
user.setName((String)userInfoJson.get("nickname"));
user.setImage((String)userInfoJson.get("headimgurl"));
userMapper.update(user);
}

return "redirect:http://dev.home.rulaifozu.xiuzhenyuan.cn";
}


明天计划的事情:

调试前台个人页面
遇到的问题:

Nginx的location配置转发问题

收获:

对Nginx的配置和微信授权的过程有更深的理解


返回列表 返回列表
评论

    分享到