技术标签: php jwt redis实现
传统的会话认证使用的是session+cookie,前后端分离的项目,更多使用的是token。本文主要实现的token,基于eggjs+redis+jwt+cookie
token会话流程图
先来了解最终实现的token会话流程图
对几个关键流程,做以下记录
服务端使用JWT生成token
在使用JWT前,需要提前准备公钥/私钥,怎么生成密钥,可参考:https://www.imqianduan.com/server/RSA-public-private.html
nodejs生成token,使用jsonwebtoken(https://www.npmjs.com/package/jsonwebtoken)
代码
/**
* 生成token
* @param {*} data 数据(eg:{uuid: 'xxx-xx-xx-xxx',role:'admin'})
* @param {*} expired token失效时间
*/
createToken(data,expired){
// 时间:秒
constcreated=Math.floor(Date.now()/1000);
// RSA私钥
constcert=fs.readFileSync(
path.resolve(__dirname,'./rsa_private_key.pem'),
);
// token密钥
// exp:自纪元以来的秒数
consttoken=jwt.sign(
{
data,
exp:created+expired,
},
cert,
{algorithm:'RS256'},
);
returntoken;
},
服务端写入redis
写入redis比较简单,需要注意:
1、redis的过期时间跟token过期时间保持一致
2、下面的redisKey可以在token能找的到
app.redis.set(redisKey,token);
app.redis.expire(redisKey,Expired);
服务端写入cookie
注意的两点:
1、设置cookie的httpOnly为true
2、maxAge跟token和redis的过期时间一致
3、使用eggjs,可以使用encrypt加密
ctx.cookies.set(config.user.tokenName,tokenData[config.user.tokenName],{
httpOnly:true,
maxAge:tokenExpired*1000,
encrypt:true,
domain:'imqianduan.com',
});
ctx.cookies.set('x-cid',1,{
httpOnly:false,
signed:false,
maxAge:tokenExpired*1000,
domain:'imqianduan.com',
});
前端记录登录状态
前面服务端已经把token写入了cookie中,并设置了httpOnly为true,所以JS已经不能读取这个token了,这也是为什么token不能localstorage存储的原因之一
上面一步中,我们还在cookie中写了一个x-cid的cookie,我们可以通过它来判断是否已经登录
前端发送业务请求
在前端使用最多的框架react/vue中,axios和fetch是最常见的发送http请求的方法之一,但axios和fetch默认都是不携带cookie的,要携带cookie,都是要进行一定的设置
fetch
设置credentials为include
credentials:'include'
axios
设置withCredentials为true
axios.defaults.withCredentials=true;
服务端中间件验证token
如果token验证通过,会返回加密的data数据 ,如{uuid:’xx-xx-xx-xx’,role:’admin’},根据这个数据 ,可以知道这个用户是谁(以前是服务端在session中记录,会消息服务器内存,现在是加密存到了客户端)
/**
* 验证token
* @param {*} token 登录token
*/
verifyToken(token){
// 公钥
constcert=fs.readFileSync(
path.resolve(__dirname,'./rsa_public_key.pem'),
);
letres='';
try{
constresult=jwt.verify(token,cert,{algorithms:['RS256']})||{};
const{exp}=result,
current=Math.floor(Date.now()/1000);
if(current<=exp){
res=result.data||{};
}
}catch(e){
console.log(e);
}
returnres;
}
**根据token中取到的uuid,查询redis
中间件部分关键代码
constresult=verifyToken(token);
consttokenKey=config.user.redisPrefix.login+result.uuid;
constredisToken=await app.redis.get(tokenKey);
// 服务器端可以找到token
if(redisToken){
// 验证是否为最新的token
if(token===redisToken){
consttokenExpired=await app.redis.ttl(tokenKey);
// 如果token超过一半时间,重新续命
if(tokenExpired
// 重新生成token
consttoken=createToken(
{uuid:result.uuid},
config.user.tokenExpired,
);
// 更新redis
app.redis.set(tokenKey,token);
app.redis.expire(tokenKey,config.user.tokenExpired);
// 重置cookie值
ctx.cookies.set(config.user.tokenName,token,{
httpOnly:true,
maxAge:config.user.tokenExpired*1000,
encrypt:true,
domain:'imqianduan.com',
});
}
// 存储用户信息到ctx,便于controller获取
ctx.userInfo={
uuid:result.uuid,
};
awaitnext();
}else{
// 登录失败移除cookie
removeCookie();
ctx.status=422;
// 如果不是最新token,则代表用户在另一个机器上进行操作,需要用户重新登录保存最新token
ctx.body={
status:1001,
msg:
'您的账号已在其他机器保持登录,如果继续将清除其他机器的登录状态',
};
}
}
结束
到这里OVER了,剩下的就是请求业务接口的问题了
关于CORS跨域问题
要实现token机制,跨域问题是不得不解决的
eggjs可以使用egg-cors(https://www.npmjs.com/package/egg-cors)
// config/plugin.js
cors:{
enable:true,
package:'egg-cors',
},
// config/config.default.js
config.cors={
origin:'http://a.imqianduan.com',
allowMethods:'GET,POST,OPTIONS',
// 要使用cookie,credentials一定要设置为true
credentials:true,
// maxAge:6000, //seconds
// 如果要使用自定义token,可以在下面设置
allowHeaders:['x-csrf-token'],
};
nginx
直接上代码,自己去调整吧
server{
listen80;
server_name a.imqianduan.com;
location/{
# add_header 'Access-Control-Allow-Origin' "http://a.imqianduan.com";
# add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
# add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
# add_header 'Access-Control-Allow-Credentials' 'true';
# add_header 'Access-Control-Expose-Headers' 'x-auth-token';
if($request_method='OPTIONS'){
add_header'Access-Control-Allow-Origin'"http://a.imqianduan.com";
add_header'Access-Control-Allow-Methods''GET, POST, PUT, DELETE, OPTIONS';
add_header'Access-Control-Allow-Headers''DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,x-csrf-token';
add_header'Access-Control-Allow-Credentials''true';
#add_header 'Access-Control-Expose-Headers' 'x-auth-token';
return200;
}
proxy_pass http://127.0.0.1:7001;
proxy_set_headerHost$host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerAccess-Control-Allow-Credentials'true';
proxy_connect_timeout60;
proxy_read_timeout60;
proxy_send_timeout60;
}
}
转载:盘古:[阿里云](https://l.gushuji.site/aliyun)飞天分布式存储系统设计深度解析盘古是什么?上图列举了目前主流的云计算厂商,我们发现一个很有趣的事情:所有云计算厂商都是“富二代”,它们的分布式存储技术全部采用自研技术,而没有用大家耳熟能详的开源分布式系统。飞天梦第一代飞天人的梦想是在大量廉价的PC服务器上,对外提供各种计算和存储服务。具体到以下几个组件:夸父,主要负责网络;女娲,主要负责协同;伏羲,主要负责调度;盘古,主要负责存储;神农,主要负责监控。
来源:Caffe源码(一):math_functions 分析主要函数math_function 定义了caffe 中用到的一些矩阵操作和数值计算的一些函数,这里以float类型为例做简单的分析1. caffe_cpu_gemm 函数:template<>void caffe_cpu_gemmfloat>(const CBLAS_TRANSPOSE TransA,
深入理解Spark RDD抽象模型和编写RDD函数Spark revolves around the concept of a resilient distributed dataset (RDD), which is an immutable , fault-tolerant , partitioned collection of elements that can be opera
AI on the Jetson Nano LESSON 1: Getting Started for Absolute Beginnerssudo apt-get updatesudo visudoicir ALL=(ALL) NOPASSWD:ALL首先 添加dns服务器vi /etc/resolv.conf#在文件中添加如下两行:nameserver 8.8.8.8nameserver 8.8.4.4更新系统时间# 首先安装 ntpdate 命令:sudo apt-get
在初始化git之后(gitinit),同时在github建立好仓库之后,本地也新增了sshkye(ssh-keygen -trsa -C ‘mailaddress’),同时也在本地新增了远程仓库(gitremoteaddorigin github地址),但是在gitpush的时候出现错误 The authenticity of host 'github.com (1...
MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前noSql数据库产品中最热门的一种。它在许多场景下用于替代传统的关系型数据库或键值对存储方式,MongoDB是用C++开发,MongoDB的官方网址为http://www.mongodb.org/1、为什么要用 NoSQL1.1 NoSQL 简介 NoSQL,全称是”Not Only Sql”,指的是非关系型的数据库。这类数据库主要有这些特点:非关系型的、分布式的、开源的、水平可扩展的。原始的目的是为了大规模.
字典的get()方法用来替换d['key'],后者如果是遇到key不存在会有异常,如果使用的d.get('key'),key不存在时它返回的是None,你可以指定两个参数如:d.get('key',0)来用0取代返回的Nonesum[value] = sum.get(value, 0) + 1还有一个类似的方法setdefault(key, value),如果字典中存在key,那么就直接返回d[...
目录1. 编程 实现一个单链表的创建1. 编程 实现一个单链表的创建
Gitlab+jenkins环境部署主机名:主机名IP:服务部署node4192.168.1.14程序员主机node5192.168.1.15Gitlab服务器node6192.168.1.16Jenkins服务器node4:程序员电脑#下载git和安装二级命令补全功能软件yum -y install git yum -y inst...
git 开发笔记(三) 1.创建分支 首先,我们创建dev分支,然后切换到dev分支:$ git checkout -b dev Switched to a new branch ‘dev’ git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:gitbranchdev git branch dev git checkout dev Switched to bra
位运算作为底层的基本运算操作,往往是和'高效'二字沾边,适当的运用位运算来优化系统的核心代码,会让你的代码变得十分的精妙。以下是我所遇之的一些简单的位运算技巧作为博文记录。1.获得int型最大值 public static void main(String[] args) { int maxInt = (1 << 31) - 1; ...