引言
微信公众号开发中,必不可少的一环:公众号授权登录、获取微信用户信息
前期准备
内网渗透=>生成本地指定端口映射的外网域名
链接:内网渗透工具natapp使用详解
域名生成之后修改yml文件配置
申请并配置微信测试公众号
01、链接:点击申请微信测试公众号,并扫码关注测试公众号
02、备份appID和appsecret,后面需要用
03、接口配置信息修改(注:此处会回调后台签名验证方法,配置时需启动后台)
04、配置网页授权域名,用于获取微信用户信息
下载微信开发者程序进行测试
链接:微信开发者工具下载
代码部分
01、maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-common -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-common</artifactId>
<version>4.1.0</version>
</dependency>
02、html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>微信授权登录示例</title>
<script src="http://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body style="text-align: center;">
<h2>欢迎您:[[${session.userInfo?.nickname}]]!</h2>
<hr/>
<span>用户信息:[[${session.userInfo}]]</span>
<script th:inline="javascript">
var sceneStr;
var t;
var userInfo = [[${session.userInfo}]];
var appId = [[${wxConfig.appId}]];
var openId = [[${session.openId}]];
var redirectUri = [[${redirectUri}]];
console.log("======用户信息 开始=========");
console.log(userInfo);
if(userInfo != null){
alert("您好:"+userInfo.nickname);
}
console.log("======用户信息 结束=========");
$(function () {
if (openId == null) {
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appId +
'&redirect_uri='+redirectUri +
'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
}
})
</script>
</body>
</html>
03、yml配置
spring:
thymeleaf:
cache: false #关闭缓存
wx:
appId: wxbd9c69753d14236e
appSecret: b44d04f1361136cdef17c07f642641a7
server: http://cm4sad.natappfree.cc
04、controller层
@Autowired
private WxConfig wxConfig;
@RequestMapping
public String index(Model model) throws UnsupportedEncodingException {
model.addAttribute("wxConfig",wxConfig);
// 进行编码
model.addAttribute("redirectUri", URLEncoder.encode(wxConfig.getServer()+"/webAuthLogin/getCode","UTF-8"));
return "login";
}
//获取openid和access_token的连接
private static String getOpenId = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
//获取用户基本信息的连接
private static String getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
@Autowired
private HttpSession httpSession;
/**
* 微信网页授权回调
* @param code
* @return
*/
@RequestMapping("getCode")
public String getCode(String code){
String getOpenIdUrl = getOpenId.replace("APPID", wxConfig.getAppId()).replace("APPSECRET", wxConfig.getAppSecret()).replace("CODE", code);
String response = HttpClientUtil.doGet(getOpenIdUrl);
JSONObject jsonObject = JSONObject.parseObject(response);
String accessToken = jsonObject.get("access_token") == null ? null :jsonObject.get("access_token").toString();
String openId = jsonObject.get("openid") == null ? null : jsonObject.get("openid").toString();
httpSession.setAttribute("accessToken",accessToken);
httpSession.setAttribute("openId",openId);
httpSession.setAttribute("userInfo",getUserInfo(openId,accessToken));
return "redirect:/";
}
/**
* 获取微信用户信息
* @param openId
* @param accessToken
* @return
*/
private JSONObject getUserInfo(String openId,String accessToken){
String userInfoUrl = getUserInfo.replace("ACCESS_TOKEN",accessToken).replace("OPENID", openId);
String userInfo = HttpClientUtil.doGet(userInfoUrl);
JSONObject jsonObject = JSONObject.parseObject(userInfo);
return jsonObject;
}
05、其他部分
由于篇幅有限,我就不放上去了。文章末尾会奉上源码作为参考。
测试结果
输入测试网址
正常情况
其他情况
本人的另一篇关于微信扫码登录的博客:
Spring boot 项目(十二)——实现电脑网页微信扫码自动授权