微信小程序登录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后台接口示例代码

package com.xx.action;  
import java.util.Map;  
import org.springframework.http.HttpMethod;  
import org.springframework.http.HttpStatus;  
import org.springframework.http.ResponseEntity;  
import org.springframework.web.client.RestTemplate;  
  
import com.google.gson.Gson;  
import com.hp.bean.WeChatAppLoginReq;  
import com.hp.bean.WeChatSession;  
import com.opensymphony.xwork2.ActionSupport;  
  
public class WeChatLogin extends ActionSupport{  
  
    /** 
     *author 李俊标 
     *2018-4-19 
         */  
     private static final long serialVersionUID = 1L;  
      
     private static final String APPID = "wx9xxxxxxxxxxx9b4";    
     private static final String SECRET = "685742***************84xs859";    
         private String code;  
       
     public String getCode() {  
        return code;  
     }  
  
  
     public void setCode(String code) {  
        this.code = code;  
     }  
     //获取凭证校检接口  
     public String login()    
     {  
         //微信的接口  
         String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+  
                 "&secret="+SECRET+"&js_code="+ code +"&grant_type=authorization_code";  
         RestTemplate restTemplate = new RestTemplate();  
         //进行网络请求,访问url接口  
         ResponseEntity<String>  responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);    
         //根据返回值进行后续操作   
         if(responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK)    
            {  
                String sessionData = responseEntity.getBody();  
                Gson gson = new Gson();  
                //解析从微信服务器获得的openid和session_key;  
                WeChatSession weChatSession = gson.fromJson(sessionData,WeChatSession.class);  
                //获取用户的唯一标识  
                String openid = weChatSession.getOpenid();  
                //获取会话秘钥  
                String session_key = weChatSession.getSession_key();  
                //下面就可以写自己的业务代码了  
                //最后要返回一个自定义的登录态,用来做后续数据传输的验证  
            }  
           
         return null;   
          
     }  
}  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值