欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用Filter實(shí)現(xiàn)自動(dòng)登錄的方法

 更新時(shí)間:2017年04月08日 14:18:03   作者:第九種格調(diào)的人生  
這篇文章主要為大家詳細(xì)介紹了java使用Filter實(shí)現(xiàn)自動(dòng)登錄的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)自動(dòng)登錄的具體代碼,供大家參考,具體內(nèi)容如下

  • 當(dāng)你勾選(記住登錄狀態(tài)),用cookie保存用戶名和密碼。不勾選,cookie失效。
  • 所有的頁面都要經(jīng)過autoLoginFilter.java 的過濾器,在這類中,必須要判斷cookies不為null,獲得所有的cookie,得到name為user的cookie,進(jìn)行用戶名和密碼的驗(yàn)證,如果不為null,則將user存入session。
  • 在LoginServlet.java中,獲得username和password參數(shù),進(jìn)行dao驗(yàn)證,如果不為空,放入seesion中,進(jìn)行頁面跳轉(zhuǎn)。
  • 創(chuàng)建cookie對象。setpath("/"),表示本應(yīng)用下的所有路徑都能訪問此cookie。
  • 對于已經(jīng)正確登錄的用戶,再次訪問其他頁面必定會(huì)再次經(jīng)過autoLoginFilter,這時(shí),判斷當(dāng)前session中的user是否為null,不為null,直接通過。
  • 對于**login.jsp的有關(guān)頁面,不需要經(jīng)過autoLoginFilter。
package com.learning.web.servlet;

import java.io.IOException;
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 com.learning.domain.User;
import com.learning.service.UserService;

@WebServlet("/servlet/loginServlet")
public class LoginServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  String autologin = request.getParameter("autologin");
  
  UserService userService=new UserService();
  User user = userService.findUser(username, password);
  //user不為null,則登錄成功
  if (user!=null) {
  //創(chuàng)建cookie來保存用戶信息
  Cookie cookie=new Cookie("user", user.getUsername()+"&"+user.getPassword());
  cookie.setPath("/");
  //autologin不為null,則記住了登錄狀態(tài)
  if (autologin!=null) {
   cookie.setMaxAge(1*60*60*24);//一天的有效時(shí)間
  }
  else {
   cookie.setMaxAge(0);
  }
  response.addCookie(cookie);
  request.getSession().setAttribute("user", user);
  request.getRequestDispatcher("/home.jsp").forward(request, response);
  }else {
  response.sendRedirect(request.getContextPath()+"/homeLogin.jsp");
  }
  
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doGet(request, response);
 }

}
package com.learning.web.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.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;

import com.learning.domain.User;
import com.learning.service.UserService;

@WebFilter(urlPatterns="/*",initParams={@WebInitParam(name="autologin",value="login"),@WebInitParam(name="",value="")})
public class AutoFilter implements Filter{

 private FilterConfig filterConfig;
 @Override
 public void destroy() {
 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException {
 // 轉(zhuǎn)換對象
 HttpServletRequest httpServletRequest = (HttpServletRequest) request;
 HttpServletResponse httpServletResponse = (HttpServletResponse) response;
 // 獲得訪問的路徑
 String uri = httpServletRequest.getRequestURI();
 String contextPath = httpServletRequest.getContextPath();
 uri = uri.substring(contextPath.length() + 1);
 // 獲得初始化參數(shù)
 String login = filterConfig.getInitParameter("autologin");
 System.out.println("直接通行的路徑:"+login);
 // 不包含"login"的路徑就要進(jìn)行過濾 (xxxlogin.jsp 不需要自動(dòng)登錄)
 if (!uri.contains(login)) {
  HttpSession session = httpServletRequest.getSession();
  User u = (User) session.getAttribute("user");
  if (u != null) {
  System.out.println("session不為null");
  chain.doFilter(request, response);
  } else {

  // 處理業(yè)務(wù)邏輯
  // 1.獲得cookie 得到User的信息

  String username = "";
  String password = "";
  UserService userService = new UserService();
  Cookie[] cookies = httpServletRequest.getCookies();
  for (int i = 0;cookies!=null&& i < cookies.length; i++) { 
   if ("user".equals(cookies[i].getName())) {
   String string = cookies[i].getValue();
   String[] values = string.split("&");
   username = values[0];
   password = values[1];
   User user = userService.findUser(username, password);
   
   // 不為空則放入session
   if (user != null) {
    System.out.println("自動(dòng)登錄了");
    httpServletRequest.getSession().setAttribute("user", user);
   }
   }
  }
  }
 }
 // 2.放行
 chain.doFilter(request, response);
 }


 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
 
 this.filterConfig=filterConfig;
 
 }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java連接數(shù)據(jù)庫的5種方式解讀

