#Java 微信公众号授权登录以及如何获取用户openId
一、微信公众号授权登录开发链接:
(微信提供公众号开发文档,先看一遍文档,比较容易理解)
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
二、微信授权登录,以及授权登录步骤
1、我们前端访问后台接口,然后由后端去重定向到微信公众号服务器。
2、根据需要做的需求,选择静默授权和非静默授权,由链接中的scope决定,重定向微信公众号服务器url地址是:
redirect_uri = 参数 (后端回调地址)
scope为snsapi_base:(静默授权,不需要用户手动同意)
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
scope为snsapi_userinfo:(非静默授权,需要用户手动同意)
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
1)scope = snsapi_base:(静默授权,不需要用户手动同意)
2)scope = snsapi_userinfo:(非静默授权,需要用户手动同意)
3、微信服务器就会回调到后台服务器携带用户code和type参数,
4、后端就可以根据code去微信服务器获取用户openId,一般会将用户的openId保留在自己数据库中或者校验该用户。
5、后台服务器就会重映射到前端的某个页面,这个需要前后端进行协商。
6、授权登录完成(获取openId完成)。
三、代码实现(只是参考,无法直接运行)
1、我们前端访问后台接口,然后由后端去重定向到微信公众号服务器,重定向微信公众号服务器url地址是。
@RequestMapping(value = "/grant", method = RequestMethod.GET)
public void grant(HttpServletRequest request, HttpServletResponse response) {
//填写你的公众号appid
String appId = "";
//填写你后端code回调地址
String redirectUri = "";
StringBuilder stringBuilder =new StringBuilder();
String url = stringBuilder.append("https://open.weixin.qq.com/connect/oauth2/authorize?appid=")
.append(appId)
.append("&redirect_uri=")
.append(redirectUri)
.append("&response_type=code&scope=snsapi_base&state=111#wechat_redirect")
.toString();
try {
response.sendRedirect(redirectUri);
} catch (IOException e) {
//log.ifno("跳转失败");
}
}
3、微信服务器就会回调到后台服务器携带用户code和type参数,
//这个方法只是一部分,完整的方法在第4步
@RequestMapping(value = "login/callback", method = RequestMethod.GET)
public void loginCallback(HttpServletResponse response, @RequestParam("code") String code) {
//已经获取到用户code,就可以根据用户code,获取用户openId
//............................
}
4、后端就可以根据code去微信服务器获取用户openId,一般会将用户的openId保留在自己数据库中或者校验该用户。
需要用到http请求,可以使用hutool工具,学习一下hutool , 学习网址https://www.hutool.cn/docs/#/
Maven
在项目的pom.xml的dependencies中加入以下内容:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.4</version>
</dependency>
获取openId代码:
@RequestMapping(value = "login/callback", method = RequestMethod.GET)
public void loginCallback(HttpServletResponse response, @RequestParam("code") String code) {
//已经获取到用户code,就可以根据用户code,获取用户openId
String openId = this.getOpenId(code);
}
privte String getOpenId(String code) {
String appId = "";
String secret = "";
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("appid", appId );
paramMap.put("secret", secret);
paramMap.put("code", code);
paramMap.put("grant_type", "authorization_code");
String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
//可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中
String result3= HttpUtil.get(url , paramMap);
//先转成JSON对象
JSONObject jsonObject = JSONObject.parseObject(result);
}
return jsonObject.getString("openid");
}
5、后台服务器就会重映射到前端的某个页面,这个需要前后端进行协商。
@RequestMapping(value = "login/callback", method = RequestMethod.GET)
public void loginCallback(HttpServletResponse response, @RequestParam("code") String code) {
//已经获取到用户code,就可以根据用户code,获取用户openId
String openId = this.getOpenId(code);
//保存openId或做一些其它业务等
//.................
//.................
//.................
//重映射到前端某个页面
String redirectUri = "";
try {
response.sendRedirect(redirectUri);
} catch (IOException e) {
//log.ifno("跳转失败");
}
}
6、授权登录完成(获取openId完成),是不是挺简单的哈哈!!!!!!!!!!。
说明:接入微信公众号支付(JSAPI)开发,就需要先获取用户openId,然后再调用微信统一下单接口,才能获取前端拉起支付框的数据。
写的不对,请指正谢谢