目录
2、手动实现微信授权(伪代码实现,记住流程与重要得参数,回调函数等)
2.6 通过openid与access_token获得用户信息
一、微信授权流程介绍
1、oAuth2协议介绍
步骤:
1.用户需要使用第三方应用的功能,微信会跳转到供应商
2.供应商将用户导向认证服务器
3.认证服务器得到用户的授权
4.用户访问第三方应用(就是一个url),并携带授权码
5.第三方携带授权码去认证服务器申请令牌
6.验证是自己发送的授权码,然后发放令牌
7.第三方拿到令牌后去,微信资源服务器申请授权的资源
8.资源服务器开放资源给第三方
2、手动实现微信授权(伪代码实现,记住流程与重要得参数,回调函数等)
备注:微信授权需要企业公众号。
2.1微信公众号授权域名设置
2.2购买域名映射到本机
先去下载域名使用的客户端: https://natapp.cn/ 域名购买也在上面购买
购买成功
复制自己的token
在自己的客户端去启动(打开刚才安装的natapp.exe文件) 输入 natapp -authtoken=你自己复制的的token 启动
启动项目 通过域名访问你项目的接口: (你在远方的朋友也是可以访问的)如果能访问到就表示ok了
2.3 设置微信公众号域名
再次启动项目
2.4 获取用户授权码
1、通过上面oauth2协议的学习我们知道需要先获得用户的授权码,进入微信官网:微信官网
2、修改地址
3、增加回调接口
package com.xmcc.wx_sell.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("weixin")
@Slf4j
public class WeiXinController {
@RequestMapping("getCode")
public void getCode(@RequestParam("code") String code){
log.info("进入getCode回调方法");
log.info("获得当前微信用户的授权码为:{}",code);
}
}
4、复制刚才的url 发送到手机端,使用微信访问,可以看到控制台输出了
获取授权码:(红色部分对照“”2、修改地址“”部分修改)
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcec0b9e65c084712&redirect_uri=http://xmccjyqs.natapp1.cc/sell/weixin/getCode&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
2.5 通过code换取授权令牌
1、同样在官网获得地址
点击重置:
package com.xmcc.wx_sell.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("weixin")
@Slf4j
public class WeiXinController {
@RequestMapping("getCode")
public void getCode(@RequestParam("code") String code){
log.info("进入getCode回调方法");
log.info("获得当前微信用户的授权码为:{}",code);
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxcec0b9e65c084712&secret=05a7e861c1985ced86af77fb8f7163bc&code="+code+"&grant_type=authorization_code";
RestTemplate restTemplate = new RestTemplate();
String forObject = restTemplate.getForObject(url, String.class);
System.out.println(forObject);
}
}
启动项目再次通过微信访问刚才的地址:
2.6 通过openid与access_token获得用户信息
#在官网查看获取用户的信息的url:
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
根据刚才的返回值修改后(每个人的都不同请自己修改) 网页访问该地址
https://api.weixin.qq.com/sns/userinfo?
access_token=19_UBhDF84jk93tBgAS1aPt1m-nUvgQUrrATcm6k2rvOOGCVi1djYUyV0C3Udw25mwdhD3yB0eAIptKBVbeauq8yfifIEPbMtDwH32Q9Kr7vZ0
&openid=oXDaO1YlBtztKWHjKcNHGmWDub3Q&lang=zh_CN
访问结果:
nikeName:就是你的微信名。
到此,整个 微信授权完成。但我们在实际开发中都是使用别人的SDK,所以,下面将介绍使用SDK完成微信授权
二、使用SDK完成微信授权
1、导入jar包
<!--微信授权SDK--> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.3.0</version> </dependency>
2、分析SDK文档和自己的API文档
API文档分析:
3. 完成代码开发
3.1 配置文件
3.2 根据SDK文档配置需要的类
package com.xmcc.wx_sell.config;
import com.xmcc.wx_sell.properties.WeixinProperties;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WechatConfig {
@Autowired
private WeixinProperties weixinProperties;
@Bean//文档中需要用到这个对象
public WxMpService wxMpService(){
WxMpServiceImpl wxMpService = new WxMpServiceImpl();
//设置微信配置的存储
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage(){
WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
//设置appid 这个在项目中肯定是通过配置来实现
wxMpInMemoryConfigStorage.setAppId(weixinProperties.getAppid());
//设置密码
wxMpInMemoryConfigStorage.setSecret(weixinProperties.getSecret());
return wxMpInMemoryConfigStorage;
}
}
3.3 编写controller
package com.xmcc.wx_sell.controller;
import com.xmcc.wx_sell.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
@Controller
@RequestMapping("wechat")
@Slf4j
public class WechatController {
@Autowired//注入配置的对象
private WxMpService wxMpService;
//根据API接口文档 书写路径
@RequestMapping("authorize")
//查看文档需要一个returnUrl参数
public String authorize(@RequestParam("returnUrl") String returnUrl) throws UnsupportedEncodingException {
//自己编写获得openid的路径 在下面定义方法getUserInfo
String url = "http://xmcc.natapp1.cc/sell/wechat/getUserInfo";
//根据sdk文档获得路径 点击方法下载文档 很清晰的解释
/**
* 第一个参数是获得授权码code后回调的地址
* 第二个是策略:获得简单的授权,还是希望获得用户的信息
* 第三个参数是我们希望携带的参数:查看API文档需要返回returnUrl 所以我们就携带它
*/
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl,"UTF-8"));
return "redirect:"+redirectUrl;
}
@RequestMapping("getUserInfo")
/**
* code:是授权码
* returnUrl:是刚才我们自己传递的参数 会传递到微信然后传回来
*/
public String getUserInfo(@RequestParam("code")String code,
@RequestParam("state") String returnUrl) throws UnsupportedEncodingException {
WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
try {
//根据sdk文档 通过code获得令牌与openid
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
} catch (WxErrorException e) {
log.error("微信获得access_token异常:{}",e.getError().getErrorMsg());
throw new CustomException(e.getError().getErrorCode(),e.getError().getErrorMsg());
}
try {
//获得用户信息 ,授权其实用不到的 这儿打出来看看
WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
log.info("获得用户信息:{}",wxMpUser.getNickname());
} catch (WxErrorException e) {
e.printStackTrace();
}
//获得openid
String openId = wxMpOAuth2AccessToken.getOpenId();
return "redirect:"+ URLDecoder.decode(returnUrl,"UTF-8")+"?openid="+openId;
}
//测试是否获得openid
@RequestMapping("testOpenid")
public void testOpenid(@RequestParam("openid")String openid){
log.info("获得用户的openid为:{}",openid);
}
}
3.4 测试
然后重复之前的在微信客户端登录:
例如我的地址是:
http://xmccjyqs.natapp1.cc/sell/wechat/authorize?returnUrl=http://xmccjyqs.natapp1.cc/sell/wechat/testOpenid
看到如下效果:
到此,整个微信授权结束,如果对大家有所帮助,那就点个赞,留个言。感谢!