java 驗(yàn)證用戶是否已經(jīng)登錄與實(shí)現(xiàn)自動登錄方法詳解
驗(yàn)證用戶是否已經(jīng)登錄
package cn.hongxin.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginFilter implements Filter{ public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //將request強(qiáng)轉(zhuǎn)成htt... HttpServletRequest req = (HttpServletRequest) request; //獲取session HttpSession ss = req.getSession(); //從session中獲取user if(ss.getAttribute("user")==null){ System.err.println("你還沒有登錄"); req.getSession().setAttribute("msg", "請你先登錄"); //重定向到登錄 HttpServletResponse resp = (HttpServletResponse) response; resp.sendRedirect(req.getContextPath()+"/index.jsp");[W2] }else{ //放行 chain.doFilter(request, response); } } public void destroy() { } }
配置到web.xml中且對jsps/*進(jìn)行過慮:
<filter> <filter-name>login</filter-name> <filter-class>cn.itcast.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>login</filter-name> <url-pattern>/jsps/*</url-pattern> <url-pattern>/views/*</url-pattern> </filter-mapping>
實(shí)現(xiàn)自動登錄
自動登錄,是為了幫助用戶多次使用這個網(wǎng)頁時,不用再次輸入用戶名和密碼就可以登錄。
是指用戶將用戶的登錄信息,人,保存到本地的文件中Cookie中。
Name,value – 聲明時 new Cookie(key,value);
Path - 默認(rèn)值,即為當(dāng)前保存cookie的這個serlvet所在的路徑。
如果Cookie在這樣的路徑:http://loclhost:8080/project/abc/AServlet
則Cookie的路徑為: http://loclhost/project/abc
則說明:
所在在http://loclhost/project/abc目錄下的servlet才可以讀取這個cookie的值。
如果:
保存Cookie類:http://loclhost:8080/project/a/b/AServlet
則Cookie的默認(rèn)path為;
第一步:開發(fā)一個登錄頁面
<c:choose> <c:when test="${empty sessionScope.name}"> <form name="x" method="post" action="<c:url value='/LoginServlet'/>"> Name:<input type="text" name="name"/><br/> auto: <input type="radio" name="auto" value="-1">不自動登錄 <br/> <input type="radio" name="auto" value="1">1天<br/> <input type="radio" name="auto" value="7">1周<br/> <input type="submit"/> </form> </c:when> <c:otherwise> 你已經(jīng)登錄了:${name}<br/> <a href="<c:url value='/LoginServlet'/>">退出</a> </c:otherwise> </c:choose>
第二步:成功保存cookie
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收用戶姓名 String name = request.getParameter("name"); String auto = request.getParameter("auto"); //將用戶信息放到session request.getSession().setAttribute("name",name); //判斷auto是否是-1 if(!auto.equals("-1")){ int day = Integer.parseInt(auto);//1|7 int seconds = 60*60*24*day; //聲明cookie Cookie c = new Cookie("autoLogin",name); c.setMaxAge(seconds); c.setPath(request.getContextPath()); //保存cookie response.addCookie(c); } }
第三步:要求訪問本網(wǎng)點(diǎn)中任何一個頁面都應(yīng)該實(shí)現(xiàn)自動登錄
寫一個過慮器,對所有url=/*進(jìn)行過慮。在doFilter中讀取所有cookie。是否存在名稱為autoLogin的名稱cookie。
永遠(yuǎn)都放行。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //在這兒讀取cookie HttpServletRequest req = (HttpServletRequest) request; //獲取所的有cookie Cookie[] cs = req.getCookies(); if(cs!=null){ for(Cookie c:cs){ if(c.getName().equals("autoLogin")){//如果存在自動登錄的cookie String value = c.getValue();//用戶名稱 //登錄成功是指 req.getSession().setAttribute("name", value); break; } } } //不管是否自動登錄成 chain.doFilter(request, response); }
第四涉:配置到web.xml中對所有url=/*
<filter> <filter-name>auto</filter-name> <filter-class>cn.itcast.filter.AutoFilter</filter-class> </filter> <filter-mapping> <filter-name>auto</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
第五步:開發(fā)退出
System.err.println("用戶退出"); //刪除整個session request.getSession().invalidate(); Cookie c = new Cookie("autoLogin", "ddd"); c.setMaxAge(0); c.setPath(request.getContextPath()); response.addCookie(c); // request.getSession().removeAttribute("name"); response.sendRedirect(request.getContextPath()+"/index.jsp");
第六步:優(yōu)化代碼
由于用戶在做手工登錄時,也會進(jìn)入AutoFiilter的doFilter方法,且讀取所有Cookie遍歷一次。而這次遍歷對用戶來說是多余。
所以應(yīng)該將LoginServet這個url在doFiler中不過過慮。
且對退出也不能自動登錄。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- JavaWeb使用Cookie模擬實(shí)現(xiàn)自動登錄功能(不需用戶名和密碼)
- java實(shí)現(xiàn)用戶自動登錄
- JAVA爬蟲實(shí)現(xiàn)自動登錄淘寶
- java驗(yàn)證用戶是否已經(jīng)登錄 java實(shí)現(xiàn)自動登錄
- Java傳入用戶名和密碼并自動提交表單實(shí)現(xiàn)登錄到其他系統(tǒng)的實(shí)例代碼
- 詳解JavaEE使用過濾器實(shí)現(xiàn)登錄(用戶自動登錄 安全登錄 取消自動登錄黑用戶禁止登錄)
- Java模擬新浪和騰訊自動登錄并發(fā)送微博
- java web實(shí)現(xiàn)自動登錄功能
- JavaWeb開發(fā)使用Cookie創(chuàng)建-獲取-持久化、自動登錄、購物記錄、作用路徑
- java web實(shí)現(xiàn)自動登錄
相關(guān)文章
SpringBoot使用Minio進(jìn)行文件存儲的實(shí)現(xiàn)
本文主要介紹了SpringBoot使用Minio進(jìn)行文件存儲的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot項(xiàng)目使用yml文件鏈接數(shù)據(jù)庫異常問題解決方案
在使用SpringBoot時,利用yml進(jìn)行數(shù)據(jù)庫連接配置需小心數(shù)據(jù)類型區(qū)分,如果用戶名或密碼是數(shù)字,必須用雙引號包裹以識別為字符串,避免連接錯誤,特殊字符密碼也應(yīng)用引號包裹2024-10-10java 配置MyEclipse Maven環(huán)境具體實(shí)現(xiàn)步驟
這篇文章主要介紹了 java 配置MyEclipse Maven環(huán)境具體實(shí)現(xiàn)步驟的相關(guān)資料,具有一定的參考價值,需要的朋友可以參考下2016-11-11springboot?如何解決yml沒有spring的小葉子標(biāo)志問題
這篇文章主要介紹了springboot?如何解決yml沒有spring的小葉子標(biāo)志問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03