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)證是否為空,不為空說明用戶登陸之后沒有關(guān)閉瀏覽器 ,直接讓請(qǐng)求通過過濾器,并定位到相應(yīng)界面
- 如果session為空,說明用戶 從上一次登陸后關(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)登錄、購物記錄、作用路徑
相關(guān)文章
Spring-AOP-ProceedingJoinPoint的使用詳解
這篇文章主要介紹了Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Springboot實(shí)現(xiàn)MQTT通信的示例代碼
本文主要介紹了Springboot實(shí)現(xiàn)MQTT通信的示例代碼,包含了MQTT協(xié)議的特點(diǎn)和工作原理等,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
簡(jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法
下面小編就為大家?guī)硪黄?jiǎn)單談?wù)凾hreadPoolExecutor線程池之submit方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
java項(xiàng)目jar包與jdk的版本不兼容的問題解決
這篇文章主要介紹了java項(xiàng)目jar包與jdk的版本不兼容的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
j2ee之AJAX二級(jí)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了j2ee之AJAX二級(jí)聯(lián)動(dòng)效果的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
java中初始化MediaRecorder的實(shí)現(xiàn)方法
這篇文章主要介紹了java中初始化MediaRecorder的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
springMVC利用FastJson接口返回json數(shù)據(jù)相關(guān)配置詳解
本篇文章主要介紹了springMVC利用FastJson接口返回json數(shù)據(jù)相關(guān)配置詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

