首先我们需要一个登录页面,在登录页面中点击微信图标时进入另一个页面并且弹出微信框,在此就直接写入微信页面的代码
<body><div id="login_container"></div><%--引入微信的js支持--%><script type="text/javascript" src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script><script type="text/javascript"> var obj = new WxLogin({ self_redirect:true, id:"login_container", appid: "wxd853562a0548a7d0", scope: "snsapi_login", redirect_uri: "http://bugtracker.itsource.cn/wechat/callback", state: "xxx", style: "white", href: "" });</script></body>
然后我们登录扫码后,然后回调controller的方法,并且将cod传入到后台。然后我们准备一个工具类和一个静态字段类。
静态字段类主要用来变更接收到的参数,
1 public class WeConstants { 2 //创建应用的时候生成的id 3 public final static String APPID = "wxd853562a0548a7d0"; 4 //用户授权后微信的回调域名 5 public final static String CALLBACK="http://bugtracker.itsource.cn"; 6 public final static String SCOPE = "snsapi_login";//表示一个网站应用 7 public final static String APPSECRET = "4a5d5615f93f24bdba2ba8534642dbb6"; 8 9 //微信上获取code的地址10 public final static String CODEURL = "https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";11 //微信上获取at的地址12 public final static String ACCESSTOKEURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";13 //微信上获取用户信息的地址14 public final static String USERINFOURL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";15 16 }
工具类主要用来处理数据,将传入的cod拿取到凭证等
public class HttpClientUtil { // http://bugtracker.itsource.cn/wechat/callback?code=222&state=99 // http://bugtracker.itsource.cn/wechat/callback code=222&state=99 public static String doGet(String uri){ //1:创建一个HttpClient的实例 CloseableHttpClient httpclient = HttpClients.createDefault(); //2:创建一个get请求实例 HttpGet httpGet = new HttpGet(uri); //请求的响应: CloseableHttpResponse response1=null; try { //3:使用HttpClient的实例执行get请求 response1= httpclient.execute(httpGet); //http请求的状态:404 500 200 System.out.println(response1.getStatusLine()); int statusCode = response1.getStatusLine().getStatusCode(); if(statusCode==200){ //请求成功: HttpEntity entity1 = response1.getEntity(); String result = EntityUtils.toString(entity1, "utf-8"); System.out.println(result); return result; }else{ //请求失败:自己做自己的业务逻辑 } }catch (Exception ex){ ex.printStackTrace(); } return null; }}
然后就是 controller类中调用的这个方法了
1 @RequestMapping("/callback") 2 public String callback(String code, String state, Model model, HttpServletRequest req){ 3 System.out.println("---------------------------------------"); 4 System.out.println(); 5 //通过code 和 state 得到 access_token 6 //public final static String ACCESSTOKEURL = "https://api.weixin.qq.com/sns/oauth2/access_token? 7 // appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; 8 String atUrl = WeConstants.ACCESSTOKEURL.replace("APPID", WeConstants.APPID) 9 .replace("SECRET", WeConstants.APPSECRET)10 .replace("CODE", code);11 12 //在发送请求获取access_tonken13 //借助发送请求工具类14 String result = HttpClientUtil.doGet(atUrl);15 System.out.println(result);16 17 //获取用户信息18 //解析jsonStr的字符串19 JSONObject jsonObject = (JSONObject)JSON.parse(result);20 String access_token = String.valueOf(jsonObject.get("access_token"));21 String openid = String.valueOf(jsonObject.get("openid"));22 //"https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";23 String userinfoStr = WeConstants.USERINFOURL.replace("ACCESS_TOKEN", access_token)24 .replace("OPENID", openid);25 26 String userInfoJsonStr = HttpClientUtil.doGet(userinfoStr);27 System.out.println(userInfoJsonStr);28 29 JSONObject userInfoJsonObj = (JSONObject)JSON.parse(userInfoJsonStr);30 String nickname = (String)userInfoJsonObj.get("nickname");31 model.addAttribute("nickname",nickname);32 return "main";33 }34 }
至此微信登录就完成了,需要注意的是,这个登录方式需要在本地host文件中将回调的域名给添加进去,否者是会报错的。
/