第三步:刷新access_token(如果需要)
第四步:拉取用户信息(需scope为 snsapi_userinfo)
这三步我们应该在上面授权回调方法中调用,起初我在想每一个方法都要返回一个json,那么是不是我要三个控制器方法才行?,怎样才能在一个控制器里调用三个方法呢?
感谢大神提供的发送GET请求的工具类:
public static JSONObject doGetJson(String url) throws Exception, IOException {
JSONObject jsonObject = null;
//初始化httpClient
DefaultHttpClient client = new DefaultHttpClient();
//用Get方式进行提交
HttpGet httpGet = new HttpGet(url);
//发送请求
HttpResponse response = client.execute(httpGet);
//获取数据
HttpEntity entity = response.getEntity();
//格式转换
if (entity != null) {
String result = EntityUtils.toString(entity, “UTF-8”);
jsonObject = JSONObject.fromObject(result);
}
//释放链接
httpGet.releaseConnection();
return jsonObject;
}
最后附上我的授权回调方法:
/**
- 微信 授权登录回调
**/
@RequestMapping(“/callback”)
public void callback(String code, String state, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println(“" + code + "=" + state + "==”);
logger.debug(“code===” + code);
logger.debug(“state===” + state);
if (StringUtils.isNotEmpty(code)) {
logger.debug(“sssssssss====” + code);
StringBuilder url = new StringBuilder();
url.append(“https://api.weixin.qq.com/sns/oauth2/access_token?”);
//微信公众平台的AK和SK
url.append(“appid=” + WeixinConfig.appId);
url.append(“&secret=” + WeixinConfig.appSecret);
//这是微信回调给你的code
url.append(“&code=” + code);
url.append(“&grant_type=authorization_code”);
System.out.println(“url.toString()===” + url.toString());
logger.debug(“url.toString()===” + url.toString());
JSONObject jsonObject = AuthUtil.doGetJson(url.toString());
logger.debug(“jsonObject================”+jsonObject);
//解析jsonStr的字符串
//1.获取微信用户的openid
String openid = jsonObject.getString(“openid”);
//2.获取获取access_token
String access_token = jsonObject.getString(“access_token”);
logger.debug(“openid===” + openid);
logger.debug(“access_token===” + access_token);
String infoUrl = “https://api.weixin.qq.com/sns/userinfo?access_token=” + access_token + “&openid=” + openid
- “&lang=zh_CN”;
logger.debug(“infoUrl===” + infoUrl);
//3.获取微信用户信息
JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
logger.debug(“userInfo======================”+userInfo);
//至此拿到了微信用户的所有信息,剩下的就是业务逻辑处理部分了
//保存openid和access_token到session
if (openid==null){
logger.debug(“-------------------------微信授权回调,获取用户信息失败!=============================”);
response.sendRedirect(“http://m.huerdai.net/html/Program-error.html”);
return;
}
request.getSession().setAttribute(“openid”, openid);
request.getSession().setAttribute(“access_token”, access_token);
logger.debug(“openid===” + openid);
logger.debug(“access_token===” + access_token);
String sessionid = getRequest().getSession().getId();
//去数据库查询有没有这个 openid
CustomerInfo customerInfoServiceOne = iCustomerInfoService.getOne(new QueryWrapper().eq(“openid”, openid));
//如果没有这一个用户,则创建
if (customerInfoServiceOne == null) {
CustomerInfo customerInfo = new CustomerInfo();
//省略实体set方法
boolean save = registerService.register(customerInfo);
if (save) {
logger.debug(“首次认证:http://m.huerdai.net”);
redisTemplate.opsForValue().set(sessionid, customerInfoServiceOne.getCustomerId());
// response.sendRedirect(“http://m.huerdai.net/index.html”);
response.sendRedirect(“http://m.huerdai.net/html/bind-tel.html”);
return;
} else {
logger.debug(“认证失败!”);
response.sendRedirect(“http://m.huerdai.net/error.html”);
return;
}
} else {
//已经授权过,没有绑定手机号,也是直接跳转到首页
redisTemplate.opsForValue().set(sessionid, customerInfoServiceOne.getCustomerId());
if (customerInfoServiceOne.getMobilePhone() == null) {
logger.debug(“已经授权过,没有绑定手机号,也是直接跳转到首页”);
//并且将用户信息存到Redis中
// response.sendRedirect(“http://m.huerdai.net/index.html”);
response.sendRedirect(“http://m.huerdai.net/html/bind-tel.html”);
return;
} else {
//已经授权过,并且已经绑定手机号
logger.debug(“有openid的跳转http://m.huerdai.net222222”);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。
因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!
由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频
如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
1712523258038)]
[外链图片转存中…(img-cxrdhWMa-1712523258039)]
既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!
由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频
如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
[外链图片转存中…(img-TwUYQ4Pn-1712523258039)]