java实现微信第三方登录流程源码详解,所遇到的坑(1)

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”);

response.sendRedirect(“http://m.huerdai.net/index.html”);

return;

}

}

} else {

logger.debug(“code获取失败!====” + code);

// return new ModelAndView(“redirect:http://m.huerdai.net/error.html”);

response.sendRedirect(“http://m.huerdai.net/error.html”);

}

}

总结

以上是字节二面的一些问题,面完之后其实挺后悔的,没有提前把各个知识点都复习到位。现在重新好好复习手上的面试大全资料(含JAVA、MySQL、算法、Redis、JVM、架构、中间件、RabbitMQ、设计模式、Spring等),现在起闭关修炼半个月,争取早日上岸!!!

下面给大家分享下我的面试大全资料

  • 第一份是我的后端JAVA面试大全

image.png

后端JAVA面试大全

  • 第二份是MySQL+Redis学习笔记+算法+JVM+JAVA核心知识整理

字节二面拜倒在“数据库”脚下,闭关修炼半个月,我还有机会吗?

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

  • 第三份是Spring全家桶资料

字节二面拜倒在“数据库”脚下,闭关修炼半个月,我还有机会吗?

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

Z-1714359597979)]

后端JAVA面试大全

  • 第二份是MySQL+Redis学习笔记+算法+JVM+JAVA核心知识整理

[外链图片转存中…(img-kQlX7aFS-1714359597979)]

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

  • 第三份是Spring全家桶资料

[外链图片转存中…(img-cBsNKUYk-1714359597980)]

MySQL+Redis学习笔记算法+JVM+JAVA核心知识整理

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值