eggjs + crypto-js 的 DES 加密实现重置密码接口?
2023-03-14 22:45:49 时间
实现过程
1.安装 crypto-js
npm install crypto-js -S
2.路由层 router.js 添加路径
// 重置用户密码 router.post('/api/user/resetPassword', verify_token, controller.user.resetPassword);
3.控制层 user.js 添加方法
'use strict'; const Controller = require('egg').Controller; const CryptoJS = require("crypto-js"); function DES_decrypt(decryptStr) { return CryptoJS.DES.decrypt( { ciphertext: CryptoJS.enc.Hex.parse(decryptStr) }, CryptoJS.enc.Utf8.parse("ABF"),// keyHex { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 } // option ).toString(CryptoJS.enc.Utf8); } class UserController extends Controller { // 重置密码 async resetPassword() { const { ctx, app } = this; try { // 0、获取用户输入的 oldPassword newPassword const { oldPassword, newPassword } = ctx.request.body; const old_password = DES_decrypt(oldPassword); const new_password = DES_decrypt(newPassword); console.log('解密--old-->', oldPassword, old_password) console.log('解密--new-->', newPassword, new_password) // 1、获取请求头 authorization 属性,值为 token const token = ctx.request.header.authorization; // 2、用 app.jwt.verify(token, app.config.jwt.secret),解析出 token 的值 const decode = await app.jwt.verify(token, app.config.jwt.secret); // 3、校验是否 token 可以,需要在鉴权中间件里加返回 if(!decode) return; // 4、根据用户名,在数据库查找相对应的id操作 const userInfo = await ctx.service.user.getUserByName(decode.username); // 5、校验是否通过 if (old_password !== userInfo.password) { ctx.body = { status: 400, desc: '原密码错误', data: null }; return; } // 6、通过 service 方法 resetPassword 修改 password 信息 const result = await ctx.service.user.resetPassword({ ...userInfo, password: new_password }); // 返回 token ctx.body = { status: 200, desc: '更新成功', data: null }; } catch (error) { ctx.body = { status: 500, desc: '更新失败', data: null } } } } module.exports = UserController;
4.服务层 user.js 添加方法
// 重置密码 async resetPassword(params) { const { app } = this; try { // 通过 app.mysql.update 方法更新 user 表, 通过 id 筛选用户 const result = await app.mysql.update('user', { ...params }, { id: params.id } ); return result; } catch(error) { console.log(error); return null; } }
5.测试接口
我们在apifox上面新增接口,然后进行测试
首先我们先登录拿到token,然后用 kaimo313
这个账号,密码是 123456
。修改为 123
.
先拿到 DES 加密字符串
'123': newPassword: "a50d7e4459c234f1" '123456': oldPassword: "d18bb870a7d5664f"
相关文章
- Amazon SageMaker Endpoint for built-in TFS模型推理优化
- 使用 Amazon Selling Partner API Guard 来进行安全审计使你的 SP-API 应用更合规
- 宣布推出统一软件开发服务 Amazon CodeCatalyst(预览版)
- 新功能 — 使用 Amazon EventBridge Pipes 在事件产生器和事件使用器之间创建点对点集成
- 新功能 – 使用适用于 IDP 的 Amazon Comprehend 处理 PDF、Word 文档和图像
- 推出 Amazon GameLift Anywhere – 在您自己的基础设施上运行游戏服务器
- 新增功能 – Amazon EC2 Hpc6id 实例针对高性能计算进行了优化
- 全新 AWS SimSpace Weaver – 在云端运行大规模空间模拟
- VPC Lattice 简介 — 简化服务到服务通信的联网(预览版)
- AWS Marketplace 供应商详情 – 简化第三方软件风险评估
- python super()
- 新功能:Amazon CloudWatch 跨账户可观察性
- AWS Application Migration Service 的主要更新:新的迁移服务器分组、更新的启动和启动后模板
- 全新 —— 正式推出 Amazon EFS Elastic Throughput
- 新的 Amazon QuickSight API 功能可加速商业智能转型
- Amazon Inspector 现可对 AWS Lambda 函数进行漏洞扫描
- 宣布推出 AWS KMS External Key Store (XKS)
- 新功能 — 使用 Lambda SnapStart 为您的 Lambda 函数提速
- 新功能 — Amazon S3 多区域接入点的失效转移控制
- AWS Control Tower 新增功能 – 全面控制管理(预览版)