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的上下文

  • 相关阅读:
    【hdu 4135】Co-prime
    【cdoj 1544】当咸鱼也要按照基本法
    【SRM 717 DIV2 C】DerangementsDiv2
    【codeforces 821E】Okabe and El Psy Kongroo
    【SRM 717 div2 B】LexmaxReplace
    【SRM 717 div2 A】 NiceTable
    Network architecture for minimalistic connected objects
    C# 委托的理解
    50条超精辟的经典语录:哗众,可以取宠,也可以失宠!
    50条超精辟的经典语录:哗众,可以取宠,也可以失宠!
  • 原文地址:https://www.cnblogs.com/Jtianlin/p/5765285.html
Copyright © 2011-2022 走看看