实现登录和认证

使用cookie-parser

cookie-parser 是一个用于解析 HTTP 请求中的 Cookie 的中间件,常用于 Node.js 的 Express 框架中。它能够解析请求头中的 Cookie 字段,并将其转换为 JavaScript 对象,方便开发者访问和操作。

加入cookie-parser中间件

js 复制代码
const cookieParser = require("cookie-parser");
app.use(cookieParser(yunjin));//加入密钥

加入之后,会在req对象中注入cookies属性,用于获取所有请求传递过来的cookie

加入之后,会在res对象中注入cookie方法,用于设置cookie

原来的写法

js 复制代码
ter.post(
  "/login",
  synchandler(async (req, res) => {
    const result = await adminServ.login(req.body.loginId, req.body.loginPwd);
    if (result) {
      // 登录成功
      res.header("set-cookie", `token=${result.id}; path=/; domain=localhost; max-age=3600`);
    }
    return result;
  }
));

加入cookie-parser组件之后

js 复制代码
ter.post(
  "/login",
  synchandler(async (req, res) => {
    const result = await adminServ.login(req.body.loginId, req.body.loginPwd);
    if (result) {
      // 登录成功
      res.cookie("token",result.id,{
        path:"/",
        domain:"localhost",
        maxAge:1000*60*60*24*7,//毫秒数
      });
    }
    return result;
  }
));

登录成功后给予token

通过cookie给予适配的浏览器,但是给适配的其他终端的通过header给予的

改进

js 复制代码
ter.post(
  "/login",
  synchandler(async (req, res) => {
    const result = await adminServ.login(req.body.loginId, req.body.loginPwd);
    if (result) {
      const value = result.id;
      // 登录成功
      res.cookie("token",value,{
        path:"/",
        domain:"localhost",
        maxAge:1000*60*60*24*7,//毫秒数
      });
      res.header("authorization", value);
    }
    return result;
  }
));

对后续请求进行认证

后续每次请求验证的时候先解析cookie或header中的token,然后验证token,通过则继续后续处理,不通过则给予错误 tokenMiddle.js认证检验

js 复制代码
const {getErr}=require("./getSendResult")
const {pathToRegexp}=require("path-to-regexp");
const crypt=require("./util/crypt");
const needTokenApi = [
  {method:"POST",path:"/api/student"},
  {method:"PUT",path:"/api/student/:id"},
];

//解析token
module.exports = async (req, res, next) => {
  const apis=needTokenApi.filter((api)=>{
    const reg=pathToRegexp(api.path);
    return api.method===req.method&&reg.text(req.path);
  });
  if(apis.length===0){
    next();
    return;
  }
  let token =req.cookies.token;
  if(!token){
    //从header中的authorization中获取
    token=req.header.authorization;
  }
  if(!token){
    //没有认证
    handleNonToken(req,res,next);
    return;
  }
  const userId=crypt.decrypt(token);
  req.userId=userId;
  next();
};
//处理没有认证的情况
function handleNonToken(req,res,next){
  res
  .status(403)
  .send(getErr("no token",403));
}

init.js使用

js 复制代码
const {asyncHandler}=require('../orm/api/getSendResult')
const crypt=require("./util/crypt")
router.post(
  "/login",
  asyncHandler(async (req, res) => {
    const result = await adminServ.login(req.body.loginId, req.body.loginPwd);
    if (result) {
      let value = result.id;
      value=crypt.encrypt(value.toString());
      // 登录成功
      res.cookie("token",value,{
        path:"/",
        domain:"localhost",
        maxAge:1000*60*60*24*7,//毫秒数
        signed:true,
      });
      res.header("authorization", value);
    }
    return result;
  }
));
module.exports = router

crypt.js手动写加密和解密

js 复制代码
//使用对称加密算法:aes 128
//128位的密钥
const secret = Buffer.from("mm7h3ck87ugk914a");
const crypto = require("crypto");
//准备一个iv,随机变量
const iv = Buffer.from(
  //写法
    // Math.random().toString(36).slice(-8) + Math.random().toString(36).slice(-8)
    "jxkvxz9704u3m8c4"
);

exports.encrypt = function (str) {
    const cry = crypto.createCipheriv("aes-128-cbc", secret, iv);
    let result = cry.update(str, "utf-8", "hex");
    result += cry.final("hex");
    return result;
};

exports.decrypt = function (str) {
    const decry = crypto.createDecipheriv("aes-128-cbc", secret, iv);
    let result = decry.update(str, "hex", "utf-8");
    result += decry.final("utf-8");
    return result;
};

getSendResult.js处理方法

js 复制代码
exports.getErr = function (err = "server internal error", errCode = 500) {
  return {
      code: errCode,
      msg: err,
  };
};

exports.getResult = function (result) {
  return {
      code: 0,
      msg: "",
      data: result,
  };
};

exports.asyncHandler = (handler) => {
  return async (req, res, next) => {
      try {
          const result = await handler(req, res, next);
          res.send(exports.getResult(result));
      } catch (err) {
          next(err);
      }
  };
};
相关推荐
星马梦缘1 天前
数据库作战记录1
数据库·sql·mysql
lbh1 天前
当我开始像写代码一样和AI对话,一切都变了
前端·openai·ai编程
We་ct1 天前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
鸡蛋灌Bean1 天前
MySQL优化系列
数据库·mysql
wefly20171 天前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
C澒1 天前
微前端容器标准化 —— 公共能力篇:通用打印
前端·架构
德育处主任Pro1 天前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
木斯佳1 天前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
陈天伟教授1 天前
人工智能应用- 预测新冠病毒传染性:04. 中国:强力措施遏制疫情
前端·人工智能·安全·xss·csrf
zayzy1 天前
前端八股总结
开发语言·前端·javascript