当前位置:网站首页>谷粒学院微信扫码登录过程记录以及bug解决
谷粒学院微信扫码登录过程记录以及bug解决
2022-07-01 03:21:00 【家家小迷弟】
做的是谷粒学院的微信登录,这里他提供的微信开放平台的信息好像出了问题,没有办法正常操作,这里做出如下改变:
原来的配置文件信息:
wx.open.app_id=wxed9954c01bb89b47
wx.open.app_secret=a7482517235173ddb4083788de60b90e
wx.open.redirect_url=http://guli.shop/api/ucenter/wx/callback
现在修改为:
wx.open.app_id=wxed9954c01bb89b47
wx.open.app_secret=a7482517235173ddb4083788de60b90e
wx.open.redirect_url=http://localhost:8160/api/ucenter/wx/callback
还要修改项目启动端口为8160。
ConstantWxUtil取到配置文件的信息:
@Component
public class ConstantWxUtil implements InitializingBean {
@Value("${wx.open.app_id}")
private String appId;
@Value("${wx.open.app_secret}")
private String appSecret;
@Value("${wx.open.redirect_url}")
private String redirectUrl;
public static String WX_OPEN_APP_ID;
public static String WX_OPEN_APP_SECRET;
public static String WX_OPEN_REDIRECT_URL;
@Override
public void afterPropertiesSet() throws Exception {
WX_OPEN_APP_ID = appId;
WX_OPEN_APP_SECRET = appSecret;
WX_OPEN_REDIRECT_URL = redirectUrl;
}
}
生成二维码方法:
@GetMapping("login")
public String genQrConnect(HttpSession session) {
// 微信开放平台授权baseUrl
String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
"?appid=%s" +
"&redirect_uri=%s" +
"&response_type=code" +
"&scope=snsapi_login" +
"&state=%s" +
"#wechat_redirect";
// 回调地址
String redirectUrl = ConstantWxUtil.WX_OPEN_REDIRECT_URL;
try {
redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8"); //url编码
} catch (UnsupportedEncodingException e) {
throw new GuliException(20001, e.getMessage());
}
String state = "imhelen";
System.out.println("state = " + state);
//生成qrcodeUrl
String qrcodeUrl = String.format(
baseUrl,
ConstantWxUtil.WX_OPEN_APP_ID,
redirectUrl,
state);
return "redirect:" + qrcodeUrl;
}
扫码登录方法:
@Autowired
private UcenterMemberService memberService;
@GetMapping("callback")
public String callback(String code, String state, HttpSession session) {
//获取到code
//拿着code请求微信固定地址,得到access_token和openid
//得到授权临时票据code
System.out.println("code = " + code);
System.out.println("state = " + state);
try{
String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" +
"?appid=%s" +
"&secret=%s" +
"&code=%s" +
"&grant_type=authorization_code";
String accessTokenUrl = String.format(baseAccessTokenUrl,
ConstantWxUtil.WX_OPEN_APP_ID,
ConstantWxUtil.WX_OPEN_APP_SECRET,
code);
String accessTokenInfo = HttpClientUtils.get(accessTokenUrl);
//获取accessToken和openid
String result = null;
Gson gson=new Gson();
HashMap mapAccessToken = gson.fromJson(accessTokenInfo, HashMap.class);
String access_token = (String) mapAccessToken.get("access_token");
String openid = (String) mapAccessToken.get("openid");
//将扫码人信息添加到数据库
//判断数据库中是否有重复微信信息
UcenterMember member=memberService.getOpenIdMember(openid);
if (member==null){
String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
"?access_token=%s" +
"&openid=%s";
String userInfoUrl = String.format(
baseUserInfoUrl,
access_token,
openid
);
String userInfo = HttpClientUtils.get(userInfoUrl);
HashMap userInfoMap = gson.fromJson(userInfo, HashMap.class);
String nickname = (String) userInfoMap.get("nickname");
String headimgurl = (String) userInfoMap.get("headimgurl");
//添加
member=new UcenterMember();
member.setNickname(nickname);
member.setAvatar(headimgurl);
member.setOpenid(openid);
memberService.save(member);
}
//使用jwt根据member对象生成token字符串
String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
return "redirect:http://localhost:3000?token="+jwtToken;
}catch (Exception e){
throw new GuliException(20001,"登录失败");
}
}
前端页面方法:意义:当页面初始化的时候,判断路径中是否有token参数,如果有,调用wxLogin方法,把token放到cookie中,再调用获取用户信息方法,根据token获取用户信息展示。
created() {
//获取路径中的token的值
this.token=this.$route.query.token
if(this.token){
this.wxLogin()
}
},
methods: {
//微信登录
wxLogin(){
cookie.set("guli_token",this.token,{
domain: 'localhost'})
cookie.set('guli_ucenter','', {
domain: 'localhost' })
loginApi.getLoginInfo()
.then(response=>{
this.loginInfo= response.data.data.userInfo
cookie.set('guli_ucenter', this.loginInfo, {
domain: 'localhost' })
})
}
}
图解如下:

简单概括一下:首先拿着app_id,app_secret,redirect_url去请求微信的一个接口,微信会返回来一个二维码,用户扫码确认登录以后,执行callback回调方法,会传过来两个值,一个state,一个code,,然后拿着code请求微信提供的一个接口,得到两个值,一个access_token为访问凭证,一个openid为每个微信的唯一标识,拿着这两个值再去请求微信的一个接口,就可以得到扫码人的微信头像昵称等信息。
个人理解为,拿着微信应用的钥匙打开一道门,得到一张地图,扫描地图得到钥匙,通过钥匙打开一道门,又得到一个钥匙,再打开一道门,才得到有用的信息。
边栏推荐
- idea插件备份表
- 串口接收数据方案设计
- 服务器渲染技术jsp
- FCN全卷积网络理解及代码实现(来自pytorch官方实现)
- 数据库DDL(Data Definition Language,数据定义语言)知识点
- ASGNet论文和代码解读2
- Edlines: a real time line segment detector with a false detection control
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- 后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
- Overview of EtherCAT principle
猜你喜欢

Ctfshow blasting WP

Edge drawing: a combined real-time edge and segment detector

LeetCode 128最长连续序列(哈希set)

Feign remote call and getaway gateway

FCN全卷积网络理解及代码实现(来自pytorch官方实现)

IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
![[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理

岭回归和lasso回归

pytorch nn.AdaptiveAvgPool2d(1)

过滤器 Filter
随机推荐
10、Scanner. Next() cannot read spaces /indexof -1
The shell script uses two bars to receive external parameters
Subnet division and subnet summary
E15 solution for cx5120 controlling Huichuan is620n servo error
Valid brackets (force deduction 20)
Md5sum operation
排序链表(归并排序)
实现pow(x,n)函数
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
Edge drawing: a combined real-time edge and segment detector
Research on target recognition and tracking based on 3D laser point cloud
数据库中COMMENT关键字的使用
[daily training] 1175 Prime permutation
Develop industrial Internet with the technical advantages of small programs
Leetcode 128 longest continuous sequence (hash set)
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Depth first traversal of C implementation Diagram -- non recursive code
[nine day training] content III of the problem solution of leetcode question brushing Report
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)