## 1.数据库获取ip
~~~
//middleware/forbidip.js
module.exports = (options, app) => {
return async function forbidIp(ctx, next) {
//要屏幕的ip 1.从数据库获取 2.从参数传入
//要屏蔽的ip
let forbidip = '127.0.0.1';
// 获取客户端的ip
// console.log(ctx.request.ip)
if (ctx.request.ip = forbidip) {
ctx.status = 403;
ctx.body = "您的ip禁止访问"
} else {
await next()
}
}
}
~~~
~~~
//middleware/forbidip.js
//config/config.default.js中配置
config.middleware = ['printdate','forbidip'];
~~~
## 2.参数传入
~~~
//config/config.default.js中配置 要传递的参数
config.middleware = ['printdate','forbidip'];
config.forbidip = {
forbidip:'127.0.0.1'
}
~~~
~~~
module.exports = (options, app) => {
return async function forbidIp(ctx, next) {
//要屏幕的ip 从参数传入
console.log(options)
if (ctx.request.ip = options.forbidip) {
ctx.status = 403;
ctx.body = "您的ip禁止访问"
} else {
await next()
}
}
}
~~~