微信授权登陆-app

news/2024/11/8 23:37:51/

app接入第三方微信登陆功能:

移动应用微信登录是基于OAuth2.0协议标准 构建的微信OAuth2.0授权登录系统,前提你需要到微信开放平台注册开发者帐号,并拥有一个已审核通过的移动应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程。

接入微信登陆授权步骤:

1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;

2. 通过code参数加上AppID和AppSecret等,通过API换取access_token;

3. 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。

注意:同一个微信开放平台下绑定的用户,在不同的应用下用户的UnionID就是相同的,openId是不相同的。

获取access_token流程:

第一步:请求CODE:

需要app调起微信


第二步:通过code获取access_token:

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

参数:

appid应用唯一标识,在微信开放平台提交应用审核通过后获得
secret应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
code填写第一步获取的code参数
grant_type填authorization_code

 appid与secret需要通过注册获取。

第三步:通过access_token调用接口

获取access_token后,进行接口调用,有以下前提:

  • access_token有效且未超时;
  • 微信用户已授权给第三方应用帐号相应接口作用域(scope)。

代码:

请求微信工具类:

package com.maobc.util;import com.alibaba.fastjson.JSONObject;
import com.maobc.entity.jar.Member;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.poi.ss.formula.functions.T;
import springfox.documentation.spring.web.json.Json;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;/*** @program: maobc-small_routine* @description: app用户登陆* @author: z.hw**/
public class WeixinLoginUtils {/*** 微信登陆通过code获取accessToken* @param appId* @param userAppSecret* @param code* @return* @throws Exception*/public StringBuilder getAccessTokenBycode(String appId,String userAppSecret,String code) throws Exception{//查看官方文档 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317853&token=&lang=String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+userAppSecret+"&code="+code+"&grant_type=authorization_code";URI uri = URI.create(url);HttpClient client = HttpClients.createDefault();HttpGet get = new HttpGet(uri);HttpResponse response=client.execute(get);StringBuilder sb = new StringBuilder();if (response.getStatusLine().getStatusCode() == 200) {HttpEntity entity = response.getEntity();BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));for (String temp = reader.readLine(); temp != null; temp = reader.readLine()) {sb.append(temp);}}return sb;}/*** access_token是否有效的验证* @param accessToken* @param openID* @return*/public boolean isAccessTokenIsInvalid(String accessToken,String openID) throws Exception{String url = "https://api.weixin.qq.com/sns/auth?access_token=" + accessToken + "&openid=" + openID;URI uri = URI.create(url);HttpClient client = HttpClients.createDefault();HttpGet get = new HttpGet(uri);HttpResponse response = client.execute(get);if (response.getStatusLine().getStatusCode() == 200) {HttpEntity entity = response.getEntity();BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));StringBuilder sb = new StringBuilder();for (String temp = reader.readLine(); temp != null; temp = reader.readLine()) {sb.append(temp);}JSONObject object = JSONObject.parseObject(sb.toString().trim());int errcode = object.getInteger("errcode");if (errcode == 0) {//未失效return true;}}return false;}/*** access_token       接口调用凭证* expires_in        access_token接口调用凭证超时时间,单位(秒)* refresh_token     用户刷新access_token* openid           授权用户唯一标识* scope          用户授权的作用域,使用逗号(,)分隔* @param APP_ID*/public JSONObject refreshAccessToken(String APP_ID,String refreshToken) throws Exception{/*** access_token是调用授权关系接口的调用凭证,由于access_token有效期(目前为2个小时)较短,当access_token超时后,可以使用refresh_token进行刷新,access_token刷新结果有两种:** 1.若access_token已超时,那么进行refresh_token会获取一个新的access_token,新的超时时间;** 2.若access_token未超时,那么进行refresh_token不会改变access_token,但超时时间会刷新,相当于续期access_token。** refresh_token拥有较长的有效期(30天)且无法续期,当refresh_token失效的后,需要用户重新授权后才可以继续获取用户头像昵称。*/String uri = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + APP_ID + "&grant_type=refresh_token&refresh_token=" + refreshToken;HttpClient client = HttpClients.createDefault();HttpGet get = new HttpGet(URI.create(uri));HttpResponse response = client.execute(get);JSONObject object =new JSONObject();if (response.getStatusLine().getStatusCode() == 200) {BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));StringBuilder builder = new StringBuilder();for (String temp = reader.readLine(); temp != null; temp = reader.readLine()) {builder.append(temp);}object = JSONObject.parseObject(builder.toString().trim());}return object;}/*** 得到用户基本信息* @param accessToken* @param openId* @param tClass* @return* @throws Exception*/public <T> T    getAppWeiXinUserInfo(String accessToken, String openId, Class<T> tClass) throws Exception{String uri = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId;HttpClient client = HttpClients.createDefault();HttpGet get = new HttpGet(URI.create(uri));HttpResponse response = client.execute(get);if (response.getStatusLine().getStatusCode() == 200) {BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));StringBuilder builder = new StringBuilder();for (String temp = reader.readLine(); temp != null; temp = reader.readLine()) {System.out.println(temp);builder.append(temp);}return JSONObject.parseObject(builder.toString(), tClass);}return null;}}

controller: 

