Filter過濾器+cookie實現自動登入和安全登入
阿新 • • 發佈:2019-02-20
實現思想:
我們只要登陸成功就把登入資訊儲存到cookie中,將使用者名稱存入session中,下一次進入介面只要通過AutoLoginfilter攔截獲取cookie,如果有登入資訊的cookie就取出來,進行自動登入處理,每一次訪問通過secureLogin的filter進行攔截,查詢session中的name屬性值的容器中有沒有值,如果沒有重定向到登入頁面,如果有就放行
登入介面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<c:if test="${!empty error}"><!-- 如果有登入錯誤資訊則顯示以下內容 -->
${error }<br/>
<c:remove var="error" scope="session"/><!-- 之後要清除掉error資訊 -->
</c:if>
<c:if test="${empty sessionScope.name}" var="boo"><!-- 如果沒有登入則顯示登入頁面 -->
<form action="<c:url value="/LoginServlet"/>" method="post">
<input type="text" name="name" />
<input type="password" name="password" />
<input type="submit" value="登入" />
自動登入:
<input type="radio" name="time" value="0"/>不自動登入
<input type="radio" name="time" value="1"/>1天內自動登入
<input type="radio" name="time" value="7" checked="checked"/>7天內自動登入
</form>
</c:if>
<c:if test="${!boo}"><!-- 如果登入成功就顯示以下內容 -->
歡迎你,${sessionScope.name }<br/>
<a href="<c:url value='/jsps/safe/1.jsp'/>">會員頁面1</a>
<a href="<c:url value='/jsps/safe/2.jsp'/>">會員頁面2</a>
<a href="<c:url value='/autocancelservlet'/>">清除自動登入</a>
</c:if>
</body>
</html>
LoginServlet:
package cn.hncu.filter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自動登入的方法,在登入時把使用者資訊放到cookie中,在下次訪問頁面時,通過過濾器獲取cookie 自動登入
*
*
**/
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
String time = request.getParameter("time");
if (name != null && name.startsWith("hncu")) {
if (password != null && password.length() > 3) {
request.getSession().setAttribute("name", name);// 往session容器中存放代表登入的資訊
// ///下面為實現自動登入,建立一個cookie
name = URLEncoder.encode(name, "utf-8");// 設定寫入cookie時的編碼
password = URLEncoder.encode(password, "utf-8");
Cookie c = new Cookie("autoLogin", name + "," + password);
c.setPath(request.getContextPath());
// 設定時間
int t = 60 * 60 * 24 * Integer.valueOf(time);
// 設定cookie最大時間
c.setMaxAge(t);
// 新增cookie
response.addCookie(c);
} else {
request.getSession().setAttribute("error", "密碼錯誤");
}
} else {
request.getSession().setAttribute("error", "使用者名稱不存在");
}
response.sendRedirect(request.getContextPath()
+ "/jsps/safe/welcome.jsp");
}
}
secureLogin:
package cn.hncu.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;
/**
* 這個過濾器是用來做防未登入訪問的
* */
public class secureLogin implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
if (req.getSession().getAttribute("name") == null) {
resp.sendRedirect(req.getContextPath() + "/jsps/login.jsp");
} else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig config) throws ServletException {
}
}
AutoLoginFilter:
package cn.hncu.filter;
import java.io.IOException;
import java.net.URLDecoder;
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.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 這是自動登入的過濾器
* */
public class AutoLoginFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
if (req.getSession().getAttribute("name") == null) {// 還沒登入,嘗試從cookie中獲取name和pwd來登入
Cookie cs[] = req.getCookies();
if (cs != null) {
for (Cookie c : cs) {
if (c.getName().equals("autoLogin")) {
System.out.println("找到了自動登入的cookie");
String val = c.getValue();
String vals[] = val.split(",");
String name = URLDecoder.decode(vals[0], "utf-8");// 設定讀取解析編碼
String password = URLDecoder.decode(vals[1], "utf-8");
if (name != null && name.startsWith("hncu")
&& password != null && password.length() > 3) {
req.getSession().setAttribute("name", name);
break;
}
}
}
}
}
chain.doFilter(request, response);// 放行
}
@Override
public void init(FilterConfig config) throws ServletException {
}
}