栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 实现 PC端 微信扫码登录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot 实现 PC端 微信扫码登录

依赖
  
        
            org.apache.httpcomponents
            httpclient
            4.5.13
        
        
            org.apache.httpcomponents
            httpcore
        

        
            com.github.binarywang
            weixin-java-mp
            4.1.0
        

PC端后台扫码登录~获取ticket及其他参数
	@GetMapping("/getTicket")
	public  ResponseData> 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> getTicket() {
		Map map = WeixinUtil.getTicket(appid, appSecret);
		return ResponseData.success(map);
	}
工具类
	public static Map 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 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

前端获取即可

手机扫码后回调

微信公众号测试平台:测试平台

@PostMapping("/qrCodeChekToken")
	public ResponseData>  qrCodeChekToken(HttpServletRequest request) throws IOException {
		return loginService.qrCodeChekToken(request);
	}

实现类
@Override
	public ResponseData> 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;
	}
}

获取到信息之后就可以做其他操作了,这里的实现使用的是微信公众平台方式实现,也可以使用微信开放平台实现,但是开放平台需要认证为开发者资质。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979483.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号