java web實(shí)現(xiàn)自動(dòng)登錄
本文實(shí)例為大家分享了java web實(shí)現(xiàn)自動(dòng)登錄的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)思路
1、在登錄的 api 或 servlet中驗(yàn)證用戶名密碼
2、如果驗(yàn)證成功,則把該用戶信息存在 服務(wù)器 的 session 緩存中,并把 可以表示該用戶的 信息存在 cookie中返回。例如:
//存儲(chǔ) session request.getSession().setAttribute("userBean", queryUser); Cookie cookie = new Cookie("auto_login", username + "#" + password); // 創(chuàng)建 cookie cookie.setMaxAge(60*60*24*7); //設(shè)置時(shí)間為 一周 cookie.setPath(request.getContextPath()); response.addCookie(cookie);
3、創(chuàng)建一個(gè)過濾器,攔截所有的用戶請(qǐng)求
4、在該過濾器中做相應(yīng)的邏輯處理,如下:
- 獲取 服務(wù)器 session 緩存中同名的session。例如: UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");
- 驗(yàn)證是否為空,不為空說(shuō)明用戶登陸之后沒有關(guān)閉瀏覽器 ,直接讓請(qǐng)求通過過濾器,并定位到相應(yīng)界面
- 如果session為空,說(shuō)明用戶 從上一次登陸后關(guān)閉過瀏覽器,則 獲取 用戶請(qǐng)求中的cookie,驗(yàn)證是否有我們定義的可以標(biāo)識(shí)用戶的特殊cookie。
- 如果沒有改cookie ,則直接返回登陸界面。
- 如果有該cookie,則通過cookie中的信息查新到用戶的信息,并跳轉(zhuǎn)到用戶想跳轉(zhuǎn)的界面
核心代碼示例
servlet 登陸邏輯代碼
package com.wl.servlet; import com.wl.dao.UserDao; import com.wl.dao.daoImpl.UserDaoImpl; import com.wl.domain.UserBean; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; @WebServlet(name = "LoginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String username = request.getParameter("username"); String password = request.getParameter("password"); String autoLogin = request.getParameter("auto_login"); System.out.println(username + " = " + password + " " + autoLogin); UserBean userBean = new UserBean(); userBean.setPassword(password); userBean.setUsername(username); UserDao userDao = new UserDaoImpl(); UserBean queryUser = userDao.login(userBean); if(queryUser != null){ if("on".equals(autoLogin)){ Cookie cookie = new Cookie("auto_login", username + "#" + password); cookie.setMaxAge(60*60*24*7); cookie.setPath(request.getContextPath()); response.addCookie(cookie); } request.getSession().setAttribute("userBean", queryUser); response.sendRedirect("index.jsp"); }else { request.getSession().setAttribute("errorInfo", "用戶名密碼不正確"); request.getRequestDispatcher("login.jsp").forward(request,response); } } catch (SQLException e) { e.printStackTrace(); } } }
filter 過濾器核心代碼
package com.wl.filter; import com.wl.dao.UserDao; import com.wl.dao.daoImpl.UserDaoImpl; import com.wl.domain.UserBean; import util.CookieUtil; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.sql.SQLException; @WebFilter(filterName = "AutoLoginFilter") public class AutoLoginFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; UserBean userBean = (UserBean) request.getSession().getAttribute("userBean"); if(userBean != null){ chain.doFilter(req, resp); } else { Cookie[] cookies = request.getCookies(); Cookie auto_login = CookieUtil.findCookie(cookies, "auto_login"); if(auto_login == null) { chain.doFilter(req,resp); } else { String value = auto_login.getValue(); String username = value.split("#")[0]; String password = value.split("#")[1]; UserBean user = new UserBean(); user.setUsername(username); user.setPassword(password); UserDao dao = new UserDaoImpl(); try { UserBean login = dao.login(user); request.getSession().setAttribute("userBean", login); } catch (SQLException e) { e.printStackTrace(); } chain.doFilter(req, resp); } } chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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使用過濾器實(shí)現(xiàn)登錄(用戶自動(dòng)登錄 安全登錄 取消自動(dòng)登錄黑用戶禁止登錄)
- java 驗(yàn)證用戶是否已經(jīng)登錄與實(shí)現(xiàn)自動(dòng)登錄方法詳解
- Java模擬新浪和騰訊自動(dòng)登錄并發(fā)送微博
- java web實(shí)現(xiàn)自動(dòng)登錄功能
- JavaWeb開發(fā)使用Cookie創(chuàng)建-獲取-持久化、自動(dòng)登錄、購(gòu)物記錄、作用路徑
相關(guān)文章
idea 實(shí)現(xiàn)git rebase操作應(yīng)用場(chǎng)景
本文結(jié)合idea工具進(jìn)行rebase的各種場(chǎng)景的操作,借助工具更能直觀地觀察到分支之間地操作差異,方便我們理解rebase的各種操作以及場(chǎng)景的使用,對(duì)idea git rebase操作知識(shí)感興趣的朋友一起看看吧2024-01-01通過實(shí)例學(xué)習(xí)Either 樹和模式匹配
這篇文章主要介紹了通過實(shí)例學(xué)習(xí)Either 樹和模式匹配,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06springAop實(shí)現(xiàn)權(quán)限管理數(shù)據(jù)校驗(yàn)操作日志的場(chǎng)景分析
這篇文章主要介紹了springAop實(shí)現(xiàn)權(quán)限管理數(shù)據(jù)校驗(yàn)操作日志的場(chǎng)景分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Java操作Jenkins操作憑證(Credential)信息方式
這篇文章主要介紹了Java操作Jenkins操作憑證(Credential)信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-05-05Java swing讀取txt文件實(shí)現(xiàn)學(xué)生考試系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java swing讀取txt文件實(shí)現(xiàn)學(xué)生考試系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06