微信小程序登录Java后台接口(详解,附示例代码)

首先看一下官方文档

地址:微信小程序官方文档API登录接口


我们先对官方给的时序图进行简单的分析

1.当小程序调用wx.login()时,会获得一个code(临时登录凭证),然后我们需要用wx.request()将code发送到自己的服务器.

2.在服务器的接口中,调用登录凭证校检接口,将appid(小程序唯一标识)+appsecret(小程序的app secret)+code发送到微信接口服务.然后微信服务器会返回session_key(会话秘钥)+openid(用户的唯一标识).

3.在服务器的接口中,已经得到微信用户的唯一标识openid,已经数据传输的session_key,接下来就写业务逻辑了.

4.返回给小程序自定义登录态,小程序将它存入storage中.接下来的wx.request()的业务请求,都会携带自定义登录态.

5.在服务器的接口中通过自定义登录态查询openid和session_key,然后返回业务数据.

划一下重点

在服务器的接口中,需要进行一个http请求,将从小程序获得的code和接口中存储的appid和secret发送给微信接口服务,然后就可以获得session_key和openid.

接口地址

https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
请求参数
参数必填说明
appid小程序唯一标识
secret小程序的 app secret
js_code登录时获取的 code
grant_type填写为 authorization_code
在不满足UnionID下发条件的情况下,返回参数
参数说明
openid用户唯一标识
session_key会话密钥
在满足UnionID下发条件的情况下,返回参数
参数说明
openid用户唯一标识
session_key会话密钥
unionid用户在开放平台的唯一标识符
返回说明
//正常返回的JSON数据包
{
      "openid": "OPENID",
      "session_key": "SESSIONKEY",
}

//满足UnionID返回条件时,返回的JSON数据包
{
    "openid": "OPENID",
    "session_key": "SESSIONKEY",
    "unionid": "UNIONID"
}
//错误时返回JSON数据包(示例为Code无效)
{
    "errcode": 40029,
    "errmsg": "invalid code"
}
小程序登录示例代码
//app.js
App({
  onLaunch: function() {
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://test.com/onLogin',
            data: {
              code: res.code
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      }
    });
  }
})
Java后台接口示例代码

[java]  view plain  copy
  1. package com.xx.action;  
  2. import java.util.Map;  
  3. import org.springframework.http.HttpMethod;  
  4. import org.springframework.http.HttpStatus;  
  5. import org.springframework.http.ResponseEntity;  
  6. import org.springframework.web.client.RestTemplate;  
  7.   
  8. import com.google.gson.Gson;  
  9. import com.hp.bean.WeChatAppLoginReq;  
  10. import com.hp.bean.WeChatSession;  
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.   
  13. public class WeChatLogin extends ActionSupport{  
  14.   
  15.     /** 
  16.      *author 李俊标 
  17.      *2018-4-19 
  18.          */  
  19.      private static final long serialVersionUID = 1L;  
  20.       
  21.      private static final String APPID = "wx9xxxxxxxxxxx9b4";    
  22.      private static final String SECRET = "685742***************84xs859";    
  23.          private String code;  
  24.        
  25.      public String getCode() {  
  26.         return code;  
  27.      }  
  28.   
  29.   
  30.      public void setCode(String code) {  
  31.         this.code = code;  
  32.      }  
  33.      //获取凭证校检接口  
  34.      public String login()    
  35.      {  
  36.          //微信的接口  
  37.          String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+  
  38.                  "&secret="+SECRET+"&js_code="+ code +"&grant_type=authorization_code";  
  39.          RestTemplate restTemplate = new RestTemplate();  
  40.          //进行网络请求,访问url接口  
  41.          ResponseEntity<String>  responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);    
  42.          //根据返回值进行后续操作   
  43.          if(responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK)    
  44.             {  
  45.                 String sessionData = responseEntity.getBody();  
  46.                 Gson gson = new Gson();  
  47.                 //解析从微信服务器获得的openid和session_key;  
  48.                 WeChatSession weChatSession = gson.fromJson(sessionData,WeChatSession.class);  
  49.                 //获取用户的唯一标识  
  50.                 String openid = weChatSession.getOpenid();  
  51.                 //获取会话秘钥  
  52.                 String session_key = weChatSession.getSession_key();  
  53.                 //下面就可以写自己的业务代码了  
  54.                 //最后要返回一个自定义的登录态,用来做后续数据传输的验证  
  55.             }  
  56.            
  57.          return null;   
  58.           
  59.      }  

  1. }  
  2. 原文转自:https://blog.csdn.net/weixin_40099554/article/details/80003058
  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值