   /*** @Description:  app微信登陆* @Author: z.hw*/@RequestMapping(value = {"getUserInfoByAppCode"})@ApiOperation(value = "getUserInfoByAppCode", notes = "不分页", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)@ApiResponses(value = {@ApiResponse(code = 404, message = "Not Found"),@ApiResponse(code = 400, message = "No Name Provided"),})publicApiResult getUserInfoByApp(@Validated UserAppAuthority userAppAuthority){return memberService.getUserInfoByApp(userAppAuthority);}

请求体:

/*** @program: maobc-small_routine* @description: app微信授权* @author: z.hw**/
@Data
public class UserAppAuthority {//新旧app 用来备份 自定义数据@NotNull(message = "类型不能为空")@NotEmpty(message = "类型不能为空")private String type;//重点需要 app调起微信获取到的code@NotEmpty(message = "code不能为空")@NotNull(message = "code不能为空")private String code;}
ApiResult:
@Data
@ToString(callSuper = true)
@EqualsAndHashCode
public class ApiResult<T> implements Serializable {/*** 状态码* 0表示成功*/private String  code = "0000";/*** 状态信息*/private String msg = "调用成功";private Object result;。。。。。。。。。。。。。}

 业务层:

/*** @Description: 用户app微信登陆* @Param:* @return:* @Author: z.hw*/public ApiResult getUserInfoByApp(UserAppAuthority userAppAuthority) {try {Member member = new Member();BeanUtils.copyProperties(userAppAuthority, member);//调用微信授权WeixinLoginUtils weixinLoginUtils = new WeixinLoginUtils();StringBuilder stringBuilder = weixinLoginUtils.getAccessTokenBycode(commConfig.userAppID, commConfig.userAppSecret, userAppAuthority.getCode());if (stringBuilder != null) {if (stringBuilder.toString().trim().contains("errcode")) {return ApiResult.Fail();}JSONObject object = JSONObject.parseObject(stringBuilder.toString().trim());String accessToken = object.getString("access_token"); //接口调用凭证String openID = object.getString("openid"); //授权用户唯一标识//获取微信用户基本信息WeiXinParam appWeiXinUserInfo = weixinLoginUtils.getAppWeiXinUserInfo(accessToken, openID, WeiXinParam.class);// TODO 业务逻辑。。。。。。。。。。。。return ApiResult.build(member);}} catch (Exception e) {e.printStackTrace();}return MaobcApiResult.Fail();}
Member:

  


import lombok.*;import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;/*** @author*/
@Entity
@Table(name = "cat_member")
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Member implements Serializable {private static final long serialVersionUID = -27353316177184L;@Id@GeneratedValue(strategy = GenerationType.AUTO)@Column(name = "id")private String id;/*** 账号状态:0 正常;1 冻结*/@Column(name = "status")private String status;/*** del_flag*/@Column(name = "del_flag")private String delFlag;/*** 手机*/@Column(name = "mobile_phone")private String mobilePhone;/*** 用户名*/@Column(name = "user_name")private String userName;/*** 邮箱*/@Column(name = "email")private String email;/*** 用户密码*/@Column(name = "password")private String password;/*** 昵称*/@Column(name = "nickname")private String nickname;/*** 性别*/@Column(name = "sex")private String sex;/*** 头像url*/@Column(name = "headimgurl")private String headimgurl;/*** 城市*/@Column(name = "city")private String city;/*** 国家*/@Column(name = "country")private String country;/*** 省份*/@Column(name = "province")private String province;/*** 语言*/@Column(name = "language")private String language;/*** 备注*/@Column(name = "remark")private String remark;/*** 生日*/@Column(name = "birthday")private String birthday;/*** 地址*/@Column(name = "address")private String address;/*** 会员积分*/@Column(name = "accumulate_points")private BigDecimal accumulatePoints;/*** 平台会员等级*/@Column(name = "level")private Integer level;/*** cat_membership会员类型表的ID*/@Column(name = "membership_id")private String membershipId;/*** 创建人*/@Column(name = "create_by")private String createBy;/*** 创建时间*/@Column(name = "create_date")private Date createDate;/*** 修改人*/@Column(name = "update_by")private String updateBy;/*** 修改时间*/@Column(name = "update_date")private Date updateDate;/*** 会员是否同意条款标识(0-未同意 1-同意)*/@Column(name = "item_status")private Integer itemStatus;private String openid;private String unionId;private String type;//是否绑定手机号码private String isBindedPhone;private String oauthId;private String memberId;private String outhId;}

WeiXinParam : 


import lombok.Data;@Data
public class WeiXinParam {String openid = "";String unionId = "";String sex = "1";String nickname = "";String city = "";String province = "";String country = "";String avatarUrl = "";String headimgurl="";}

参考资料:

微信开发平台:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN 

 


http://www.ppmy.cn/news/806658.html

相关文章

微信授权

这里写自定义目录标题 项目场景&#xff1a;微信授权问题描述&#xff1a;根据code值获取手机号 项目场景&#xff1a;微信授权 问题描述&#xff1a;根据code值获取手机号 步骤一&#xff1a; String result “”; WxMaJscode2SessionResult session null; try { session …

微信公众平台如何授权第三方平台,干货到!微信公众号怎样添加第三方平台及取消授权

微信公众号是我们经常使用的行业资讯平台&#xff0c;通过公众号我们可以更有效的传播我们的服务信息、提供更改的服务水平、效率。有时候我们需要授权第三方平台来进行操作会更加方便&#xff0c;那么如何在微信公众平台授权第三方平台呢&#xff1f;下面小编为大家分享一个方…

微信授权 登录

文章目录 一、微信公众号授权登录1.跳转微信授权页面2.授权成功获取code去获取用户信息&#xff08;openId等信息&#xff09;3.签名 二、微信小程序授权登录1.先登录&#xff0c;在授权2.获取用户信息 总结 一、微信公众号授权登录 1.跳转微信授权页面 toWXAuth(){var uri …

微信授权登陆步骤

伙伴们一起做了Javaweb小项目&#xff0c;微信登录授权模块是我负责&#xff0c;给大家普及后顺便发到这里&#xff0c;如果有不足的地方欢迎提出&#xff01; 进入项目任意页面 进入项目任意页面&#xff0c;后台首先会判断该用户是否登录。 若没有登录&#xff08;即sessi…

如何取消html5微信页面授权,微信授权管理在哪里?查看微信授权过的第三方应用并取消授权的方法图解...

近日微信迎来了6.5.16版本的更新&#xff0c;主要修复了已知问题&#xff0c;但其实小编发现在这个新版本当中。微信低调新增了一个授权管理功能。对于之前使用微信账户登录或进行有关授权其他应用的用户来说&#xff0c;这个时候就可以取消授权了&#xff0c;在一定程度上防止…

微信小程序登录授权流程

文章目录 小程序授权登陆流程1、当用户进入微信小程序时&#xff0c;首先我们先判断用户是否授权过此小程序2、如果没有授权&#xff0c;我们通过一个按钮来实现授权登录3、通过bindgetuserinfo事件&#xff0c;我们可以获取到个人的信息、加密偏移数据、加密用户信息&#xff…

如何在微信中取消已授权的第三方应用APP

由于需要测试微信登录功能&#xff0c;在微信授权登录成功后需要解除绑定功能。一直没有找到对应的方法。偶然间在 “发现” > "游戏"里看见了最近在玩的列表中居然有相关的APP&#xff0c;然后在菜单中“游戏管理”取消游戏关联"即可。但是APP和游戏没有任何…

JavaFX学习:MVC模式中的PropertyValueFactory

PropertyValueFactory类是“TableColumn cell value factory”,绑定创建列表中的项。示例如下&#xff1a; TableColumn<Person,String> firstNameCol new TableColumn<Person,String>("First Name");firstNameCol.setCellValueFactory(new PropertyVal…