springboot微信扫码登录

这个和QQ登录差不多。具体详细步骤也不多说了,直接参考官网即可,这里写上主要代码
https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

这一步可以放在后台代码里面,也可以放js里面,看自己喜好

@RequestMapping("/memeber/wxLogin")
	public void test(HttpServletResponse response) {
		String baseUrl="https://open.weixin.qq.com/connect/qrconnect?"
				+ "appid="+wx_appid
				+"&redirect_uri="+wx_redirecturl
				+"&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
		try {
			response.sendRedirect(baseUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

在对应的回调域里面写处理

@RequestMapping("/weixinlogin")
	public String wxcallback(String code,HttpServletResponse response) throws Exception {
		String baseUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?code=" + code + "&appid=wx3bdb1192c22883f3&secret=db9d6b88821df403e5ff11742e799105&grant_type=authorization_code";
		String msg = restTemplate.getForObject(baseUrl, String.class);
		Map<String, Object> map1 = JSON.parseObject(msg, Map.class);//字符串形式转map
        Object access_token = map1.get("access_token");
        Object openid = map1.get("openid");
        String infoUrl="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid;
        //获取到的用户信息也是String格式,也需要转一下map,和上面那个类似
        String user_info =restTemplate.getForObject(infoUrl.toString(), String.class);
        JSONObject parseObject = JSON.parseObject(user_info);
        Map<String, Object> map= new HashMap<String, Object>();
        map=parseObject;
        //这个map就是用户信息
        return "redirect:/";
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值