Springboot中登录后关于cookie和session拦截问题的案例分析
一、前言
1、简单的登录验证可以通过Session或者Cookie实现。
2、每次登录的时候都要进数据库校验下账户名和密码,只是加了cookie或session验证后;比如登录页面A,登录成功后进入页面B,若此时cookie过期,在页面B中新的请求url到页面c,系统会让它回到初始的登录页面。(类似单点登录sso(singlesignon))。
3、另外,无论基于Session还是Cookie的登录验证,都需要对HandlerInteceptor进行配置,增加对URL的拦截过滤机制。
二、利用Cookie进行登录验证
1、配置拦截器代码如下:
publicclassCookiendSessionInterceptorimplementsHandlerInterceptor{ @Override publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{ log.debug("进入拦截器"); Cookie[]cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(Cookiecookie:cookies){ log.debug("cookie===for遍历"+cookie.getName()); if(StringUtils.equalsIgnoreCase(cookie.getName(),"isLogin")){ log.debug("有cookie---isLogin,并且cookie还没过期..."); //遍历cookie如果找到登录状态则返回true继续执行原来请求url到controller中的方法 returntrue; } } } log.debug("没有cookie-----cookie时间可能到期,重定向到登录页面后请重新登录。。。"); response.sendRedirect("index.html"); //返回false,不执行原来controller的方法 returnfalse;} @Override publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{ } @Override publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{ } }
2、在Springboot中拦截的请求不管是配置监听器(定义一个类实现一个接口HttpSessionListener)、过滤器、拦截器,都要配置如下此类实现一个接口中的两个方法。
代码如下:
@Configuration publicclassWebConfigimplementsWebMvcConfigurer{ //这个方法是用来配置静态资源的,比如html,js,css,等等 @Override publicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){ } //这个方法用来注册拦截器,我们自己写好的拦截器需要通过这里添加注册才能生效 @Override publicvoidaddInterceptors(InterceptorRegistryregistry){ //addPathPatterns("/**")表示拦截所有的请求 //excludePathPatterns("/firstLogin","/zhuce");设置白名单,就是拦截器不拦截。首次输入账号密码登录和注册不用拦截! //登录页面在拦截器配置中配置的是排除路径,可以看到即使放行了,还是会进入prehandle,但是不会执行任何操作。 registry.addInterceptor(newCookiendSessionInterceptor()).addPathPatterns("/**").excludePathPatterns("/", "/**/login", "/**/*.html", "/**/*.js", "/**/*.css", "/**/*.jpg"); } }
3.前台登录页面index.html(我把这个html放在静态资源了,也让拦截器放行了此路由url)
前端测试就是一个简单的form表单提交
账号:
密码:
4、后台控制层Controller业务逻辑:登录页面index.html,登录成功后loginSuccess.html。
在loginSuccess.html中可提交表单进入次页demo.html,也可点击“退出登录”后台清除没有超时的cookie,并且回到初始登录页面。
@Controller @Slf4j @RequestMapping(value="/") publicclassTestCookieAndSessionController{ @AutowiredJdbcTemplatejdbcTemplate; /***首次登录,输入账号和密码,数据库验证无误后,响应返回你设置的cookie。再次输入账号密码登录或者首次登录后再请求下一个页面,就会在请求头中携带cookie,*前提是cookie没有过期。*此url请求方法不管是首次登录还是第n次登录,拦截器都不会拦截。*但是每次(首次或者第N次)登录都要进行,数据库查询验证账号和密码。*做这个目的是如果登录页面A,登录成功后进页面B,页面B有链接进页面C,如果cookie超时,重新回到登录页面A。(类似单点登录)*/ @PostMapping(value="login") publicStringtest(HttpServletRequestrequest,HttpServletResponseresponse,@RequestParam("name1")Stringname,@RequestParam("pass1")Stringpass)throwsException{ try{ Mapresult=jdbcTemplate.queryForMap("select*fromuserinfowherename=?andpassword=?",newObject[]{name,pass}); if(result==null||result.size()==0){ log.debug("账号或者密码不正确或者此人账号没有注册"); thrownewException("账号或者密码不正确或者此人账号没有注册!"); }else{ log.debug("查询满足条数----"+result); Cookiecookie=newCookie("isLogin","success"); cookie.setMaxAge(30); cookie.setPath("/"); response.addCookie(cookie); request.setAttribute("isLogin",name); log.debug("首次登录,查询数据库用户名和密码无误,登录成功,设置cookie成功"); return"loginSuccess";} }catch(DataAccessExceptione){ e.printStackTrace(); return"error1"; } } /**测试登录成功后页面loginSuccess,进入次页demo.html*/ @PostMapping(value="sub") publicStringtest()throwsException{ return"demo"; } /**能进到此方法中,cookie一定没有过期。因为拦截器在前面已经判断力。过期,拦截器重定向到登录页面。过期退出登录,清空cookie。*/ @RequestMapping(value="exit",method=RequestMethod.POST) publicStringexit(HttpServletRequestrequest,HttpServletResponseresponse)throwsException{ Cookie[]cookies=request.getCookies(); for(Cookiecookie:cookies){ if("isLogin".equalsIgnoreCase(cookie.getName())){ log.debug("退出登录时,cookie还没过期,清空cookie"); cookie.setMaxAge(0); cookie.setValue(null); cookie.setPath("/"); response.addCookie(cookie); break; } } //重定向到登录页面 return"redirect:index.html"; } }
5、效果演示:
①在登录“localhost:8082”输入账号登录页面登录:
②拦截器我设置了放行/login,所以请求直接进Controller相应的方法中:
日志信息如下:
下图可以看出,浏览器有些自带的不止一个cookie,这里不要管它们。
③在loginSuccess.html,进入次页demo.html。cookie没有过期顺利进入demo.html,并且/sub方法经过拦截器(此请求请求头中携带cookie)。
过期的话直接回到登录页面(这里不展示了)
④在loginSuccess.html点击“退出登录”,后台清除我设置的没过期的cookie=isLogin,回到登录页面。
三、利用Session进行登录验证
1、修改拦截器配置略微修改下:
Interceptor也略微修改下:还是上面的preHandle方法中:
2.核心
前端我就不展示了,就是一个form表单action="login1"
后台代码如下:
/**利用session进行登录验证*/ @RequestMapping(value="login1",method=RequestMethod.POST) publicStringtestSession(HttpServletRequestrequest, HttpServletResponseresponse, @RequestParam("name1")Stringname, @RequestParam("pass1")Stringpass)throwsException{ try{ Mapresult=jdbcTemplate.queryForMap("select*fromuserinfowherename=?andpassword=?",newObject[]{name,pass}); if(result!=null&&result.size()>0){ StringrequestURI=request.getRequestURI(); log.debug("此次请求的url:{}",requestURI); HttpSessionsession=request.getSession(); log.debug("session="+session+"session.getId()="+session.getId()+"session.getMaxInactiveInterval()="+session.getMaxInactiveInterval()); session.setAttribute("isLogin1","true1"); } }catch(DataAccessExceptione){ e.printStackTrace(); return"error1"; } return"loginSuccess"; } //登出,移除登录状态并重定向的登录页 @RequestMapping(value="/exit1",method=RequestMethod.POST) publicStringloginOut(HttpServletRequestrequest){ request.getSession().removeAttribute("isLogin1"); log.debug("进入exit1方法,移除isLogin1"); return"redirect:index.html"; } }
日志如下:可以看见springboot内置的tomcat中sessionid就是请求头中的jsessionid,而且默认时间1800秒(30分钟)。
我也不清楚什么进入拦截器2次,因为我login1设置放行了,肯定不会进入拦截器。可能是什么静态别的什么资源吧。
session.getId()=F88CF6850CD575DFB3560C3AA7BEC89F==下图的JSESSIONID
//点击退出登录,请求退出url的请求头还是携带JSESSIONID,除非浏览器关掉才消失。(该session设置的属性isLogin1移除了,session在不关浏览器情况下或者超过默认时间30分钟后,session才会自动清除!)
四、完结
到此这篇关于Springboot中登录后关于cookie和session拦截案例的文章就介绍到这了,更多相关Springboot登录关于cookie和session拦截内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。