java 驗(yàn)證用戶是否已經(jīng)登錄與實(shí)現(xiàn)自動(dòng)登錄方法詳解
驗(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("你還沒(méi)有登錄");
req.getSession().setAttribute("msg", "請(qǐng)你先登錄");
//重定向到登錄
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect(req.getContextPath()+"/index.jsp");[W2]
}else{
//放行
chain.doFilter(request, response);
}
}
public void destroy() {
}
}
配置到web.xml中且對(duì)jsps/*進(jìn)行過(guò)慮:
<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)自動(dòng)登錄
自動(dòng)登錄,是為了幫助用戶多次使用這個(gè)網(wǎng)頁(yè)時(shí),不用再次輸入用戶名和密碼就可以登錄。
是指用戶將用戶的登錄信息,人,保存到本地的文件中Cookie中。
Name,value – 聲明時(shí) new Cookie(key,value);
Path - 默認(rèn)值,即為當(dāng)前保存cookie的這個(gè)serlvet所在的路徑。
如果Cookie在這樣的路徑:http://loclhost:8080/project/abc/AServlet
則Cookie的路徑為: http://loclhost/project/abc
則說(shuō)明:
所在在http://loclhost/project/abc目錄下的servlet才可以讀取這個(gè)cookie的值。
如果:
保存Cookie類:http://loclhost:8080/project/a/b/AServlet
則Cookie的默認(rèn)path為;
第一步:開發(fā)一個(gè)登錄頁(yè)面
<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">不自動(dòng)登錄
<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èn)本網(wǎng)點(diǎn)中任何一個(gè)頁(yè)面都應(yīng)該實(shí)現(xiàn)自動(dòng)登錄
寫一個(gè)過(guò)慮器,對(duì)所有url=/*進(jìn)行過(guò)慮。在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")){//如果存在自動(dòng)登錄的cookie
String value = c.getValue();//用戶名稱
//登錄成功是指
req.getSession().setAttribute("name", value);
break;
}
}
}
//不管是否自動(dòng)登錄成
chain.doFilter(request, response);
}
第四涉:配置到web.xml中對(duì)所有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("用戶退出");
//刪除整個(gè)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)化代碼
由于用戶在做手工登錄時(shí),也會(huì)進(jìn)入AutoFiilter的doFilter方法,且讀取所有Cookie遍歷一次。而這次遍歷對(duì)用戶來(lái)說(shuō)是多余。
所以應(yīng)該將LoginServet這個(gè)url在doFiler中不過(guò)過(guò)慮。
且對(duì)退出也不能自動(dòng)登錄。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- JavaWeb使用Cookie模擬實(shí)現(xiàn)自動(dòng)登錄功能(不需用戶名和密碼)
- java實(shí)現(xiàn)用戶自動(dòng)登錄
- JAVA爬蟲實(shí)現(xiàn)自動(dòng)登錄淘寶
- java驗(yàn)證用戶是否已經(jīng)登錄 java實(shí)現(xiàn)自動(dòng)登錄
- Java傳入用戶名和密碼并自動(dòng)提交表單實(shí)現(xiàn)登錄到其他系統(tǒng)的實(shí)例代碼
- 詳解JavaEE使用過(guò)濾器實(shí)現(xiàn)登錄(用戶自動(dòng)登錄 安全登錄 取消自動(dòng)登錄黑用戶禁止登錄)
- Java模擬新浪和騰訊自動(dòng)登錄并發(fā)送微博
- java web實(shí)現(xiàn)自動(dòng)登錄功能
- JavaWeb開發(fā)使用Cookie創(chuàng)建-獲取-持久化、自動(dòng)登錄、購(gòu)物記錄、作用路徑
- java web實(shí)現(xiàn)自動(dòng)登錄
相關(guān)文章
SpringBoot使用Minio進(jìn)行文件存儲(chǔ)的實(shí)現(xiàn)
本文主要介紹了SpringBoot使用Minio進(jìn)行文件存儲(chǔ)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringBoot項(xiàng)目使用yml文件鏈接數(shù)據(jù)庫(kù)異常問(wèn)題解決方案
在使用SpringBoot時(shí),利用yml進(jìn)行數(shù)據(jù)庫(kù)連接配置需小心數(shù)據(jù)類型區(qū)分,如果用戶名或密碼是數(shù)字,必須用雙引號(hào)包裹以識(shí)別為字符串,避免連接錯(cuò)誤,特殊字符密碼也應(yīng)用引號(hào)包裹2024-10-10
java 配置MyEclipse Maven環(huán)境具體實(shí)現(xiàn)步驟
這篇文章主要介紹了 java 配置MyEclipse Maven環(huán)境具體實(shí)現(xiàn)步驟的相關(guān)資料,具有一定的參考價(jià)值,需要的朋友可以參考下2016-11-11
Java?awt-對(duì)話框簡(jiǎn)單實(shí)現(xiàn)方式
這篇文章主要介紹了Java?awt-對(duì)話框簡(jiǎn)單實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
springboot?如何解決yml沒(méi)有spring的小葉子標(biāo)志問(wèn)題
這篇文章主要介紹了springboot?如何解決yml沒(méi)有spring的小葉子標(biāo)志問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

