传统的会话认证使用的是session+cookie,前后端分离的项目,更多使用的是token。本文主要实现的token,基于eggjs+redis+jwt+cookie
先来了解最终实现的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) {// 时间:秒const created = Math.floor(Date.now() / 1000);// RSA私钥const cert = fs.readFileSync(path.resolve(__dirname, './rsa_private_key.pem'),);// token密钥// exp:自纪元以来的秒数const token = jwt.sign({data,exp: created + expired,},cert,{ algorithm: 'RS256' },);return token;},
写入redis比较简单,需要注意:
1、redis的过期时间跟token过期时间保持一致
2、下面的redisKey可以在token能找的到
app.redis.set(redisKey, token);app.redis.expire(redisKey, Expired);
注意的两点:
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验证通过,会返回加密的data数据 ,如{uuid:’xx-xx-xx-xx’,role:’admin’},根据这个数据 ,可以知道这个用户是谁(以前是服务端在session中记录,会消息服务器内存,现在是加密存到了客户端)
/*** 验证token* @param {*} token 登录token*/verifyToken(token) {// 公钥const cert = fs.readFileSync(path.resolve(__dirname, './rsa_public_key.pem'),);let res = '';try {const result = 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);}return res;}
**根据token中取到的uuid,查询redis
中间件部分关键代码
const result = verifyToken(token);const tokenKey = config.user.redisPrefix.login + result.uuid;const redisToken = await app.redis.get(tokenKey);// 服务器端可以找到tokenif (redisToken) {// 验证是否为最新的tokenif (token === redisToken) {const tokenExpired = await app.redis.ttl(tokenKey);// 如果token超过一半时间,重新续命if (tokenExpired < config.user.tokenExpired / 2) {// 重新生成tokenconst token = createToken({ uuid: result.uuid },config.user.tokenExpired,);// 更新redisapp.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,};await next();} else {// 登录失败移除cookieremoveCookie();ctx.status = 422;// 如果不是最新token,则代表用户在另一个机器上进行操作,需要用户重新登录保存最新tokenctx.body = {status: 1001,msg:'您的账号已在其他机器保持登录,如果继续将清除其他机器的登录状态',};}}
到这里OVER了,剩下的就是请求业务接口的问题了
CORS跨域问题要实现token机制,跨域问题是不得不解决的
eggjs可以使用egg-cors(https://www.npmjs.com/package/egg-cors)
// config/plugin.jscors: {enable: true,package: 'egg-cors',},
// config/config.default.jsconfig.cors = {origin: 'http://a.imqianduan.com',allowMethods: 'GET,POST,OPTIONS',// 要使用cookie,credentials一定要设置为truecredentials: true,// maxAge:6000, //seconds// 如果要使用自定义token,可以在下面设置allowHeaders: ['x-csrf-token'],};
nginx
直接上代码,自己去调整吧
server {listen 80;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';return 200;}proxy_pass http://127.0.0.1:7001;proxy_set_header Host $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_header Access-Control-Allow-Credentials 'true';proxy_connect_timeout 60;proxy_read_timeout 60;proxy_send_timeout 60;}}
公司新入手的Macbook m1最新款,使用NPM RUN DEV,你都可以去喝杯咖啡再回来,再看还在 build,怎么解决?
internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string‘, value)
for循环里使用await
gulp4构建报错: Error: EPERM: operation not permitted, mkdir
如何在VUE项目中添加stylelint,检查css,scss,less的语法问题,保证团队代码的规范统一
linux下如何安装nodejs
egg-validate默认是英文提示,如何改成中文?
传统的会话认证使用的是session+cookie,前后端分离的项目,更多使用的是token。本文主要实现的token,基于eggjs+redis+jwt+cookie
RSA加密的玩法
在package.json里设置了NODE_ENV在代码中判断不了
The 'typeof' Babel helper is used more than once in your code. It's strongly recommended that you use the "external-helpers" plugin or the "es2015-rollup" preset. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information
Unknown plugin &quot;external-helpers&quot; specified in
项目从svn迁到git了,遇到的一个坑:前端工程化打包,会给每个js\css\img文件名添加一个hash值,svn下这个一点问题没有,迁到git后,hash值在不同的电脑上总是不一样。那是什么原因呢?也许你还会发现,有些本来没有修改过的文件,在"git status"时也提示有修改,需要提交。
nodejs应用如果在服务端使用npm包转换jpg,png,gif为webp格式,大大缩小图片的大小
nodejs从8.15.0版本升级到10.15.0版本,gulp打包报错~gulp[2092]: src\node_contextify.cc:633: Assertion `args[1]->IsString()' failed.
npm ERR! Cannot read property &amp;#39;match&amp;#39; of undefined
ReferenceError: internalBinding is not defined
默认情况下,webpack-dev-server只可使用localhost或127.0.0.1访问,用局域网IP访问不了
mvvm项目总会有一些静态文件,如静态合同、协议等等,如何通过webpack来原样复制?
利用http-server搭建静态服务器,即浏览html文件用的~
webpack最小化打包lodash插件,如果全量打包的话,lodash会非常大
vue2.0 项目,利用webpack搭建mock api服务,用于本地调试数据
webpack打包,报"WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB)",记录一下
webpack 是一个现代 JavaScript 应用程序的静态模块打包器(static module bundler)。在 webpack 处理应用程序时,它会在内部创建一个依赖图(dependency graph),用于映射到项目需要的每个模块,然后将所有这些依赖生成到一个或多个bundle
网站是基于eggjs写的,某天突然报spawn tail ENOENT错误,难道'child_process'调用spawn问题?
用npm-check批量更新依赖,检测package.json文件,检测到可更新的文件,然后批量更新
nodejs 社区乃至 Web 前端工程化领域发展到今天,作为 node 自带的包管理工具的 npm 已经成为每个前端开发者必备的工具。但是现实状况是,我们很多人对这个nodejs基础设施的使用和了解还停留在: 会用 npm install 这里(一言不合就删除整个 node_modules 目录然后重新 install 这种事你没做过吗?)
如何发布一个npm模块到npmjs.com
nrm是一个npm源管理工具,利用它我们可以快速切换npm源,利用nrm还可以测试哪个源最快。如果哪个npm依赖安装失败,不防切换一下npm源试试
npm 教程
阮一峰老师的package.json文件详解