javaweb微信扫码登录demo

前期准备:1.微信公众号,微信开放平台...

@RequestMapping(value = "/indexTest")
public class WeiXinCoreController {
@Autowired
private LoginService loginService;
    //返回微信二维码,可供扫描登录
@RequestMapping(value = "/wxLogin")
@ResponseBody
protected void wxLogin(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
log.info("WeixinUtil.CORE_REDIRECT_URL=="+WeixinUtil.CORE_REDIRECT_URL);
String url = "https://open.weixin.qq.com/connect/qrconnect?appid="+WeixinUtil.KAPPID
+"&redirect_uri="+URLEncoder.encode(WeixinUtil.CORE_REDIRECT_URL)
+"&response_type=code"
+"&scope=snsapi_login&state=STATE#wechat_redirect";
/*String url="https://open.weixin.qq.com/connect/oauth2/authorize?" +
               "appid="+WeixinUtil.APPID
               +"&redirect_uri="+WeixinUtil.CORE_REDIRECT_URL
               + "&response_type=code&scope=snsapi_userinfo&state=STAT#wechat_redirect";*/
resp.sendRedirect(url);

}
 
@RequestMapping(value = "/callBack")
@ResponseBody
protected ModelAndView callBack(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
String code = req.getParameter("code");
log.info("code=="+code);
String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WeixinUtil.KAPPID
+ "&secret="+WeixinUtil.KAPPSECRET
+ "&code="+code
+ "&grant_type=authorization_code";
JSONObject jsonObject = AuthUtil.doGetJson(url);
String openid = jsonObject.getString("openid");
String token = jsonObject.getString("access_token");
String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+token
+"&openid="+openid
+"&lang=zh_CN";
JSONObject userInfo = AuthUtil.doGetJson(infoUrl);
//req.setAttribute("info", userInfo);
//req.getRequestDispatcher("newIndex1.html").forward(req, resp);
//1.使用微信用户信息直接登录,无需注册和绑定
System.out.println(userInfo);
//return new ModelAndView("newIndex1", "info", userInfo);
//2.将微信与当前系统的账号进行绑定
List<Login> listLogin = loginService.findByOpenid(openid);
System.out.println("listLogin=="+listLogin);
if(listLogin.size()>0){
//req.setAttribute("info", userInfo);
//req.getRequestDispatcher("newIndex1.html").forward(req, resp);
return new ModelAndView("newIndex1", "info", userInfo);
}else{
System.out.println("openid=="+openid);
//req.setAttribute("openid", openid);
//req.getRequestDispatcher("login.html").forward(req, resp);
return new ModelAndView("login", "openid", openid);
}

}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
    <title>微信扫码登录</title>
</head>
<body>
<a href ="http://eggtoy.51118518.com/indexTest/wxLogin">微信扫码登录</a>
</body>

</html>

public class AuthUtil {
public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException{
JSONObject jsonObject = null ;
DefaultHttpClient  client = new DefaultHttpClient();
HttpGet httpGet =new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity != null){
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = jsonObject.fromObject(result);
}
httpGet.releaseConnection();
return jsonObject;

}


猜你喜欢

转载自blog.csdn.net/qq_37361830/article/details/80415947