## 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()
}
}
}
~~~