J2EE/JAVA——使用fillter和cookie完成自动登录业务(实例演示)

1.什么是cookie

        Cookie,是某些网站为了辨别用户身份,进行Session跟踪而储存在用户本地终端上的数据(通常经过加密),由用户客户端计算机暂时或永久保存的信息 。通常是由键值对的方式保存在浏览器中。

        在浏览器的开发者工具中,在Storage目录下的Cookies中就可以查找到当前页面的cookies,cookies通常用于保存用户的简单的文本本件,因此它可以帮助我们实现记录用户个人信息的功能。

2.自动登录的原理

        简单来说,自动登录的原理就是使用cookies来保存用户名和密码,在登录前比对即可。一个简单的自动登录需要一下的流程。

1.当用户第一次登录时,在登陆界面就应该设置有自动登录的选项,用户选中自动登录才能有自动登录的功能,然后将用户名和密码传到后台中。

2.然后在后台中首先从数据库的数据对比,看是否正确。正确且用户选择了自动登录则将用户名和密码创建为Cookie,并设置访问路径和保存的时间,然后发送到浏览器中。

3.Cookie发送到浏览器中就会保存在浏览器的cookie区域内,这个时候就可以用上面的方法来查看cookie的值了。

4.用户再次登录时,就会将cookie所存入的用户名和密码带入到后台中,与数据库中的数据进行对比,若对比存在则自动跳转到登录后的页面中。

3.一个简单的自动登录实例

        下面我们就举例一个十分简单自动登录实例源码,这里直接将用户名和密码写死了,正常的企业流程应该有与数据库中数据对比的过程。

User类的javabean源码:

这个User是在登录中使用到的一个类。

public class User {

	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

自动登录的过滤器:

我们将创建cookie并且对比的过程放入到过滤器中使用,这样在控制器登录前就会优先执行这个里面的代码。

public class AutoLoginFilter implements Filter {

	public void doFilter(
			ServletRequest req, 			// 过滤请求
			ServletResponse response, 		// 过滤响应  
			FilterChain chain				// 过滤器链
			) throws IOException, ServletException {
		// 做登录校验的预处理业务 
		// 需要注意的是,过滤器的Request对象是不再协议的,需要强制下转型
		HttpServletRequest request = (HttpServletRequest) req;
		//获得一个名为autologin的cookie,cookie本质上是一个键值对集合的数据结构
		Cookie[] cookies = request.getCookies();
		String autologin = null;
		// 如果cookie不空,则遍历cookie,业务上,如果拿到了自动登录信息,则初始化autologin
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			if ("autologin".equals(cookies[i].getName())) { // 用名来判断 
				autologin = cookies[i].getValue();			// 取值来用
				break;
			}
		}
		
		// 检查是否有登录信息
		if (autologin != null) {
			//做自动登录,【业务上,这个值是两个部分,即用户名和密码】
			String[] parts = autologin.split("-");
			String username = parts[0];
			String password = parts[1];
			//检查用户名和密码
			if ("NavyDagger".equals(username) && "123456".equals(password)) {
				//登录成功则将用户状态存入session域
				User user = new User();
				user.setUsername(username);
				user.setPassword(password);
				request.getSession().setAttribute("user", user);
			}
		}
				
		//做完预处理工作后,下一步是放行
		chain.doFilter(request, response);
	}

}

登录的控制器源码:

这里也应该设置有验证用户名和密码的过程,因为在用户首次登录的时候就需要在这里进行登录验证并封装cookie。

public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 获取参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		// 硬判断,写死了用户名和密码,思考真实的业务场境,如何取用户名和密码判断?
		if ("NavyDagger".equals(username) && "123456".equals(password)) {
			// 通过了检测,则封装数据
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			request.getSession().setAttribute("user", user);
			
			//发送自动登录的cookie,为以后的自动登录做准备
			String autoLogin = request.getParameter("autologin");
			if (autoLogin !=null) {
				// 封装用户名和密码,然后丢到cookie的某个键值中
				Cookie cookie = new Cookie("autologinnapa", username+"-"+password);
				// 设置cookie的有效时间
				cookie.setMaxAge(Integer.parseInt(autoLogin));
				// 设置路径
				cookie.setPath(request.getContextPath());
				// 添加cookie
				response.addCookie(cookie);
			}
			
			//Servlet的核心业务完成:通过登录校验,控制跳转到首页
			response.sendRedirect(request.getContextPath()+"/index.jsp");
		} else {
			//Servlet的核心业务完成:未通过登录校验,控制跳转回到登录页,并给出出错信息提示
			request.setAttribute("errorMsg", "用户名或密码错误");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}

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

}

