zoukankan      html  css  js  c++  java
  • java的web项目中使用cookie保存用户登陆信息

    本文转自:http://lever0066.iteye.com/blog/1735963

    最近在编写论坛系统的实现,其中就涉及到用户登陆后保持会话直到浏览器关闭,同时可以使用cookie保存登陆信息以便在下次可以自动登陆。使用struts2框架实现。   下面是在action里登陆和注销的函数 

    Java代码 
    public String logout() {   

        this.getSession().clear();   
         Cookie idCookie = new Cookie("id", "");   
         Cookie passwordCookie = new Cookie("password", "");   
         idCookie.setMaxAge(0); //使cookie失效   
         passwordCookie.setMaxAge(0);   
         idCookie.setPath("/");   //这个不能少   
         passwordCookie.setPath("/");   
         servletResponse.addCookie(idCookie);   
         servletResponse.addCookie(passwordCookie);   
        return SUCCESS;   
    }   
      
    public String login() {   
      
         User user = userService.getUser(id, password);   
        if (user != null) {   
            this.getSession().put("user", id);   
            //如果选择保存登陆信息   
            if (saveLogin != null) {   
                 Cookie idCookie = new Cookie("id", id); //可以使用md5或着自己的加密算法保存   
                 Cookie passwordCookie = new Cookie("password", password);   
                 idCookie.setPath("/webappName/"); //cookie路径问题,在我的其他文章里有专门的讲解   
                 idCookie.setMaxAge(MAX_AGE);   
                 passwordCookie.setPath("/webappName/");   
                 passwordCookie.setMaxAge(MAX_AGE);   
                 servletResponse.addCookie(idCookie);   
                 servletResponse.addCookie(passwordCookie);   
                 saveLogin = null;   
             }   
            return SUCCESS;   
         } else {   
            this.setInfo("用户名或密码错误");   
            return INFO;   
         }   
    } 

    在保存完cookie后要保证以后用户每次访问该系统的任意页面系统都必须先访问用户的cookie查看是否有登陆信息,这个使用servlet的filter实现最为方便。下面就是filter的代码 
         

    private void doBeforeProcessing(ServletRequest request) {   
             HttpSession session = ((HttpServletRequest) request).getSession(true);   
            //首先检查session,若已经登陆则直接忽略一下代码   
            if (session.getAttribute("user") != null) {   
                return;   
             }   
             Cookie[] cookies = ((HttpServletRequest) request).getCookies();   
             String id = null;   
             String password = null;   
            if (cookies != null) {   
                for (Cookie c : cookies) {   
                    if (c.getName().equals("id")) {   
                         id = c.getValue();   
                     }   
                    if (c.getName().equals("password")) {   
                         password = c.getValue();   
                     }   
                 }   
             }   
             UserService userService = (UserService) this.getApplicationContext().getBean("userService");   
            if (userService.isUser(id, password)) {   
                 session = ((HttpServletRequest) request).getSession(true);   
                 session.setAttribute("user", id);   
             }   
         }   
      
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)   {   
             doBeforeProcessing(request);   
             chain.doFilter(request, response);   
         }   

         其中涉及的在filter中访问spring的上下文

  • 相关阅读:
    sp_executesql 带输出参数的 EXEC 动态执行 (z)
    查端口是否被占用 DOS 命令netstat(Z)
    MS SQL处理树型结构数据 将子节点记录的各项字段值累加赋值给其父节点
    lazarus unidac使用注意事项
    UNIDAC在arm linux运行出错的解决方法
    [转]为Linux下的Lazarus添加中文输入支持
    银河麒麟(aarch64)安装Lazarus步骤
    raspberry 添加拼音五笔输入法
    lazarus跨平台编译步骤
    拷贝构造函数与赋值操作符的具体实现 【转】参考度4.5星
  • 原文地址:https://www.cnblogs.com/Jtianlin/p/5765285.html
Copyright © 2011-2022 走看看