失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微信小程序登录Java后台接口

微信小程序登录Java后台接口

时间:2023-11-01 15:57:13

相关推荐

微信小程序登录Java后台接口

微信小程序登录java后台接口

首先看一下微信小程序的开发文档:

微信小程序开发文档

步骤:

小程序端向微信接口服务发送请求——wx.login();获取到登录临时凭证code小程序端拿着获取到的code向后台(这里是java服务端),使用wx.request()向自己的服务器发送请求(接口服务器自己定义)后台服务器拿着小程序端传过来的code,以及自己的APPID,secretKey向微信方发送HttpGet请求后台服务器获取到微信方返回回来的openId,session_key,然后加上自己本地的登录状态(本地自己定义),发送给小程序端小程序端接收到后台传输过来的登录状态,保存到storage中以供以后使用。

具体实现步骤

小程序端

随便弄一个按钮,绑定下面的方法

//与后端通信bindtest: function (){wx.login({success:res=>{let _code=res.code;//获取到code之后再发送给后端wx.request({url: 'http://localhost:8080/login',data:{code:_code,},method:'POST',header: {'content-type': 'application/x-www-form-urlencoded' // 默认是json},success: function (res) {console.log(res.data);},fail: function (res) {console.log(".....fail.....");}})}})}

java服务器端

package com.kylin.wxtest.controller;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.kylin.wxtest.bean.Login;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;@RestControllerpublic class UserController {private static final long serialVersionUID=1L;private static final String APPID = "wxb88xxxxxxxx46140e";private static final String SECRET = "19fa40c6xxxxxxxx6ae971267";private String code;public String getCode() {return code;}public void setCode(String code) {this.code = code;}@RequestMapping(value = "/login")public String login(String code){System.out.println(code);System.out.println("------------------------------------");//微信那边的接口,grant_type=authorization_code是固定的String url="https://api./sns/jscode2session?appid="+APPID+"&secret="+SECRET+"&js_code="+ code +"&grant_type=authorization_code"; //发送请求给微信后端CloseableHttpClient httpClient= HttpClients.createDefault();HttpGet httpGet=new HttpGet(url);InputStream inputStream=null;CloseableHttpResponse httpResponse=null;StringBuilder result=new StringBuilder();try {httpResponse=httpClient.execute(httpGet);HttpEntity entity=httpResponse.getEntity();inputStream=entity.getContent();BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));String line="";while ((line=bufferedReader.readLine())!=null){System.out.println(line); //这里需要使用fastjson来提取一下内容JSONObject jsonObject= JSON.parseObject(line);Login login=new Login();login.setOpenid(jsonObject.getString("openid"));login.setSession_key(jsonObject.getString("session_key"));result.append(login.getOpenid()+"hello_world"+login.getSession_key());System.out.println(result.toString());}} catch (IOException e) {e.printStackTrace();}return result.toString();}}

总结

这样小程序端就可以获取到openId以及session_key;相当于授权成功!

如果觉得《微信小程序登录Java后台接口》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。