Cookie/Session机制详解:http://blog.csdn.net/fangaoxin/article/details/6952954

SpringMVC记住密码功能:http://blog.csdn.net/liupeng_family/article/details/38420963?utm_source=tuicool&utm_medium=referral

SpringMVC中使用Interceptor拦截器:http://elim.iteye.com/blog/1750680

登录Controller中,通过登录验证后:

  1. if(autoLoginTimeout > 0){
  2. //自动登录cookie
  3. Cookie userNameCookie = new Cookie("loginUserName", user.getUserName());
  4. Cookie passwordCookie = new Cookie("loginPassword", user.getPassword());
  5. userNameCookie.setMaxAge(autoLoginTimeout);
  6. userNameCookie.setPath("/");
  7. passwordCookie.setMaxAge(autoLoginTimeout);
  8. passwordCookie.setPath("/");
  9. response.addCookie(userNameCookie);
  10. response.addCookie(passwordCookie);
  11. }

(注:如果不设置cookie的path,会默认设为当前路径,所以最好统一设置一个path,否则登出时可能会发现并没有删除登录时的cookie。

附:Cookie跨域操作 http://www.iteye.com/topic/34400

若退出登录,则删除cookie:

  1. @RequestMapping("/logout")
  2. public String logout(HttpServletRequest request, HttpServletResponse response, Model model){
  3. User loginUser = (User) request.getSession().getAttribute("loginUser");
  4. //删除登录cookie
  5. Cookie userNameCookie = new Cookie("loginUserName", loginUser.getUserName());
  6. Cookie passwordCookie = new Cookie("loginPassword", loginUser.getPassword());
  7. userNameCookie.setMaxAge(0);
  8. userNameCookie.setPath("/");
  9. passwordCookie.setMaxAge(0);
  10. passwordCookie.setPath("/");
  11. response.addCookie(userNameCookie);
  12. response.addCookie(passwordCookie);
  13. request.getSession().removeAttribute("loginUser");
  14. return "redirect:xxx";
  15. }

拦截器——用户未登录时检查cookie并实现自动登录(/免登录):

  1. public class LoginInterceptor implements HandlerInterceptor {
  2. @Resource
  3. private UserService userService;
  4. /**
  5. * preHandle方法是进行处理器拦截用的,该方法将在Controller处理之前进行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在
  6. * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在
  7. * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返
  8. * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。
  9. */
  10. @Override
  11. public boolean preHandle(HttpServletRequest request,
  12. HttpServletResponse response, Object handler) throws Exception {
  13. User loginUser = (User) request.getSession().getAttribute("loginUser");
  14. if(loginUser == null){
  15. String loginCookieUserName = "";
  16. String loginCookiePassword = "";
  17. Cookie[] cookies = request.getCookies();
  18. if(null!=cookies){
  19. for(Cookie cookie : cookies){
  20. //if("/".equals(cookie.getPath())){ //getPath为null
  21. if("loginUserName".equals(cookie.getName())){
  22. loginCookieUserName = cookie.getValue();
  23. }else if("loginPassword".equals(cookie.getName())){
  24. loginCookiePassword = cookie.getValue();
  25. }
  26. //}
  27. }
  28. if(!"".equals(loginCookieUserName) && !"".equals(loginCookiePassword)){
  29. User user = userService.getUserByName(loginCookieUserName);
  30. if(loginCookiePassword.equals(user.getPassword())){
  31. request.getSession().setAttribute("loginUser", user);
  32. }
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. /**
  39. * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之
  40. * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操
  41. * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像,
  42. * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor
  43. * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要在Interceptor之后调用的内容都写在调用invoke方法之后。
  44. */
  45. @Override
  46. public void postHandle(HttpServletRequest request,
  47. HttpServletResponse response, Object handler,
  48. ModelAndView modelAndView) throws Exception {
  49. // TODO Auto-generated method stub
  50. }
  51. /**
  52. * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,
  53. * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。
  54. */
  55. @Override
  56. public void afterCompletion(HttpServletRequest request,
  57. HttpServletResponse response, Object handler, Exception ex)
  58. throws Exception {
  59. // TODO Auto-generated method stub
  60. }
  61. }

(注:从浏览器获取cookie时getPath会是null,后台只能得到cookie的name和value。

附:cookie.getPath Domain MaxAge 为null的问题:http://blog.csdn.net/eunyeon/article/details/52931370

spring mvc配置文件:

    1. <mvc:interceptors>
    2. <bean class="com.interceptor.LoginInterceptor" />
    3. </mvc:interceptors>

spring mvc 用cookie和拦截器实现自动登录(/免登录)的更多相关文章

  1. Spring MVC全局异常处理与拦截器校检

    在使用Spring MVC进行开发时,总是要对系统异常和用户的异常行为进行处理,以提供给用户友好的提示,也可以提高系统的安全性. 拦截系统响应错误 首先是拦截系统响应错误,这个可以在web.xml中配 ...

  2. Spring MVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  3. Spring MVC基础知识整理➣拦截器和自定义注解

    概述 Spring MVC中通过注解来对方法或者类进行动态的说明或者标注,类似于配置标识文件的属性信息.当标注的类或者方式被使用时候,通过提取注解信息来达到对类的动态处理.在 MVC中,我们常用的注解 ...

  4. 备忘录《一》基于cookie使用拦截器实现客户每次访问自登陆一次

    原创声明:本文为本人原创作品,绝非他处摘取,转载请联系博主 相信大家在各大网站都会遇到,登录时,在登录框出现下次免登陆/一个月免登陆的类似选项,本次博文就是讲解如何实现,在这记录一下,也算是做个备忘录 ...

  5. spring自定义注解实现登陆拦截器

    1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...

  6. Spring AOP 源码分析 - 拦截器链的执行过程

    1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...

  7. 通过spring抽象路由数据源+MyBatis拦截器实现数据库自动读写分离

    前言 之前使用的读写分离的方案是在mybatis中配置两个数据源,然后生成两个不同的SqlSessionTemplate然后手动去识别执行sql语句是操作主库还是从库.如下图所示: 好处是,你可以人为 ...

  8. Spring AOP深入理解之拦截器调用

    Spring AOP深入理解之拦截器调用 Spring AOP代理对象生成回想 上一篇博客中:深入理解Spring AOP之二代理对象生成介绍了Spring代理对象是怎样生成的,当中重点介绍了JDK动 ...

  9. spring mvc: 资源绑定视图解析器(不推荐)

    spring mvc: 资源绑定视图解析器(不推荐) 不适合单控制器多方法访问,有知道的兄弟能否告知. 访问地址: http://localhost:8080/guga2/hello/index 项目 ...

随机推荐

  1. Django中使用Celery实现定时任务(用djcelery)

    一.引言 Django是python语言下的一个比较热门的Web框架,越来越多的企业和开发者使用Django实现自己的Web服务器.在Web服务器开发过程中,有时候我们不仅仅是要实现Web服务器端和用 ...

  2. delphi 编译生成ipa文件 adhoc步骤

    找IPA文件 开发模式ipa文件和发布模式ipa文件,路径不同. http://www.itnose.net/detail/6101808.html 一.开发模式Development 不需要真机,可 ...

  3. can't load package the specified module could not be found

    can't load package the specified module could not be found 用 Dependency Walker 2.2Dependency Walker ...

  4. WebConfig配置讲解

    http://www.cnblogs.com/cyq1162/archive/2006/11/16/562690.html sqlserver配置数据库连接字符串时需分2种情况 windows 和 s ...

  5. vue 解决报错1

    [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available ...

  6. ubuntu查询可用安装包

    当使用apt-get install packages时,如果不知道安装包的具体名称,可以使用关键字进行搜索,使用:apt-cache search keywords

  7. spring cloud 服务提供者

    1. pom 依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  8. 两个关于URL解析的例子

    例一: /* 解析URL查寻串中的name=value参数对 将name=value对存储在对象属性中,并返回对象 alert(getQuery().name) */ function getQuer ...

  9. redis数据迁移

    redis的备份和还原,借助了第三方的工具---redis-dump,  redis中使用redis-dump导出.导入.还原数据实例 1.安装redis-dump # yum install rub ...

  10. TEXT 4 A question of standards

    TEXT 4 A question of standards 一个关乎标准的问题 Feb 9th 2006 From The Economist Global Agenda More suggesti ...