欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

防止登录成功后重复刷新页面跳回登录页面

发布时间:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 防止登录成功后重复刷新页面跳回登录页面 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

登录action:system/Syslogin/sysLogin.do

public String sysLogin() { try {//验证码验证String yzm = Struts2Utils.getRequest().getParameter("yzm");//登录验证String username = Struts2Utils.getRequest().getParameter("username");String password = Struts2Utils.getRequest().getParameter("password");String authCode = (String) Struts2Utils.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);if (authCode == null || !authCode.equalsIgnoreCase(yzm) ) {return "yzmError";}User user = userService.getsysLoginByUsername(username);Loginrecord loginrecord = new Loginrecord();if(user==null){//捕获异常,保证继续执行(通过验证码方可记录)try {//登录日志(失败)loginrecord.setUsername(username);loginrecord.setPassword(password);loginrecord.setType(1);loginrecord.setResult(0);loginrecord.setIp(RequestUtils.getIpAddr(Struts2Utils.getRequest()));loginrecord.setLogtime(new Date());loginrecordService.insert(loginrecord);} catch (Exception e) {logger.info(e);e.printStackTrace();}return "loginError";}else if(!user.getPassword().equals(password)){//捕获异常,保证继续执行(通过验证码方可记录)try {//登录日志(失败)loginrecord.setUsername(username);loginrecord.setPassword(password);loginrecord.setType(1);loginrecord.setResult(0);loginrecord.setIp(RequestUtils.getIpAddr(Struts2Utils.getRequest()));loginrecord.setLogtime(new Date());loginrecordService.insert(loginrecord);} catch (Exception e) {logger.info(e);e.printStackTrace();}return "loginError";}else{Struts2Utils.getSession().setAttribute(Constants.CURRENT_USER, user);//捕获异常,保证继续执行(通过验证码方可记录)try {//登录日志(成功)loginrecord.setUsername(username);loginrecord.setType(1);loginrecord.setResult(1);loginrecord.setIp(RequestUtils.getIpAddr(Struts2Utils.getRequest()));loginrecord.setLogtime(new Date());loginrecord.setUserid(user.getUserid());loginrecordService.insert(loginrecord);} catch (Exception e) {logger.info(e);e.printStackTrace();}//登录成功后返回成功页面的跳转方法return "!/system/Syslogin/index.do"; }
//登录成功跳转页面public String index() {User currentUser = (User)Struts2Utils.getSession().getAttribute(Constants.CURRENT_USER);if(currentUser==null){//请先登录return "sysLogin";}else{if(currentUser.getIsadmin()!=1){//不是管理员return "sysLogin";}}return "/WEB-INF/frame/backFrame/index.jsp";}


采用调用跳转登录成功页面的方法就可以避免登录成功后重复刷新页面跳回登录页面,因为登录成功后的url改变了

正常成功后路径:system/Syslogin/sysLogin.do

通过

return "!/system/Syslogin/index.do"; 路径就变了:system/Syslogin/index.do


不管怎么刷新只会执行public String index()方法,前提是必须做session判断是否还有效.

总结

以上是生活随笔为你收集整理的防止登录成功后重复刷新页面跳回登录页面的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。