依赖
<!-- httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.1.0</version>
</dependency>
PC端后台扫码登录~获取ticket及其他参数
@GetMapping("/getTicket")
public ResponseData<Map<String, Object>> getTicket() {
return loginService.getTicket();
}
application.yml配置
### 微信网页(h5)配置参数
wxh5:
appid: wx57f8a596522dfb99 #公众号APPID (这使用的是测试账户公众号)
appSecret: 426c9109b51dd1f5183ec506a7d78dbd #公众号AppSecret (这使用的是测试账户公众号)
serverurl: http://jdqaiv.natappfree.cc #服务器回调地址(最好是https协议)
实现类
@Value("${wxh5.appid}")
private String appid;
@Value("${wxh5.appSecret}")
private String appSecret;
@Value("${wxh5.serverurl}")
private String serverurl;
@Override
public ResponseData<Map<String, Object>> getTicket() {
Map<String, Object> map = WeixinUtil.getTicket(appid, appSecret);
return ResponseData.success(map);
}
工具类
public static Map<String, Object> getTicket(String appid, String appSecret) {
String accessToken = getAccessToken(appid, appSecret);
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + accessToken.trim();
JSONObject jsonObject = new JSONObject();
JSONObject actionInfo = new JSONObject();
JSONObject scene = new JSONObject();
scene.put("scene_str", getRandomString(20));
actionInfo.put("scene", scene);
jsonObject.put("expire_seconds", "604800");
jsonObject.put("action_name", "QR_STR_SCENE");
jsonObject.put("action_info", actionInfo);
String json = HttpClient.sendJson(url, jsonObject.toString());
JSONObject resJson = JSONObject.parseObject(json);
Map<String, Object> map = new HashMap<>();
map.put("ticket", resJson.get("ticket").toString());
map.put("expire_seconds", resJson.get("expire_seconds").toString());
map.put("url", resJson.get("url").toString());
return map;
}
HttpClient
public static String sendJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
获取二维码
通过 ticket 换取二维码
获取二维码 ticket 后,开发者可用 ticket 换取二维码图片。请注意,本接口无须登录态即可调用。
请求说明
HTTP GET请求(请使用 https 协议)https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET 提醒:TICKET记得进行UrlEncode
前端获取即可
手机扫码后回调
微信公众号测试平台:测试平台
这里的URL是微信扫码后的回调地址,需要做验证
微信服务器会对我们填写的URL发送请求
如果我们的URL为http://jdqaiv.natappfree.cc/login/qrCodeChekToken
那么,微信服务器就会发送如下请求
http://jdqaiv.natappfree.cc/login/qrCodeChekToken?timestamp=1598322596602&signature=33f90dee6b86275b5851143d871d0122ef48cb0e&nonce=8588471354&echostr=c695cca1-8f36-403b-96e0-ffdac740b0ef
当返回结果为echostr的值时,就算调用成功了
c695cca1-8f36-403b-96e0-ffdac740b0ef
所以我们需要在自己的项目中写一个方法,提供给微信服务器调用
@GetMapping("/login/qrCodeChekToken")
public String callBack(HttpServletRequest request) throws WxErrorException, IOException {
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
return echostr;
校验通过后台再改为如下的代码:
@PostMapping("/qrCodeChekToken")
public ResponseData<Map<String, Object>> qrCodeChekToken(HttpServletRequest request) throws IOException {
return loginService.qrCodeChekToken(request);
}
实现类
@Override
public ResponseData<Map<String, Object>> qrCodeChekToken(HttpServletRequest request) throws IOException {
WxMpXmlMessage message = WxMpXmlMessage.fromXml(request.getInputStream());//获取消息流,并解析xml
String messageType = message.getMsgType(); //消息类型
String messageEvent = message.getEvent(); //消息事件
// openid
String fromUser = message.getFromUser(); //发送者帐号
String text = message.getContent(); //文本消息 文本内容
log.info("文本消息来自:{}", fromUser);
log.info("文本消息:{}", text);
return null;
}
}
获取到信息之后就可以做其他操作了,这里的实现使用的是微信公众平台方式实现,也可以使用微信开放平台实现,但是开放平台需要认证为开发者资质。