当前位置:   article > 正文

OAuth2.0第三方微信登录_微信auth2.0

微信auth2.0

目录

OAuth2.0第三方微信登录

为什么要第三方登录

提升用户体验

有利于应用发展

推广第三方平台

OAuth2.0是什么

OAuth2.0角色说明

OAuth2.0授权模式

测试公众号申请

用户同意授权,获取code

通过code换取网页授权access_token

拉取用户信息(需scope为 snsapi_userinfo)

开始测试


OAuth2.0第三方微信登录

为什么要第三方登录

提升用户体验

第三方登录简单便捷,对于不同平台存在不同用户名和密码的问题,第三方登录正好解决,几乎可以使用一个第三方账号搞定所有的登录问题。

第三方登录可以将用户在第三方应用中的个人信息同步到登录应用中,不需要再次填写个人资料信息。

第三方登录使用的个人信息都是公用的非敏感信息,例如头像和昵称。一些敏感信息,如手机号和邮箱,第三方应用是不会对外提供的。

有利于应用发展

更加便捷的应用登录注册方式,给用户提供满意的使用体验,在一定程度上保证了应用的用户量。

第三方平台登录的介入,让应用可以直接获取用户的头像和昵称等信息,降低了应用的开发成本。

目前市面上的短信验证码费用在0.05元每条左右,用户使用第三方平台登录,能有效降低应用登录的成本。

推广第三方平台

增加用户对第三方平台的依赖性,用户使用第三方平台登录应用的次数越多,代表着该平台对用户的粘性越高。

获得更广泛的影响力,只要用户使用提供第三方平台登录的应用,那么这个第三方平台就会被用户浏览,有利于平台拉取新用户和促进用户活跃度。

OAuth2.0是什么

OAuth(Open Authotization)是一个关于授权的开放网络标准,允许用户授权第三方应用访问存储在另外的服务提供者上的信息,而且不需要将用户名和密码提供给第三方应用。OAuth在全世界得到广泛应用,目前的版本是2.0版本。

协议特点:

简单:不论是OAuth服务的提供者还是应用的开发者,都是很容易理解和使用的。

安全:没有涉及到用户的敏感信息,安全性高。

开放:任何的服务提供商都可以实现OAuth,任何的软件开发商都可以使用OAuth。

OAuth2.0角色说明

客户端:本身不存储资源,需要通过资源拥有者授权去请求资源服务器的资源。

资源拥有者:通常为用户,也可以是应用程序,即资源的拥有者。

授权服务器:用于服务提供商对资源拥有者的身份进行认证、对访问的资源进行授权,认证成功后会给客户端发放令牌,作为客户端访问资源服务器的凭证。

资源服务器:存储资源的服务器,例如微信端存储的用户信息。

OAuth2.0授权模式

OAuth2.0有四种授权模式:授权码模式、隐式授权模式、用户名密码模式、客户端模式。

其中授权码模式是OAuth2.0目前最安全最复杂也是最常用的授权流程。

测试公众号申请

网址:

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

使用微信扫码登录,登录成功后微信会分配appID和appsecret。

其中的URL填后端服务接口的访问URL,Token可以随便填。

当然我们直接填http://localhost:8080/check是不行的,微信访问不到。 

需要用到内网穿透工具cploar

官网:

https://www.cpolar.com/

注册后,选择免费套餐,然后下载安装cploar。

在安装目录里找到webui并打开,用注册的账号登录。

隧道名称随便填,协议填http,本地地址填后端服务的端口,然后点击创建。

创建好后会给出对应的访问域名,使用这个域名微信才能成功访问到我们电脑上的后端服务。

微信官方的要求是:

意思就是微信会去访问我们的后端服务,访问地址就是以下的URL。请求会传上面的四个参数,其中关键的是echostr参数,我们必须原样返回。

这里就不进行校验了,直接返回echostr。

  1. import org.springframework.web.bind.annotation.GetMapping;
  2. import org.springframework.web.bind.annotation.RestController;
  3. @RestController
  4. public class WxController {
  5. @GetMapping("/check")
  6. public String check(String signature, String timestamp, String nonce, String echostr) {
  7. // 校验
  8. return echostr;
  9. }
  10. }

点击提交,提示配置成功就可以了。

测试号二维码,用微信扫一下关注就行了。

往下翻,找到网页服务 => 网页账号 => 修改。

填回调域名

