微信公众号网页授权登录

公司开发小程序需进行微信公众号网页授权,作者分享实现过程。调用接口需公众号认证,先设置网页授权域名,创建Spring Boot工程,引入相关maven包,修改配置文件,封装HttpClient类,实现微信授权登录Controller,最后用外网地址在微信访问。

微信公众号网页授权登录
最近公司需要做个小程序的功能,一开始就涉及到微信公众号的网页授权,我在百度上搜索了蛮久,但是一直看不到想要的,有些写的太复杂,有些写的不清不楚,我这边来写一下我的例子。
调用接口需要微信公众号认证后才可!!!!
第一步:在开发->接口权限->网页服务->网页授权->网页授权获取用户基本信息
在这里插入图片描述点击网页授权域名的设置。
在这里插入图片描述此处按提示将文件放入项目中,我的是springboot项目,将文件放置在这里插入图片描述
即可。域名不要带http/https。
设置成功后,看下面即可
1 创建一个Spring boot工程
引入相关maven包

org.apache.httpcomponents httpclient 4.5.5 com.alibaba fastjson 1.2.47

修改application.yml文件
server:
port: 80

oauth:
wx:
appid: wxeadf53935631ac92
appsecret: f01fd7238bec6ae603be799a08d06304

callback:
http: http://java-j2ee.eicp.net/wxcallback

封装一个HttpClient的类
package com.spring.wxoauth.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientUtils {

/**
 * 发起GET请求
 * @param url
 * @return
 * @throws IOException
 */
public static JSONObject doGet(String url) throws IOException {
    JSONObject jsonObject = null;
    CloseableHttpClient client = HttpClients.createDefault();

    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = client.execute(httpGet);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
        String result = EntityUtils.toString(entity, "UTF-8");
        jsonObject = JSONObject.parseObject(result);
    }

    httpGet.releaseConnection();
    return jsonObject;
}

}

开始实现微信授权登录Controller
package com.spring.wxoauth.controller;

import com.alibaba.fastjson.JSONObject;
import com.spring.wxoauth.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.net.URLEncoder;

@Controller
public class LoginController {

@Value("${oauth.wx.appid}")
private String appid;

@Value("${oauth.wx.appsecret}")
private String appsecret;

@Value("${oauth.callback.http}")
private String http;

@GetMapping("/index")
public String index() {
    return "/index";
}

@GetMapping("/wxlogin")
public String wxlogin() {
    String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
            "&redirect_uri=" + URLEncoder.encode(http) +
            "&response_type=code" +
            "&scope=snsapi_userinfo" +
            "&state=STATE#wechat_redirect";
    return "redirect:" + url;
}

//@ResponseBody
@GetMapping("/wxcallback")
public String wxcallback(String code,       ModelMap map    ) throws IOException {
    String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid +
            "&secret=" + appsecret +
            "&code=" + code +
            "&grant_type=authorization_code";
    JSONObject jsonObject = HttpClientUtils.doGet(url);
    System.out.println(jsonObject.toJSONString());

    String openid = jsonObject.getString("openid");
    String accessToken = jsonObject.getString("access_token");


    String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken +
            "&openid=" + openid +
            "&lang=zh_CN";
    JSONObject userInfoJson = HttpClientUtils.doGet(userInfoUrl);
    System.out.println(userInfoJson);

    //return "SUCCESS";

    // 1种情况, 使用微信帐号做为本系统帐号
    map.put("userinfo", userInfoJson);
    return "/home";
}

}

弄好后,将外网地址给自己的微信上访问下就可以啦!!!在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值