这个和QQ登录差不多。具体详细步骤也不多说了,直接参考官网即可,这里写上主要代码
https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
这一步可以放在后台代码里面,也可以放js里面,看自己喜好
@RequestMapping("/memeber/wxLogin")
public void test(HttpServletResponse response) {
String baseUrl="https://open.weixin.qq.com/connect/qrconnect?"
+ "appid="+wx_appid
+"&redirect_uri="+wx_redirecturl
+"&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
try {
response.sendRedirect(baseUrl);
} catch (IOException e) {
e.printStackTrace();
}
}
在对应的回调域里面写处理
@RequestMapping("/weixinlogin")
public String wxcallback(String code,HttpServletResponse response) throws Exception {
String baseUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?code=" + code + "&appid=wx3bdb1192c22883f3&secret=db9d6b88821df403e5ff11742e799105&grant_type=authorization_code";
String msg = restTemplate.getForObject(baseUrl, String.class);
Map<String, Object> map1 = JSON.parseObject(msg, Map.class);//字符串形式转map
Object access_token = map1.get("access_token");
Object openid = map1.get("openid");
String infoUrl="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid;
//获取到的用户信息也是String格式,也需要转一下map,和上面那个类似
String user_info =restTemplate.getForObject(infoUrl.toString(), String.class);
JSONObject parseObject = JSON.parseObject(user_info);
Map<String, Object> map= new HashMap<String, Object>();
map=parseObject;
//这个map就是用户信息
return "redirect:/";
}