1.新建一个jwttest.router.js
引入toast
const {success,fail} = require("../toast");
const jwt = require("jsonwebtoken");
const key="web";
module.exports = function (router){
router.post("/login",async ctx=>{
const {name,pwd} = ctx.request.body;
if(name==="admin" && pwd==="123456"){
const token = jwt.sign({name:"admin"},key,{expiresIn:300})
return success(ctx, "Bearer " + token);
}else{
return fail(ctx,"账号和密码不正确");
}
}).get("/data",async ctx=>{
return success(ctx,{});
})
};
在app.js中引入jwttest
// 引入类
const Koa =require('koa');
// 引入koa-router
const Router =require('koa-router');
// 引入koa-body
const koaBody=require('koa-body');
const tagRouter =require("./routers/tag.router");
const userRouter =require("./routers/user.router");
const contentRouter =require("./routers/content.router");
const jwtRouter =require("./routers/jwttest.router");
const {fail} = require("./toast");
const jwt = require("jsonwebtoken");
const key="web";
// 创建对象
const app =new Koa();
app.use(koaBody({
strict:false
}));
app.use(async (ctx,next) =>{
const exp = [
'/login',
'/',
'register',
]; //不需要验证的地址
if(exp.includes(ctx.url)){
await next();
return;
}
console.log(ctx.headers)
const authorization = ctx.headers.authorization;
if(!authorization){
return fail(ctx,"请添加 token 信息");
}
const token = authorization.split(' ')[1];
try{
const user =jwt.verify(token,key);
if(user.name==="admin"){
await next();
}else{
return fail(ctx,"权限不足");
}
}catch(error){
return fail(ctx,error);
}
})
const router =new Router({// 可以传递参数配置路由前缀
});
tagRouter(router);
userRouter(router);
contentRouter(router);
jwtRouter(router);
app.use(router.routes());
app.use(router.allowedMethods({
}));
// localhost:3000
app.listen(8000,()=>{
console.log("http://localhost:8000");
});
最后通过text.http查看是否显示正确
@url =http://localhost:8000
@json=Content-Type: application/json
@auth=Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2Mzg1MjA5MDAsImV4cCI6MTYzODUyMTIwMH0.PAvCI6TsrzYs7_P4e4v_ieLlLJY-iT0ro42oTYFhV1Q
### 登录
POST {{url}}/login
{{json}}
{
"name":"admin",
"pwd":"123456"
}
### 获取数据
GET {{url}}/data
{{auth}}
### 查询
GET {{url}}/tag
{{auth}}
### 添加
POST {{url}}/tag
{{auth}}
{{json}}
#content
//表单方式
# id=1000&name="张三"
{
"text":"6234"
}
### 删除
DELETE {{url}}/tag
{{auth}}
{{json}}
{
"_id":"61a887ada8457be730ee21d6"
}
### content 查询
GET {{url}}/content
{{auth}}
### content 添加
POST {{url}}/content HTTP/1.1
{{auth}}
{{json}}
{
"title":"孤独的根号三",
"content":"一个孤独的根号三七八九"
}
### 删除
DELETE {{url}}/content
{{auth}}
{{json}}
{
"_id":"61a889f2116f2745d91ff7e6"
}
### 修改
PUT {{url}}/content?_id=61a88dcf1509ffac77824c93
{{auth}}
{{json}}
{
"top":"true"
}
### user 查询
GET {{url}}/user HTTP/1.1
{{auth}}
### user 添加
POST {{url}}/user HTTP/1.1
{{auth}}
{{json}}
{
"name":"小明",
"password":"12233yfr",
"email":"1637743640@qq.com",
"namegrop":"限制会员"
}
### user删除
DELETE {{url}}/user
{{auth}}
{{json}}
{
"_id":"61a88c540d6c4ff5952338c6"
}
### user修改
PUT {{url}}/user?_id=61a88df389ca9f8a670616ef
{{auth}}
{{json}}
{
"name":"小红"
}