Cookie是Web程序中常用的一种会话跟踪技术,实际是客户端浏览器保存的一小段文本信息。Cookie在实际应用中很常见,比如很多网站登录时的“记住我”功能、电子商务网站的“购物车”功能……Session作为另一个常用的会话技术,常与Cookie拿来比较,文末给链接作扩展介绍。下面主要介绍下Cookie的使用:
1. Cookie的有效期设置:setMaxAge(int maxAge)方法。如果maxAge为正数,表示maxAge秒后该Cookie失效;如果maxAge为负数,表示该Cookie只在本浏览器打开的窗口及其窗口内有效,关闭窗口即失效,Cookie的默认有效期为-1,即关闭即失效;如果maxAge为0,表示即时失效,即删除该Cookie。
2. Cookie的修改删除:Cookie没有提供相应的修改删除API,需要修改需要同名Cookie进行覆盖,需要删除则把有效期设置为0即可。
3. Cookie的跨域性:Cookie是不可跨域的,为了隐私安全,不同域名间不可相互访问。不过同级域名下的Cookie可以通过setDomain()方法实现互访。例如:cookie.setDomain(".baidu.com")。
4. Cookie的访问路径:Cookie可以通过setPath()方法设置该Cookie只允许哪个路径下的程序访问。例如cookie.setPath("/")表示允许所有路径下的程序都可以访问。
5. Cookie的安全设置:Cookie可以通过setSecure()方法,把值设置为true则表示浏览器只会在HTTPS和SSL等安全协议中传输此Cookie。
6. Cookie是否允许客户端脚本读取:Cookie可以通过setHttpOnly()方法,设置为true则表示不允许客户端脚本读取Cookie,比如JavaScript。这个可以在一定程度上避免跨站脚本攻击。
介绍得差不多了,下面直接上示例。
示例主要简单模拟登录场景常见的“记住我”功能,实际应用中肯定比这个示例复杂得多,还有很多安全性的考虑。比如将Cookie中存的数据进行加密或者将用户的“记住”信息存在数据库中。下面为了演示用法只做简单处理。
一、登录处理类
package com.research.spring.controller;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.research.spring.model.UserInfo;
/**
* 登录控制器
* @author wdmcygah
*
*/
@Controller
@RequestMapping("/login")
public class LoginController {
//这里模拟数据库或其它地方里面存放的数据
private static ConcurrentHashMap<String, String> userMap = new ConcurrentHashMap<String, String>();
@RequestMapping("/show")
public String login( HttpServletRequest request,Model model){
String name = getRememberedName(request);
if( null != name ){
model.addAttribute("status", "2");
model.addAttribute("userName", name);
}
return "/login/show";
}
@RequestMapping("/loginIn")
public String loginIn( UserInfo info, boolean remember, HttpServletRequest request, HttpServletResponse response,Model model){
if( "admin".equals(info.getUserName()) && "admin".equals(info.getPassword()) ){
if( remember ){
rememberUser(request, response, info);
}
model.addAttribute("status", "1");
model.addAttribute("userName", info.getUserName());
return "/login/show";
}else{
model.addAttribute("status", "-1");
return "/login/show";
}
}
@RequestMapping("/loginOut")
public String loginOut( String userName, HttpServletResponse response,Model model){
if( null != userName ){
//删除缓存
userMap.remove(userName);
//删除Cookie数据
removeCookie(userName, response);
}
return "/login/show";
}
/**
* 删除userName对应的Cookie值(实际是把有效期设置为0)
* @param userName
* @param response
*/
private void removeCookie(String userName, HttpServletResponse response) {
Cookie cookie = new Cookie(userName,"remove");
//有效期为0即为删除,同名cookie会进行覆盖
cookie.setMaxAge(0);
response.addCookie(cookie);
}
/**
* 记住用户
* @param requset
* @param response
* @param info
*/
private void rememberUser(HttpServletRequest request, HttpServletResponse response, UserInfo info) {
//这里只是模拟,实际情况可能需要对值进行算法加密
String cName = info.getUserName();
String cValue = info.getUserName() + new Date().getTime();
Cookie cookie = new Cookie(cName,cValue);
//有效期30分钟
cookie.setMaxAge(30*60);
response.addCookie(cookie);
//数据缓存到内存中
userMap.put(cName, cValue);
}
/**
* 得到记住的用户名
* @param request
* @param response
*/
private String getRememberedName(HttpServletRequest request) {
Cookie [] cookies = request.getCookies();
if( (null==cookies) || (0==cookies.length) ){
return null;
}
String cName = null;
String cValue = null;
for( Cookie c : cookies ){
cName = c.getName();
cValue = c.getValue();
//用户名及对应的值与缓存中的相同,则认为是记住的用户
if( (cValue!=null) && ((cName!=null)) && (cValue.equals(userMap.get(cName)))) {
return cName;
}
}
return null;
}
}
二、登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Bootstrap -->
<link href="<%=request.getContextPath()%>/statics/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<form action="loginIn.htm" method="post" id="loginForm"
class="form-horizontal" role="form">
<fieldset class="text-center">
<legend> 使用登录样例演示Cookie用法</legend>
<c:choose>
<c:when test="${status=='1'}">
<div class="alert alert-success" role="alert">登录成功!
<input type="hidden" id="userName" value="${userName}" />
</div>
<button type="button" class="btn btn-primary btn-large"
id="loginOutButton">注销</button>
<button type="button" class="btn btn-primary btn-large"
οnclick="window.location.href='show.htm' ">返回</button>
</c:when>
<c:when test="${status=='2'}">
<div class="alert alert-success" role="alert">这次是因为记住用户登录成功的!
<input type="hidden" id="userName" value="${userName}" />
</div>
<button type="button" class="btn btn-primary btn-large"
id="loginOutButton">注销</button>
<button type="button" class="btn btn-primary btn-large"
οnclick="window.location.href='show.htm' ">返回</button>
</c:when>
<c:when test="${status=='-1'}">
<div class="alert alert-danger" role="alert">用户名或密码错误!</div>
<button type="button" class="btn btn-primary btn-large"
οnclick="window.location.href='show.htm' ">返回</button>
</c:when>
<c:otherwise>
<div class="form-group">
<label class="col-sm-4 control-label" for="name">用户名</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="userName"
id="userName">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="password">密码</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="password"
id="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-8">
<input type="checkbox" name="remember"
id="remember">
<label class="control-label" for="remember">记住我</label>
</div>
</div>
<div class="form-actions ">
<button type="submit" class="btn btn-primary btn-large">提交</button>
<button type="reset" class="btn">取消</button>
</div>
</c:otherwise>
</c:choose>
</fieldset>
</form>
</div>
<!-- js -->
<script
src="<%=request.getContextPath()%>/statics/js/import/jquery-1.11.1.js"></script>
<script
src="<%=request.getContextPath()%>/statics/js/import/bootstrap.min.js"></script>
<script
src="<%=request.getContextPath()%>/statics/js/my/login.js"></script>
</body>
</html>
代码在JDK1.8、chrome浏览器下测试通过。测试链接:http://localhost:8080/spring/login/show.htm,登录账号密码:admin/admin。
若想查看完整源码,可以查看我的Github仓库:https://github.com/wdmcygah/research-spring。其中仓库里面有些与本博文不相关的代码,注意区分。
扩展介绍:
1. 在网上看到的一篇非常好的介绍Cookie与Session的文章:Cookie/Session机制详解
2. Cookie编辑器:Edit this Cookie。这里提供一个Chrome插件下载地址:http://pan.baidu.com/share/link?shareid=648188478&uk=637880896&app=zd,可以直接在浏览器端查看编辑Cookie,可以算做一个可视化Cookie管理器吧,挺好用的。