[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>JWT基础鉴权
Web安全是Web应用中非常重要的一环,主要由后端和服务器承担安全保障
面对请求源,后端有着各种各样的鉴权机制: session,cookie,token,jwt,OAuth,OAuth2,api-key,signature…
本节以jwt为例,演示一个极简的token鉴权
<>准备工作
* 拷贝第一节HelloWorld项目
* 安装一种jwt依赖(本节使用jsonwebtoken)
<>实践
接下来我们使用jsonwebtoken来实现最常见的登录鉴权,登录成功后返回一个token,之后凭借这个token去访问另外的路由
* 首先引入jwt依赖 const jwt = require('jsonwebtoken').default;
* 定义一个密钥
之后我们会利用这个密钥生成token,和检验token
const secret = 'mysecretkey';
* 写个登录时生成token的函数
传入用户名和密码,检验密码,正确就生成一个token
生成token: jwt.sign(标志,密钥,选项(生命周期等))
function getToken(user) { let token = null; const payload = { uname: user.uname
, }; if (user.uname == 'root' && user.passwd == 'root') { token = jwt.sign(
payload, secret, { expiresIn: '1h' }); } return token; }
* 写一个访问时检验token的函数
传入req,res,next,从请求头中取出名字为’token’的一个token,若请求头里不带token则返回请求报告没token,token校验通过了则放行
function checkToken(req, res, next) { const token = req.headers['token']; if (!
token) return res.status(401).json({ message: 'No token provided.' }); jwt.
verify(token, secret, (err, decoded) => { if (err) return res.status(500).json({
message: 'Failed to authenticate token.' }); req.userId = decoded.id; next(); })
; }
* 编写登录接口 app.post('/login', (req, res) => { let user = req.body; let token =
getToken(user); if(token!=null) { res.send(token); } else { res.status(401).send
("wrong"); } });
* 编写一个需要校验token的接口
将token检验函数作为中间件挂载到 /root 路由上
app.get('/root', checkToken, (req, res) => { res.send('hello root user!'); });
<>接口测试
尝试用api调试工具先访问 login 接口,利用正确的用户名和密码获取token,然后用得到的token去访问 root 接口
<>下一节-全局错误处理