springboot整合微信网页授权登陆

微信网页授权步骤:


1 第一步:用户同意授权,获取code

2 第二步:通过code换取网页授权access_token

3 第三步:刷新access_token(如果需要)

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

5 附:检验授权凭证(access_token)是否有效​

一.第一步:用户同意授权,获取code

scope参数中的snsapi_base(静默授权,用户无感知)和snsapi_userinfo(非静默授权,提示框需要用户确认授权)


https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect​

如果是前后端分离,该链接后端拼接好返回给前端,有前端进行访问,redirect_uri对应的回调地址,是前端服务器的地址,并且微信公众号回调地址不带http://,本地调试需要开内网穿透(花生壳),不然微信回调请求不到,链接上带的域名和微信公众号配置的回调地址域名保持一致。

二.前端请求上面的地址后获取code,然后请求后端callBack接口进行授权

@Transactional
@Override
public Map<String, Object> callBack(string code) {
    String outh2_url = WechatConstants.OUTH2_URL + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
    Map<String, Object> params = new HashMap<>();
    //发送请求 get提交 拿code凭证去获取openid和access_token
    String s = HttpClientUtils.doGet(outh2_url, params);
    if (StringUtils.isEmpty(s) || s.contains("errcode")) {
        log.info(s);
        throw new ServiceException(ExceptionEnum.AUTHORIZATION_FAILED);
    }
    JSONObject jsonObject = JSONObject.parseObject(s);
    log.info("发送请求 get提交 拿code凭证去获取openid和access_token:{}", jsonObject.toJSONString());
    String openid = jsonObject.getString("openid");
    String access_token = jsonObject.getString("access_token");
    //获取用户接口
    String userinfo_url = WechatConstants.USERINFO_URL + access_token + "&openid=" + openid + "&lang=zh_CN";
    String result = HttpClientUtils.doGet(userinfo_url, params);
    if (StringUtils.isEmpty(result) || result.contains("errcode")) {
        log.info(result);
        throw new ServiceException(ExceptionEnum.WEI_XIN_USER_INFO);
    }
    JSONObject userInfo = JSONObject.parseObject(result);
    log.info("获取用户接口:{}", userInfo.toJSONString());
    //通过openId和companyCode查询数据库是否有过授权
    WeiXinAccount weiXinAccount = weiXinAccountMapper.selectOneByCondition(new WeiXinAccount().setCompanyCode(req.getCompanyCode()).setOpenId(openid));
    //保存用户微信相关信息(头像,微信id,微信名等)
    Date date = new Date();
    WeiXinAccount weiXinUserInfo = new WeiXinAccount()
            .setCity(userInfo.getString("city"))
            .setNickName(userInfo.getString("nickname"))
            .setHeadImageUrl(userInfo.getString("headimgurl"))
            .setSex(userInfo.getInteger("sex"))
            .setUpdateTime(date);
    if (null == weiXinAccount) {
        weiXinUserInfo.setCompanyCode(req.getCompanyCode()).setOpenId(openid).setCreateTime(date);
        weiXinAccountMapper.insert(weiXinUserInfo);
    } else {
        weiXinUserInfo.setId(weiXinAccount.getId());
        weiXinAccountMapper.updateByPrimaryKeySelective(weiXinUserInfo);
    }
    Map<String, Object> resultMap = new HashMap<>(2);
    resultMap.put("openId", openid);
    //标记是否已经绑定已有的账号
    resultMap.put("binding", Boolean.FALSE);
    if (null != weiXinAccount && null != weiXinAccount.getCustomerId()) {
        resultMap.put("binding", Boolean.TRUE);
    }
    return resultMap;
}

静态参数类

public class WechatConstants {

    public static final String CONNECT_OUTH2_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=";

    public static final String OUTH2_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=";

    public static final String USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=";

    public static final String OAUTH2_SCOPE_USER_INFO = "snsapi_userinfo";
}

版权声明:本文为qq_38377774原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。