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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaWeb登录记住密码功能可以通过Cookie来实现,具体步骤如下: 1. 在登录时,判断用户是否勾选了“记住密码”选项,如果勾选了,则将用户名和密码保存到Cookie中。 2. 在登录成功后,判断是否勾选了“记住密码”选项,如果勾选了,则将用户名和密码保存到Cookie中。 3. 在用户下次登录时,先判断Cookie中是否保存了用户名和密码,如果有,则将其自动填充到登录表单中。 4. 如果用户手动退出登录,则需要删除保存在Cookie中的用户名和密码信息。 这里提供一个简单的示例代码: ```java //保存Cookie Cookie usernameCookie = new Cookie("username", username); usernameCookie.setMaxAge(7 * 24 * 60 * 60); //设置Cookie有效期为7天 response.addCookie(usernameCookie); Cookie passwordCookie = new Cookie("password", password); passwordCookie.setMaxAge(7 * 24 * 60 * 60); //设置Cookie有效期为7天 response.addCookie(passwordCookie); //读取Cookie Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if ("username".equals(cookie.getName())) { String username = cookie.getValue(); //将用户名填充到登录表单中 } if ("password".equals(cookie.getName())) { String password = cookie.getValue(); //将密码填充到登录表单中 } } } //删除Cookie Cookie usernameCookie = new Cookie("username", null); usernameCookie.setMaxAge(0); response.addCookie(usernameCookie); Cookie passwordCookie = new Cookie("password", null); passwordCookie.setMaxAge(0); response.addCookie(passwordCookie); ``` 需要注意的是,为了保障安全,保存在Cookie中的密码应该进行加密处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值