以下案例会用到的依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.httpcomponents</groupId>
  7. <artifactId>httpclient</artifactId>
  8. <version>4.5.13</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>cn.hutool</groupId>
  12. <artifactId>hutool-all</artifactId>
  13. <version>5.8.23</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>com.google.zxing</groupId>
  17. <artifactId>core</artifactId>
  18. <version>3.3.3</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.alibaba</groupId>
  22. <artifactId>fastjson</artifactId>
  23. <version>1.2.83</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.project-lombok</groupId>
  27. <artifactId>lombok</artifactId>
  28. <optional>true</optional>
  29. </dependency>

用户同意授权,获取code

在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(已认证服务号,默认拥有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

用户在点击微信登录的时候,访问/wxLogin接口,展示登录二维码。

需要用到Hutool工具包来生成二维码

https://blog.csdn.net/qq_74312711/article/details/134885930?spm=1001.2014.3001.5502

  1. import cn.hutool.extra.qrcode.QrCodeUtil;
  2. import jakarta.servlet.http.HttpServletResponse;
  3. import lombok.RequiredArgsConstructor;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.io.IOException;
  7. import java.net.URLEncoder;
  8. import java.nio.charset.StandardCharsets;
  9. @RestController
  10. @RequiredArgsConstructor
  11. public class WxController {
  12. private final HttpServletResponse response;
  13. @GetMapping("/wxLogin")
  14. public void wxLogin() throws IOException {
  15. // 回调地址
  16. String redirectUrl = URLEncoder.encode("http://74d7e570.r15.cpolar.top/wxCallBack", StandardCharsets.UTF_8);
  17. // 二维码地址
  18. String appid = "填自己的appID";
  19. String url = "https://open.weixin.qq.com/connect/oauth2/authorize" +
  20. "?appid=" + appid +
  21. "&redirect_uri=" + redirectUrl +
  22. "&response_type=code" +
  23. "&scope=snsapi_userinfo" +
  24. "&state=STATE#wechat_redirect";
  25. // 生成二维码并返回
  26. response.setContentType("image/png");
  27. QrCodeUtil.generate(url, 300, 300, "png", response.getOutputStream());
  28. }
  29. @GetMapping("/check")
  30. public String check(String signature, String timestamp, String nonce, String echostr) {
  31. // 校验
  32. return echostr;
  33. }
  34. }

通过code换取网页授权access_token

首先请注意,这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。

尤其注意:由于公众号的secret和获取到的access_token安全级别都非常高,必须只保存在服务器,不允许传给客户端。后续刷新access_token、通过access_token获取用户信息等步骤,也必须从服务器发起。

请求方法

获取code后,请求以下链接获取access_token:

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

返回说明

正确时返回的JSON数据包如下:

  1. {
  2. "access_token":"ACCESS_TOKEN",
  3. "expires_in":7200,
  4. "refresh_token":"REFRESH_TOKEN",
  5. "openid":"OPENID",
  6. "scope":"SCOPE",
  7. "is_snapshotuser": 1,
  8. "unionid": "UNIONID"
  9. }

错误时微信会返回JSON数据包如下(示例为Code无效错误):

{"errcode":40029,"errmsg":"invalid code"}

封装TokenInfo实体类:

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @AllArgsConstructor
  5. @Data
  6. @NoArgsConstructor
  7. public class TokenInfo {
  8. private String accessToken;
  9. private Integer expiresIn;
  10. private String refreshToken;
  11. private String openid;
  12. private String scope;
  13. private Integer isSnapshotuser;
  14. private String unionid;
  15. }

拉取用户信息(需scope为 snsapi_userinfo)

如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。

请求方法

http:GET(请使用https协议):

https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

返回说明

正确时返回的JSON数据包如下:

  1. {
  2. "openid": "OPENID",
  3. "nickname": NICKNAME,
  4. "sex": 1,
  5. "province":"PROVINCE",
  6. "city":"CITY",
  7. "country":"COUNTRY",
  8. "headimgurl":"https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
  9. "privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
  10. "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
  11. }

错误时微信会返回JSON数据包如下(示例为openid无效):

{"errcode":40003,"errmsg":" invalid openid "}

封装WxUserInfo实体类:

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @AllArgsConstructor
  5. @Data
  6. @NoArgsConstructor
  7. public class WxUserInfo {
  8. private String openid;
  9. private String nickname;
  10. private Integer sex;
  11. private String province;
  12. private String city;
  13. private String country;
  14. private String headimgurl;
  15. private String privilege;
  16. private String unionid;
  17. }

回调接口/wxCallBack

  1. import cn.hutool.extra.qrcode.QrCodeUtil;
  2. import com.alibaba.fastjson.JSONObject;
  3. import jakarta.servlet.http.HttpServletResponse;
  4. import lombok.RequiredArgsConstructor;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.io.IOException;
  8. import java.net.URLEncoder;
  9. import java.nio.charset.StandardCharsets;
  10. @RestController
  11. @RequiredArgsConstructor
  12. public class WxController {
  13. private final HttpServletResponse response;
  14. @GetMapping("/wxCallBack")
  15. public String wxCallBack(String code) throws IOException {
  16. WxUserInfo user = GetUserInfoUtils.getUser(code);
  17. return JSONObject.toJSONString(user);
  18. }
  19. @GetMapping("/wxLogin")
  20. public void wxLogin() throws IOException {
  21. // 回调地址
  22. String redirectUrl = URLEncoder.encode("http://74d7e570.r15.cpolar.top/wxCallBack", StandardCharsets.UTF_8);
  23. // 二维码地址
  24. String appid = "填自己的appID";
  25. String url = "https://open.weixin.qq.com/connect/oauth2/authorize" +
  26. "?appid=" + appid +
  27. "&redirect_uri=" + redirectUrl +
  28. "&response_type=code" +
  29. "&scope=snsapi_userinfo" +
  30. "&state=STATE#wechat_redirect";
  31. // 生成二维码并返回
  32. response.setContentType("image/png");
  33. QrCodeUtil.generate(url, 300, 300, "png", response.getOutputStream());
  34. }
  35. @GetMapping("/check")
  36. public String check(String signature, String timestamp, String nonce, String echostr) {
  37. // 校验
  38. return echostr;
  39. }
  40. }

GetUserInfoUtils

  1. import com.alibaba.fastjson.JSON;
  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.client.methods.HttpGet;
  4. import org.apache.http.impl.client.CloseableHttpClient;
  5. import org.apache.http.impl.client.HttpClients;
  6. import org.apache.http.util.EntityUtils;
  7. import java.io.IOException;
  8. public class GetUserInfoUtils {
  9. public static WxUserInfo getUser(String code) throws IOException {
  10. // 构造HTTP请求
  11. CloseableHttpClient httpClient = HttpClients.createDefault();
  12. // 用code交换token
  13. String appid = "填自己的appID";
  14. String secret = "填自己的appsecret";
  15. String tokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" +
  16. "?appid=" + appid +
  17. "&secret=" + secret +
  18. "&code=" + code +
  19. "&grant_type=authorization_code";
  20. // 发起请求
  21. HttpGet httpGet = new HttpGet(tokenUrl);
  22. String responseResult = "";
  23. // 接收返回的数据,转成utf-8编码
  24. HttpResponse response = httpClient.execute(httpGet);
  25. if (response.getStatusLine().getStatusCode() == 200) {
  26. responseResult = EntityUtils.toString(response.getEntity(), "utf-8");
  27. }
  28. // 封装到TokenInfo
  29. TokenInfo tokenInfo = JSON.parseObject(responseResult, TokenInfo.class);
  30. // 用access_token获取用户信息
  31. String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
  32. "?access_token=" + tokenInfo.getAccessToken() +
  33. "&openid=" + tokenInfo.getOpenid() +
  34. "&lang=zh_CN";
  35. // 发起请求
  36. HttpGet httpGet1 = new HttpGet(userInfoUrl);
  37. // 接收返回的数据,转成utf-8编码
  38. HttpResponse response1 = httpClient.execute(httpGet1);
  39. if (response.getStatusLine().getStatusCode() == 200) {
  40. responseResult = EntityUtils.toString(response1.getEntity(), "utf-8");
  41. }
  42. // 封装到WxUserInfo
  43. WxUserInfo wxUserInfo = JSON.parseObject(responseResult, WxUserInfo.class);
  44. return wxUserInfo;
  45. }
  46. }

开始测试

到此就可以开始测试了

打开浏览器,访问/wxLogin,出现二维码。用微信扫码登录,返回用户的信息则说明测试成功。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/745885
推荐阅读
  

闽ICP备14008679号