储存在用户本地终端上的数据 为了辨别用户身份、进行跟踪而储存在用户本地终端上的数据(通常经过加密)。
客户端第一次访问服务器的时候服务器通过响应头向客户端发送Cookie,属性之间用分号空格分隔
var http = require('http');
http.createServer(function (req,res) {
if(req.url=='/write'){
res.setHeader('Set-Cookie',['name=zfpx; domain=.zfpx.cn','age=7']);
res.end('write');
}
}).listen(8080);
客户端发送的cookie,属性之间用分号空格分隔
var http = require('http');
var querystring = require('querystring')
http.createServer(function (req,res) {
if(req.url=='/read'){
//"name=zfpx; age=7"
res.end(JSON.stringify(querystring.parse(req.headers['cookie'],'; ')));
}
}).listen(8080);
属性 | 含义 |
---|---|
name=value | 多组以数组形式,存储key/value |
Path | 默认是/,匹配到后发送cookie |
secure | 当 secure 值为 true 时,cookie 在 HTTP 中是无效,在 HTTPS 中才有效 |
Domain | 默认是当前域名,可以用.表示任意 |
maxAge | 最大失效时间(秒),设置在多少秒后失效 |
Expires | 过期时间(秒),在设置的某个时间点后该 Cookie 就会失效,如 expires=(new Date(Date.now()+4000)).toGMTString() |
httpOnly | 如果在COOKIE中设置了httpOnly属性,则通过程序(JS脚本)将无法读取到COOKIE信息,防止XSS攻击产生 |
$ npm install cookie-parser
res.cookie(name,value,[options]);
req.cookies
var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
app.use(cookieParser());
app.get('/write', function (req,res) {
res.cookie('name','zfpx');
res.send('');
});
app.get('/read', function (req,res) {
res.clearCookie('name');//清除cookie
res.send(req.cookies);
});
function cookieParser(){
return function (req,res,next) {
var arr = [];
res.cookie = function (name,value,options) {
arr.push(`${name}=${value}`);
res.setHeader('Set-Cookie',arr);
}
req.cookies = querystring.parse(req.headers['cookie'],'; ')
next();
}
}
app.use(cookieParser());
options选项
res.cookie('name','zfpx',{domain:'a.zfpx.cn'});
res.cookie('name','zfpx',{path:'/read'});
res.cookie('name','zfpx',{expires:new Date(Date.now()+4000)});
res.cookie('name','zfpx',{maxAge:2000});
res.cookie('name','zfpx',{httpOnly:true});
res.cookie('name','zfpx',{secure:true});
通过crypto来进行加密
md5加密(散列算法)
var crypto = require('crypto');
console.log(crypto.getHashes());
var md5 = crypto.createHash('md5');
var result = md5.update('hello').digest('hex');
console.log(result);
HMAC(散列算法与密钥结合)
var crypto = require('crypto');
var hmac = crypto.createHmac('sha256','jw');
var md5Sum = hmac.update('hello').digest('hex');
console.log(md5Sum);
var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
app.use(cookieParser('jw'));
app.get('/visit', function (req,res) {
var visit = req.signedCookies.visit;
if(visit){
visit++;
}else{
visit = 1;
}
res.cookie('visit',visit,{signed:true});
res.send(`你是${visit}次访问`);
});
app.listen(8080);
function sha256(value,flag){
var crypto = require('crypto');
return value+'.'+crypto.createHmac('sha256',flag).update(value).digest('base64').replace(/\=/g,'');
}
var express = require('express');
var app = express();
var path = require('path');
app.set('view engine','html');
app.set('views',path.resolve('views'));
app.engine('html',require('ejs').__express);
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended:true}));
var flag = true;
app.get('/login', function (req,res) {
res.render('login',{title:'登陆',flag:flag})
});
app.post('/login', function (req,res) {
if(req.body.username){
res.cookie('username',req.body.username);
flag = false;
res.redirect('/user');
}else{
res.redirect('back');
}
});
app.get('/user', function (req,res) {
if(req.cookies.username){
res.render('user',{title:'用户列表',username:req.cookies.username})
}else{
res.redirect('/');
}
});
app.get('/', function (req,res) {
res.render('index',{title:'首页'})
});
app.get('/logout', function (req,res) {
res.clearCookie('username');
flag=true;
res.redirect('/login');
});
app.listen(8080);
服务器端设置cookie
var express = require('express');
var app = express();
var path = require('path');
app.set('view engine','html');
app.set('views',path.resolve('views'));
app.engine('html',require('ejs').__express);
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended:true}));
app.get('/', function (req,res) {
res.sendFile('./login.html',{root:__dirname});
});
app.post('/login', function (req,res) {
if(req.body.username == 'admin'){
res.cookie('username',req.body.username);
res.cookie('password','hello');
res.send('登陆成功');
}else{
res.redirect('back');
}
});
app.listen(8080);
客户端根据设置的cookie自动登录
window.onload = function () {
var cookies = document.cookie.split('; ');
var bb = cookies.map(item=>{
var arr = item.split('=');
return {name:arr[0],value:arr[1]}
}).find(item=>{
return 'username' ==item.name;
});
username.value=bb.value;
myform.submit();
}