登出控制器源码:

在用户登出账号时,因为在服务端不能直接删除掉客户端的cookie,所以只能够采用将cookie的保存时间设置为0,然后在客户端就会接收到该cookie已经过期才会删除cookie。

public class LogoutServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		// 移除session
		request.getSession().removeAttribute("user");
		// 创建cookie
		Cookie cookie = new Cookie("autologin", "msg");
		cookie.setPath(request.getContextPath());

		// 将cookie置为过期
		cookie.setMaxAge(0);
		response.addCookie(cookie);
		
		// 导航,重定向
		response.sendRedirect(request.getContextPath()+"/index.jsp");
	}

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

}

web.xml文件源码:

因为需要使用到过滤器,所以应该在web.xml文件中进行配置才行。

  <!-- 过滤器配置  -->
   <filter>
    <filter-name>AutoLoginFilter</filter-name>
    <filter-class>
  		filter.AutoLoginFilter
  	</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>AutoLoginFilter</filter-name>
    <url-pattern>/*</url-pattern>  
  </filter-mapping> 
  
  
  
  <!-- 登录业管控制,实现路由 -->
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>filter.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  
  <!-- 登出,即注销业务控制,实现路由 -->
  <servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>filter.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/LogoutServlet</url-pattern>
  </servlet-mapping>
</web-app>

登录页面和登录后页面的jsp源码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body style="text-align: left;">


<font color="#eeeeee">提示:NavyDagger:123456</font><br />
<form action="${pageContext.request.contextPath }/LoginServlet"  method="post">
	<table border="1" width="600px" cellpadding="0" cellspacing="0" align="center">
		<tr>
			<td height="30" align="center">用户名</td>
			<td><input type="text" name="username" />${errorMsg }</td>
		</tr>
		
		<tr>
			<td height="30" align="center">密码</td>
			<td><input type="text" name="password" " /></td>
		</tr>
		
		<tr>
			<td height="35" align="center">自动登录时间</td>
			<td>
				<input type="radio" name="autologin" value="${60*60*24*31 }"/>一个月
				<input type="radio" name="autologin" value="${60*60*24*31*3 }"/>三个月
				<input type="radio" name="autologin" value="${60*60*24*31*6 }"/>半年
				<input type="radio" name="autologin" value="${60*60*24*31*12 }"/>一年
			</td>
		</tr>

		<tr>
			<td height="30" align="center" colspan="2">
				<input type="submit" value="登录" />&nbsp;&nbsp;&nbsp;&nbsp;
				<input type="reset" value="重置" />
			</td>
		</tr>
		
	</table>
</form>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示登录用户信息</title>
</head>
<body>
	<br />
	<center>
		<h3>欢迎光临</h3>
	</center>
	<br />
	<br />
	<c:choose>
		<c:when test="${sessionScope.user==null }">
			<a href="${pageContext.request.contextPath }/login.jsp">用户登录</a>
		</c:when>
		<c:otherwise>
			欢迎您,${sessionScope.user.username }!
			<a href="${pageContext.request.contextPath }/LogoutServlet">注销</a>			
		</c:otherwise>
	</c:choose>
	<hr />
</body>
</html>

下面是演示流程:
首先运行登录页面并输入正确的用户名和密码并选择自动登录的时间:

 

 成功进入到欢迎页面中:

这个时候我们进入到开发者工具就可以看见我们的用户名和密码已经保存在浏览器的cookie中:
 

这个时候我们如果我们再次登录,就会将cookie中的数据进行验证,验证通过后就可以完成自动登录了。

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值