CSRF安全机制
1、Egg 安全机制
CSRF 的防范 http://eggjs.org/zh-cn/core/security.html
2、Egg Post 提交数据
1. 提交数据
<form action="/news/doAdd?_csrf=<%=csrf%> " method="POST">
用户名: <input type="text" name="username" /> <br><br>
密码: <input type="text" name="password" type="password" />
<button type="submit">提交</button>
</form>
2、提交数据的另一种方案
<form action="/news/doAdd> " method="POST">
<input type="hidden" name="_csrf" value="<%=csrf%>">
用户名: <input type="text" name="username" /> <br><br>
密
码: <input type="text" name="password" type="password" />
<button type="submit">提交</button>
</form>
3、获取数据(egg.js 获取数据不需要配置中间件直接通过下面方式获取)
this.ctx.request.body
4、获取 csrf 的值
获取:this.ctx.csrf
3、Egg 配置模板全局变量
ctx.state.csrf=ctx.csrf;
//设置全局变量
写一个中间件app/middleware/csrf.js
module.exports = (options, app) => {
return async function csrfMiddleware(ctx, next) {
ctx.state.csrf=ctx.csrf;
await next();
};
}
开启中间件config/config.default.js
config.middleware = ['csrf'];
Last updated
Was this helpful?