微信第三方登录,新手必看

微信第三方登录,新手必看

微信第三方步骤:

  1. 先去微信第三方登录注册应用,必须要公司注册,也可以去淘宝上买在这里插入图片描述

准备:在微信开发平台,创建网站应用 appId值,appsecret值和回调域名

在这里插入图片描述

  1. 回调域在这里插入图片描述

  2. **

获取Code值有两种方式

  • 展示二维码让用户扫码 直接使用a标签进行链接二维码(方便)

这里有三个值需要用户自填

1 appId(微信开发平台创建应用后提供)

2.redirect_uri(回调域名,必须是开发平台的创建应用的回调域名之下)**(提醒一下:微信默认不让加http或https跟端口号,会报redirect_uri错误的这里给不用加端口号,比如下面这种形式的

3.state(唯一凭证,随便填)

`https://open.weixin.qq.com/connect/qrconnect?appid=appid&redirect_uri=http://www.baidu.com/wxLogin&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect`

在这里插入图片描述

 `https://open.weixin.qq.com/connect/qrconnect?appid=appid&redirect_uri=回调域地址&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect`
  1. 跳转到自定义页面 ,让用户进行扫码(这里我就不讲了,自我认为还是微信提供链接扫码比较方便)

java中处理扫码登录的流程

**
1.加入配置类

package cn.xcn.demo.until;

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.apache.http.util.EntityUtils;

public class HttpClientUtil {

    public static String get(String uri){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet(uri);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode==200){
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity,"UTF-8");
                return result;
            }else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

2.导入依赖

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.7</version>
        </dependency>
        //阿里的json转换工具
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>
  1. 获取url链接
//获取token和openid请求路径,需更改其APPID和密钥,其他的代码中动态修改
    public String GETTOKEN = "" +
            "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=应用密钥&code=CODE&grant_type=authorization_code";

//用户信息请求路径
    public String USERINFO = "" +
            "https://api.weixin.qq.com/sns/userinfo?access_token=TOKEN&openid=OPENID"
  1. 最后处理请求的代码

第一种:

   public ModelAndView callBack(String code, String state){
        //回调获得code,通过用户授权的code去获取微信令牌
        String token = HttpClientUtil.get(GETTOKEN.replaceAll("CODE", code));
        Map map = JSON.parseObject(token);//这里也可以不用map处理token
        //获取到了关键的令牌和openid后,
        //就可以正式开始查询微信用户的信息,完成我们要做的微信绑定
        String access_token = (String) map.get("access_token");
        String openid = (String) map.get("openid");
        String userInfo = HttpClientUtil.get(USERINFO.replaceAll("TOKEN", access_token).replaceAll("OPENID", openid));
        Map info = JSON.parseObject(userInfo);
        //返回一个登录成功后的页面,携带微信用户信息
        System.out.println(userInfo);
        return new ModelAndView("weichat","userInfo",info);
    }

第二种:

 public Object callBack(HttpServletRequest request, HttpServletResponse response){
        // 微信官方发给我们一个临时凭证
        System.out.println("111");
        String code = request.getParameter("code");
        System.out.println(code);
        // 通过code,去微信官方申请一个正式的token令牌
        String getTokenByCodeUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=应用秘钥&code="+code+"&grant_type=authorization_code";
        String tokenString = HttpClientUtil.doGet(getTokenByCodeUrl);
        System.out.println(tokenString);
        // 将json格式的token字符串转换成实体对象,方便存和取
        Token token = JSON.parseObject(tokenString, Token.class);

        // 通过token。去微信官方获取用户的信息
        String getUserByTokenUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+token.getAccess_token()+"&openid="+token.getOpenid();

        String userInfoString = HttpClientUtil.doGet(getUserByTokenUrl);

        System.out.println(userInfoString);

        // 将json格式的user字符串转换成实体对象,方便存取
        WxUserInfo wxUserInfo = JSON.parseObject(userInfoString, WxUserInfo.class);//我们有对象的实体

        System.out.println("微信昵称:"+wxUserInfo.getNickname());

        System.out.println("微信头像:"+wxUserInfo.getHeadimgurl());

        // 上面代码只是获取到了微信用户信息 后需须添加自己项目登录的流程

        /**
         * 登录流程
         */

        return wxUserInfo;

微信实体类:

package com.xc.smallrookie.web.login.domain;

import lombok.Data;

/**
 * 令牌实体类
 */
@Data
public class Token {
    private String access_token;//	接口调用凭证
    private String expires_in;//	access_token接口调用凭证超时时间,单位(秒)
    private String refresh_token;//	用户刷新access_token
    private String openid;//	授权用户唯一标识
    private String scope;//	用户授权的作用域,使用逗号(,)分隔
    private String unionid;//	当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段。
 
    public Token() {
    }
 
    public String getAccess_token() {
        return access_token;
    }
 
    // get set
}

最后前端处理的话,就用Thymeleaf来处理返回的数据

如果你要是想得到用户的信息的话,要有外网进行访问

这里我建议大家可以下载个花生壳来进行外网访问,可以下面评论,或者私信我都可以的,我大概是参考这位大哥的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值