cookie记住密码功能

很多门户网站都提供了记住密码功能,虽然现在的浏览器都已经提供了相应的记住密码功能

效果就是你每次进入登录页面后就不需要再进行用户名和密码的输入:

记住密码功能基本都是使用cookie来进行实现的,因此我也不例外,我进行了cookie的封装

package xidian.sl.netcredit.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class CookieUtil {
    /**
     * Cookie 追加
     * @return
     * @throws Exception
     */
    public static void addCookie(String name,String value, int timeLong){
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(timeLong);
        ServletActionContext.getResponse().addCookie(cookie);
    }
    /**
     * Cookie 取得
     * @return
     * @throws Exception
     */
    public static String getCookie(String name){
        HttpServletRequest request = ServletActionContext.getRequest();
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie : cookies)
            {
                if(cookie.getName().equals(name))
                {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}

还是比较简单的,这个在用户进行记住密码勾选的时候就可以调用addCookie来记住密码,下次再次打开登陆页面时再调用相应的getCookie类

不过这个要特别注意的是:cookie在浏览器端进行存储是明文的,必须要进行加密,下面的截图就是未加密的时候:

 

 

 

 

posted on 2013-08-19 15:20  发表是最好的记忆  阅读(10996)  评论(1编辑  收藏  举报