java 微信登录

我们想要引一个maven用来处理JSON数据 ,当然你也可以选择其他的json包来替代

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.23</version>
        </dependency>

在创建一个请求微信接口的工具类

    /**
     * 获取连接的json对象
     *
     * @param url 路径
     */
    public static JSONObject loadJson(String url) {
        StringBuffer json = new StringBuffer();
        try {
            URL urlObject = new URL(url);
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jSONObject = new JSONObject();
        jSONObject = JSON.parseObject(json.toString());
        return jSONObject;
    }

在创建一个微信的配置类

package com.longlongUtil.props;

/**
 * @author 29027
 * 第三方登录参数
 */
public class ThirdPartyLoginsProps {
    /**微信登录appid;*/
    public final static String WX_APPID = "";
    /**微信登录scope*/
    public final static String WX_SCOPE = "";
    /**微信登录http*/
    public final static String WX_HTTP = "";
}
package com.longlongUtil.util;

import com.alibaba.fastjson.JSONObject;
import com.longlongUtil.props.ThirdPartyLoginsProps;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 29027
 * <p>
 * 第三方登录工具类
 */
public class ThirdPartyLoginsUtil {
    /**
     * 微信登录
     */
    public static void wxLogin(HttpServletRequest request, HttpServletResponse response, String tenantId) throws IOException, IOException {
        String appId = ThirdPartyLoginsProps.WX_APPID;
        String scope = ThirdPartyLoginsProps.WX_SCOPE;
        String http = ThirdPartyLoginsProps.WX_HTTP;
        // 第一步:用户同意授权,获取code
        /*APPID&secret=SECRET&code=CODE&grant_type=authorization_code*/
        String url = "https://open.weixin.qq.com/connect/qrconnect?"
                + "appid=" + appId + "&scope=" + scope + "&redirect_uri=" + http;
        response.sendRedirect(url);
    }

    /**
     * 微信登录后回调
     */
    public JSONObject wxcallback(String code) throws IOException {
        String appid = ThirdPartyLoginsProps.WX_APPID;
        String appSecret = ThirdPartyLoginsProps.WX_SCOPE;

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid +
                "&secret=" + appSecret +
                "&code=" + code +
                "&grant_type=authorization_code";
        JSONObject token = HttpUtils.loadJson(url);
        System.out.println(token);

        //获取用户的openid和access_token
        String access_token = token.get("access_token").toString();
        String openid = token.get("openid").toString();
        return this.userWx(access_token, openid);
    }
    /**
     * 获取微信用户信息
     * */
    /**
     * 获取微信的用户信息
     */
    @GetMapping("/userWx")
    public JSONObject userWx(String access_token, String openid) throws IOException {
        String url = "https://api.weixin.qq.com/sns/userinfo?" +
                "access_token=" + access_token +
                "&openid=" + openid;
        JSONObject token = HttpUtils.loadJson(url);
        return token;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值