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面试大全
后端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核心知识整理