<noframes id="bhrfl"><address id="bhrfl"></address>

    <address id="bhrfl"></address>

    <noframes id="bhrfl"><address id="bhrfl"><th id="bhrfl"></th></address>

    <form id="bhrfl"><th id="bhrfl"><progress id="bhrfl"></progress></th></form>

    <em id="bhrfl"><span id="bhrfl"></span></em>

    全部
    常見問題
    產品動態
    精選推薦

    如何實現網頁的自動登錄

    管理 管理 編輯 刪除

    了解cookie

    我們在瀏覽器進行操作的時候,有時候會在我們的瀏覽器中留下cookie數據,他不同于session域對象,cookie對象有這自己的生命周期,只要生命周期還在,那么cookie就不會消失,具體session和cookie的不同:

    1)存儲的位置不同

    Cookie存儲在瀏覽器端

    Session存儲在服務器端:session攜帶cookie名稱jsessionid存在瀏覽器端存的

    2)存儲數據類型不一樣

    Cooike的構造方法:

    public Cooike(String name,String value):cookie只能存儲String類型

    HttpSession.setAttribute(String name,Object value):可以存儲任意類型

    3)存儲的數據大小是否有限制

    Cookie在瀏覽器端是有限制的,一個站點下的cookie數據有限制的;

    HttSession可以不斷的設置數據,沒有限制

    c4cd3202303041131022869.png

    這張圖就可以看到cookie的創建時間和到期時間,生命周期是一個月.

    自動登錄

    在了解完cookie后,可以開始進行自動登錄的操作了,我們可以通過獲取到瀏覽器中的cookie,來獲取到自動登錄的賬戶的密碼賬戶,然后讓他進行自動登錄,不需要進行賬戶密碼的輸入.

    那么首先要在前端,當我們勾選了自動登錄后,讓我們的后端java吧此次登錄的賬戶密碼添加到瀏覽器中的cookie中:

    <input type="checkbox" name="autolog" value="auto"> 自動登錄
    

    勾選后,autolog的值就為auto了,那么后端讀取到名字為autolog的值,如果為auto就代表了自己勾選了賬戶密碼.接下來進行后端的操作:

        public void log(HttpServletRequest request, HttpServletResponse response) throws IOException {
            HttpSession session1 = request.getSession();
            session1.invalidate();
            String username = request.getParameter( "username" );
            String password = request.getParameter( "password" );
            String autolog = request.getParameter( "autolog" );
            String rpassword = MD5Utils.md5( password );
            System.out.println( "前端輸入的用戶名是" + username );
            NewServiceDao newServiceDao = new NewServiceDaoImpl();
            User user = newServiceDao.logService( username, rpassword );
            System.out.println( user );
            if (user != null) {
                if (user.getState() == 1) {
                    HttpSession session = request.getSession();
                    session.setAttribute( "user", user );
                    if (autolog.equals( "auto" )) {	//讀取數據是否為auto,判斷是否要將數據添加
                        String count = username + "=" + rpassword;
                    //注意,cookie的數據只能存儲字符串類型,不可以添加object,所以我們自己規定格式			
                    //用等號來分割賬戶和密碼
                        count = URLEncoder.encode( count, "utf-8" );
                        //為了防止被人看到賬戶密碼,給他進行轉換格式的添加,并且防止了中文亂碼
                        Cookie auto = new Cookie( "auto", count );
                        //新建一個cookie對象,cookie對象的名字為auto,值為剛剛拼接的賬戶密碼的字符串
                        auto.setMaxAge( 60 * 60 * 24 * 30 );
                        //設置cookie對象的生命周期
                        response.addCookie( auto );
                        //將此次cookie對象添加到瀏覽器中
                    } else {
                        Cookie auto = new Cookie( "auto", "" );
                        auto.setMaxAge( 0 );
                        //生命周期為0意思是刪除cookie
                        response.addCookie( auto );
                    }
                    response.sendRedirect( request.getContextPath() + "/jsp" );
                } else {
                    HttpSession session = request.getSession();
                    session.setAttribute( "user", user );
                    String s = JiHuo.jiHuo();
                    MailUtils.sendMail( user.getEmail(), s, "激活碼" );
                    session.setAttribute( "codee", s );
                    response.sendRedirect( request.getContextPath() + "/jsp/zhong.jsp" );
                }
            }
        }
    

    這次操作后,我們的cookie對象已經添加到瀏覽器中了,我們可以去看看

    47986202303041132361514.png

    可以看到名字為auto的cookie,他的生命周期還有內容,現在就要去讓他去進行自動登錄了.
    首先寫一個過濾器,當我們點擊登錄,或者網址輸入登錄界面的時候,直接讓他進行自動登錄.

    @WebFilter(value = "/jsp/login.jsp",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
    //DispatcherType.REQUEST地址欄直接訪問     DispatcherType.FORWARD 請求轉發
    public class AutoFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("初始化了");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                HttpServletRequest request1=(HttpServletRequest) request;
                HttpServletResponse response1=(HttpServletResponse)response;
                Cookie[] cookies = request1.getCookies();
                //獲取網頁中的全部cookie對象
                if (cookies!=null){
                //判斷是否為空
                    String value=null;
                    for (Cookie cookie:cookies){	//遍歷全部的cookie對象
                        if (cookie.getName().equals( "auto" )){//找到名字為auto的cookie對象
                           value = cookie.getValue();
                           //讀取到他的value值
                        }
                    }
                    if (value!=null){
                    //如果value值不為空,則代表有名字為這個的cookie值
                        value= URLDecoder.decode( value,"utf-8" );
                        //吧格式轉換回來
                        String[] split = value.split( "=" );
                        //因為我們規定了用等號切割賬戶密碼,所以獲取切割后的數組
                        String username = split[0];//賬戶
                        String password = split[1];//密碼
                        NewServiceDao serviceDao = new NewServiceDaoImpl();
                        User user = serviceDao.logService( username, password );
                        //獲取到user對象
                        if (user!=null){
                            HttpSession session = request1.getSession();
                            session.setAttribute( "user",user );
                            //吧user對象存儲進域對象,并且跳轉到登錄后的界面
                            response1.sendRedirect( request1.getContextPath()+"/jsp" );
                        }
                    }
                    else{
                    //如果不是,則放行
                        chain.doFilter( request1,response1 );
                    }
            }else{
            //如果不是,則放行
                    chain.doFilter( request1,response1 );
                }
        }
    
        @Override
        public void destroy() {
            System.out.println("自動登錄器銷毀");
        }
    }
    

    過濾器的原理:

    頭部的@WebFilter里,value值是在那個界面會進行過濾,比如現在過濾器中value = “/jsp/login.jsp”,則代表當在網頁為/jsp/login.jsp的網頁中的時候,會進入過濾器,進行操作,chain.doFilter(request,response)的意思是進行放行操作,讓程序走自己該走的地方,寫完這些后,當我們已經有了cookie對象名字為auto的時候,讓他進行自動登錄

    退出賬戶

    現在如果我想切換賬戶,但是因為cookie對象存在,所以我們現在除非手動刪除cookie,否則不管咋樣,進導登錄界面后,都會自動登錄了,那么為了不去手動刪除,而是通過點擊退出,來讓賬戶退出,則我們需要寫一個方法:

        public void change(HttpServletRequest request, HttpServletResponse response) throws IOException {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals( "auto" )) {
                    //獲取這個名字為auto的cookie
                        System.out.println("進來了");
                        cookie.setMaxAge(0);
                        //生命周期給他賦為0則代表刪除了他
                        response.addCookie( cookie );
                        HttpSession session = request.getSession();
                        session.invalidate();
                        //清空session域
                        response.sendRedirect( request.getContextPath()+"/jsp/login.jsp" );
                    }
                }
            }else{
                try {
                    request.getRequestDispatcher( "/jsp/login.jsp" ).forward( request,response );
                } catch (ServletException e) {
                    e.printStackTrace();
                }
            }
        }
    

    這樣,就吧cookie對象為auto的刪除掉了


    請登錄后查看

    CRMEB-慕白寒窗雪 最后編輯于2023-03-04 11:34:22

    快捷回復
    回復
    回復
    回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
    排序 默認正序 回復倒序 點贊倒序

    {{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

    作者 管理員 企業

    {{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
    {{item.is_suggest == 1? '取消推薦': '推薦'}}
    沙發 板凳 地板 {{item.floor}}#
    {{item.user_info.title || '暫無簡介'}}
    附件

    {{itemf.name}}

    {{item.created_at}}  {{item.ip_address}}
    打賞
    已打賞¥{{item.reward_price}}
    {{item.like_count}}
    {{item.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復

    {{itemc.user_info.nickname}}

    {{itemc.user_name}}

    回復 {{itemc.comment_user_info.nickname}}

    附件

    {{itemf.name}}

    {{itemc.created_at}}
    打賞
    已打賞¥{{itemc.reward_price}}
    {{itemc.like_count}}
    {{itemc.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復
    查看更多
    打賞
    已打賞¥{{reward_price}}
    3536
    {{like_count}}
    {{collect_count}}
    添加回復 ({{post_count}})

    相關推薦

    快速安全登錄

    使用微信掃碼登錄
    {{item.label}} 加精
    {{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態 精選推薦 首頁頭條 首頁動態 首頁推薦
    取 消 確 定
    回復
    回復
    問題:
    問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
    答案:
    提交
    bug 需求 取 消 確 定
    打賞金額
    當前余額:¥{{rewardUserInfo.reward_price}}
    {{item.price}}元
    請輸入 0.1-{{reward_max_price}} 范圍內的數值
    打賞成功
    ¥{{price}}
    完成 確認打賞

    微信登錄/注冊

    切換手機號登錄

    {{ bind_phone ? '綁定手機' : '手機登錄'}}

    {{codeText}}
    切換微信登錄/注冊
    暫不綁定
    亚洲欧美字幕
    CRMEB客服

    CRMEB咨詢熱線 咨詢熱線

    400-8888-794

    微信掃碼咨詢

    CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
    返回頂部 返回頂部
    CRMEB客服