    java連接數(shù)據(jù)庫的5種方式解讀

    這篇文章主要介紹了java連接數(shù)據(jù)庫的5種方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>
    2024-04-04
  • 使用JPA雙向多對多關(guān)聯(lián)關(guān)系@ManyToMany

    使用JPA雙向多對多關(guān)聯(lián)關(guān)系@ManyToMany

    這篇文章主要介紹了使用JPA雙向多對多關(guān)聯(lián)關(guān)系@ManyToMany,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java實(shí)現(xiàn)手寫自旋鎖的示例代碼

    Java實(shí)現(xiàn)手寫自旋鎖的示例代碼

    自旋鎖是專為防止多處理器并發(fā)而引入的一種鎖,它在內(nèi)核中大量應(yīng)用于中斷處理等部分。本文將用Java實(shí)現(xiàn)手寫自旋鎖,需要的可以參考一下
    2022-08-08
  • spring?boot項(xiàng)目中如何使用nacos作為配置中心

    spring?boot項(xiàng)目中如何使用nacos作為配置中心

    這篇文章主要介紹了spring?boot項(xiàng)目中如何使用nacos作為配置中心問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java透明窗體的設(shè)置方法

    Java透明窗體的設(shè)置方法

    在本文中我們給大家整理了關(guān)于Java透明窗體的設(shè)置方法以及需要注意的地方,需要的朋友們學(xué)習(xí)參考下。
    2019-03-03
  • Java線程池ForkJoinPool(工作竊取算法)的使用

    Java線程池ForkJoinPool(工作竊取算法)的使用

    Fork就是把一個(gè)大任務(wù)切分為若干個(gè)子任務(wù)并行地執(zhí)行,Join就是合并這些子任務(wù)的執(zhí)行結(jié)果,最后得到這個(gè)大任務(wù)的結(jié)果。Fork/Join?框架使用的是工作竊取算法。本文主要介紹了ForkJoinPool的使用,需要的可以參考一下
    2022-11-11
  • java8 統(tǒng)計(jì)字符串字母個(gè)數(shù)的幾種方法總結(jié)(推薦)

    java8 統(tǒng)計(jì)字符串字母個(gè)數(shù)的幾種方法總結(jié)(推薦)

    下面小編就為大家分享一篇java8 統(tǒng)計(jì)字符串字母個(gè)數(shù)的幾種方法總結(jié)(推薦),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來吧
    2017-11-11
  • 詳解Java中restTemplate的使用

    詳解Java中restTemplate的使用

    這篇文章主要為大家詳細(xì)介紹了Java中restTemplate用法的相關(guān)資料,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下
    2022-11-11
  • Java編寫實(shí)現(xiàn)窗體程序顯示日歷

    Java編寫實(shí)現(xiàn)窗體程序顯示日歷

    這篇文章主要為大家詳細(xì)介紹了Java編寫實(shí)現(xiàn)窗體程序顯示日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Springboot3整合Mybatis3的完整步驟記錄

    Springboot3整合Mybatis3的完整步驟記錄

    這篇文章主要給大家介紹了關(guān)于Springboot3整合Mybatis3的完整步驟,Spring Boot和MyBatis分別是兩個(gè)功能強(qiáng)大的框架,它們的協(xié)同使用可以極大地簡化數(shù)據(jù)訪問層的開發(fā),提高整體的開發(fā)效率,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01

最新評論