zoukankan      html  css  js  c++  java
  • 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在浏览器端进行存储是明文的,必须要进行加密,下面的截图就是未加密的时候:

  • 相关阅读:
    思念
    空白
    curl json string with variable All In One
    virtual scroll list All In One
    corejs & RegExp error All In One
    socket.io All In One
    vue camelCase vs PascalCase vs kebabcase All In One
    element ui 表单校验,非必填字段校验 All In One
    github 定时任务 UTC 时间不准确 bug All In One
    input range & color picker All In One
  • 原文地址:https://www.cnblogs.com/shenliang123/p/3267920.html
Copyright © 2011-2022 走看看