当前位置:网站首页>0623~ holiday self study
0623~ holiday self study
2022-07-24 17:47:00 【Life is so hard】
Today, I mainly review the three aspects (VX) Sign in , Go through the previous process
Sort out the wechat login process
General carding :
1. Request from user , Redirect to wechat scanning page
2. return code adopt code Exchange for token
3. adopt token In exchange for user information
4. Judge whether mobile phone binding is required according to user information
1. Click wechat login on the front , Send a request back ;
2. The backend calls the authorization code obtained to verify , Redirect to wechat scanning page ;
//1. The front end sends a request to jump the path
//2. The backend redirects to the wechat scanning page
@GetMapping("/jump")
public String jump(){
//authorization-code-url: "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect"
String url = wechatProperties.getAuthorizationCodeUrl();
String state=UUID.randomUUID().toString().replace("-","");
url=String.format(url,wechatProperties.getAppId(),wechatProperties.getRedirectUri(),state);
return "redirect:"+url;
}3. The user will return after clicking authorization code And state Two figures ;
4. according to code Exchange for token Address ;
//2. adopt code Exchange for token String tokenUrl = wechatProperties.getTokenUrl(); //https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code tokenUrl=String.format(tokenUrl,wechatProperties.getAppId(),wechatProperties.getAppSecrect(),code); System.out.println(tokenUrl);
5. Exchange for token Address backward vx Send the request ; Return the user result set
// Get token Send a request to vx String tokenresult = HttpUtil.sendPost(tokenUrl, null); //2.1 Take what you get token Information Convert to object WechartTokenDto wechartTokenDto = JSONObject.parseObject(tokenresult, WechartTokenDto.class);
6. use token In exchange for user information ;
//3. adopt token Get user information //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID String userinfoUrl = wechatProperties.getUserinfoUrl(); //https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s userinfoUrl = String.format(userinfoUrl, wechartTokenDto.getAccess_token(), wechartTokenDto.getOpenid()); System.out.println(userinfoUrl);
7. Use the user information to vx Send a request , Return the result set of user information ;
String userinfo = HttpUtil.sendPost(userinfoUrl, null); WechatUserInfoDto wechatUserInfoDto = JSONObject.parseObject(userinfo, WechatUserInfoDto.class);
8. use openid The unique ID determines whether the user logs in for the first time ;
9. First login , Then put the user information into storage first , Then jump to the binding mobile number page
//3.1 according to Openid Judge whether the user is logging in for the first time
Example example = new Example(WechatUser.class);
example.and().andEqualTo("openid", wechatUserInfoDto.getOpenid());
WechatUser wechatUserInfo = wechatUserMapper.selectOneByExample(example);
if(wechatUserInfo==null) {
WechatUser wechatUser = new WechatUser();
//4. If openid It's empty Description is the first login Save the wechat scanning code login user into the database
wechatUser.setSex(wechatUserInfoDto.getSex());
wechatUser.setUnionid(wechatUserInfoDto.getUnionid());
wechatUser.setOpenid(wechatUserInfoDto.getOpenid());
String nickname = wechatUserInfoDto.getNickname();
try {
String s = new String(nickname.getBytes("ISO-8859-1"), "utf-8");
wechatUser.setNickname(s);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
wechatUser.setHeadimgurl(wechatUserInfoDto.getHeadimgurl());
wechatUser.setAddress(wechatUserInfoDto.getCity());
wechatUserMapper.insert(wechatUser);
String bindUrl = "http://127.0.0.1:9002/binder.html?id="+wechatUser.getId();
return "redirect:"+bindUrl;10. Not the first login , Judge the phone binding id Is it empty , If it is empty, you will also jump to the mobile phone binding page ;
}else{
//5. If you come here, you have logged in before Judge userId Is it empty If it is empty, jump to the binding page
if(wechatUserInfo.getUserId()==null){
String bindUrl = "http://127.0.0.1:9002/binder.html?id="+wechatUserInfo.getId();
return "redirect:"+bindUrl;11. It's not the first time to log in, but it's also bound to a mobile phone , Jump directly to the home page , Save the login information into redis;
} else{
//6. I've logged in before , It also binds users , Jump directly to the home page
String indexUrl ="http://127.0.0.1:9002/index.html?wechatUserInfo=%s&token=%s";
String token = UUID.randomUUID().toString();
System.out.println("888888888888888888888888888888888888888888888888"+token);
redisTemplate.opsForValue().set(token, JSONObject.toJSONString(wechatUserInfo), 30, TimeUnit.MINUTES);
Map<Object, Object> map = new HashMap<>();
map.put("token",token);
map.put("wechatUserInfo",wechatUserInfo);
indexUrl=String.format(indexUrl,wechatUserInfo,token);
// After scanning the code and logging in successfully, return to the home page
return "redirect:"+indexUrl;
}11. Save the login information into redis Back to the front end ;
Wechat binding steps
1. According to wechat users id Check whether the user's mobile phone has been bound ;
2. Not bound , Add a mobile number to bind , And bind the user id
3. Bound , Then bind the user directly id;
4. Login information is saved redis Back to the front end ;
1. According to wechat users id Check whether the user's mobile phone has been bound ;
WechatUser wechatUser = wechatUserMapper.selectByPrimaryKey(wxBindDto.getWxbindid());
//4. First, compare the mobile number entered by the code scanning wechat user with the user information in the database
// If the mobile number exists, do to vx User binding user id operation
// If the mobile number does not exist Then add a new user data
Example example = new Example(LoginInfo.class);
example.and().andEqualTo("phone",wxBindDto.getPhone());
LoginInfo loginInfo = loginInfoMapper.selectOneByExample(example);2. Not bound , Add a mobile number to bind , And bind the user id
if(loginInfo==null){ // If the mobile number does not exist Then add a new user data loginInfo = new LoginInfo(); loginInfo.setPhone(wxBindDto.getPhone()); loginInfo.setPassword("e10adc3949ba59abbe56e057f20f883e"); loginInfoMapper.insert(loginInfo); }wechatUser.setUserId(loginInfo.getId()); wechatUserMapper.updateByPrimaryKey(wechatUser);
3. Bound , Then bind the user directly id;
wechatUser.setUserId(loginInfo.getId()); wechatUserMapper.updateByPrimaryKey(wechatUser);
4. Login information is saved redis Back to the front end ;
//4. return token And user information to the front end Map<String, Object> map = new HashMap<>(); String token = UUID.randomUUID().toString(); System.out.println(token+"1111111111111111111111111111111"); redisTemplate.opsForValue().set(token,JSONObject.toJSONString(wechatUser),30,TimeUnit.MINUTES); map.put("token",token); map.put("wechatUser",wechatUser); return JSONResult.success(map);
边栏推荐
- 二维卷积——torch.nn.conv2d的使用
- [waiting for insurance] what does waiting for insurance rectification mean? What are the rectification contents?
- 0629~SaaS平台设计~全局异常处理
- 0614~放假自习
- Image information is displayed by browser: data:image/png; Base64, + image content
- Memory allocation and recycling strategy
- 深入解析著名的阿里云Log4j 漏洞
- Iqiyi Tiktok reconciled, Weibo lying gun?
- 再见收费的Navicat!这款开源的数据库管理工具界面更炫酷!
- TCP protocol debugging tool tcpengine v1.3.0 tutorial
猜你喜欢

How does win11 enhance the microphone? Win11 enhanced microphone settings

Getaverse, a distant bridge to Web3

C # print reports using fastreport.net

C语言自定义类型讲解 — 联合体

What are the pitfalls from single architecture to distributed architecture?

Common methods of number and math classes

【网络安全】网站中间件存在的解析漏洞

After separation, the impression notes are still difficult to live, but there are many coquettish operations

PXE高效批量网络装机

生信常用分析图形绘制02 -- 解锁火山图真谛!
随机推荐
C语言中的字符与字符串库函数的使用以及模拟实现
Analog electricity - what is the resistance?
Brats18 - Multimodal MR image brain tumor segmentation challenge continued
Development Series III of GaN (lapgan, srgan)
0701~放假总结
Hcip fourth day notes
mac数据库管理软件Navicat Premium Essentials Mac
C语言自定义类型 — 枚举
The use and Simulation of character and string library functions in C language
C language custom type explanation - structure
Iqiyi Tiktok reconciled, Weibo lying gun?
Make good use of these seven tips in code review, and it is easy to establish your opposition alliance
Hcip day 3
C # print reports using fastreport.net
[network security] analysis vulnerability of website Middleware
Trends of semiconductor industry
High performance complexity analysis of wechat circle of friends
0613~自习
《STL源码剖析》应该怎样读?
Two dimensional convolution -- use of torch.nn.conv2d