当前位置:网站首页>Millet College wechat scanning code login process record and bug resolution
Millet College wechat scanning code login process record and bug resolution
2022-07-01 03:45:00 【My little brother】
It is the wechat login of cereal college , There seems to be something wrong with the information he provided on the wechat open platform , There is no way to operate normally , The following changes are made here :
The original configuration file information :
wx.open.app_id=wxed9954c01bb89b47
wx.open.app_secret=a7482517235173ddb4083788de60b90e
wx.open.redirect_url=http://guli.shop/api/ucenter/wx/callback
Now change to :
wx.open.app_id=wxed9954c01bb89b47
wx.open.app_secret=a7482517235173ddb4083788de60b90e
wx.open.redirect_url=http://localhost:8160/api/ucenter/wx/callback
Also modify the project startup port to 8160.
ConstantWxUtil Get the information of the configuration file :
@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;
}
}
Generation of two-dimensional code method :
@GetMapping("login")
public String genQrConnect(HttpSession session) {
// Wechat open platform authorization 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";
// token url
String redirectUrl = ConstantWxUtil.WX_OPEN_REDIRECT_URL;
try {
redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8"); //url code
} catch (UnsupportedEncodingException e) {
throw new GuliException(20001, e.getMessage());
}
String state = "imhelen";
System.out.println("state = " + state);
// Generate qrcodeUrl
String qrcodeUrl = String.format(
baseUrl,
ConstantWxUtil.WX_OPEN_APP_ID,
redirectUrl,
state);
return "redirect:" + qrcodeUrl;
}
Code scanning login method :
@Autowired
private UcenterMemberService memberService;
@GetMapping("callback")
public String callback(String code, String state, HttpSession session) {
// Get code
// Hold code Request wechat fixed address , obtain access_token and openid
// Authorized temporary notes 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);
// obtain accessToken and 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");
// Add the scanner information to the database
// Judge whether there is duplicate wechat information in the database
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");
// add to
member=new UcenterMember();
member.setNickname(nickname);
member.setAvatar(headimgurl);
member.setOpenid(openid);
memberService.save(member);
}
// Use jwt according to member Object to generate token character string
String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
return "redirect:http://localhost:3000?token="+jwtToken;
}catch (Exception e){
throw new GuliException(20001," Login failed ");
}
}
Front page method : significance : When the page is initialized , Determine whether there is in the path token Parameters , If there is , call wxLogin Method , hold token Put it in cookie in , Then call the method to get user information , according to token Get user information display .
created() {
// Get... In the path token Value
this.token=this.$route.query.token
if(this.token){
this.wxLogin()
}
},
methods: {
// Wechat login
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' })
})
}
}
The diagram is as follows :

In a nutshell : Take it first app_id,app_secret,redirect_url Go to request an interface of wechat , Wechat will return a QR code , After the user scans the code to confirm login , perform callback The callback method , Two values will be passed , One state, One code,, Then take code Request an interface provided by wechat , Get two values , One access_token For access credentials , One openid It is the unique identification of each wechat , Take these two values and request an interface of wechat , You can get the wechat avatar, nickname and other information of the code scanner .
Personal understanding is , Open a door with the key of wechat application , Get a map , Scan the map to get the key , Open a door with a key , Get another key , Open another door , To get useful information .
边栏推荐
- 242. 有效的字母异位词
- [小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
- 208. 实现 Trie (前缀树)
- Promql select time series
- 【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
- 在 C 中声明函数之前调用函数会发生什么?
- 报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
- SEM of C language_ Tvariable type
- Leetcode:829. Sum of continuous integers
- Database DDL (data definition language) knowledge points
猜你喜欢

C语言的sem_t变量类型

[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API

LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)

Future of NTF and trends in 2022

【TA-霜狼_may-《百人计划》】2.2 模型与材质空间

Pyramid Scene Parsing Network【PSPNet】论文阅读

Explain spark operation mode in detail (local+standalone+yarn)

IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?

pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
![[TA frost wolf \u may- hundred people plan] 1.3 secret of texture](/img/dc/ce2819258632cdcf2e130a4c05600e.png)
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture
随机推荐
LeetCode 128最长连续序列(哈希set)
Pathmeasure implements loading animation
[TA frost wolf \u may - "hundred people plan"] 2.1 color space
pytorch nn. AdaptiveAvgPool2d(1)
392. 判断子序列
Ouc2021 autumn - Software Engineering - end of term (recall version)
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
Leetcode:829. 连续整数求和
torch. histc
Error: plug ins declaring extensions or extension points must set the singleton directive to true
389. 找不同
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算
What happens when a function is called before it is declared in C?
Take you through a circuit board, from design to production (dry goods)
在 C 中声明函数之前调用函数会发生什么?
214. 最短回文串
171. excel table column No
72. 编辑距离
小程序容器技术与物联网IoT的结合点