Egg.js Post 提交数据、Egg.js 安全机制 CSRF 的防范、以及配置模板全局变量

1Egg 安全机制 CSRF 的防范

http://eggjs.org/zh-cn/core/security.html

2Egg Post 提交数据

 

    <form action="/add" method="POST">

        <input type="hidden" name="_csrf" value="<%=csrf%>">
    
        用户名: <input type="text" name="username" /> <br><br>  
  
        密 码: <input type="password" name="password" type="password" />  <br><br>
  
        <button type="submit">提交</button>
    </form>

 

3、获取数据(egg.js 获取数据不需要配置中间件直接通过下面方式获取)

 

this.ctx.request.body

 

4、获取 csrf 的值

 

获取:this.ctx.csrf

 

3Egg 配置模板全局变量

在中间件文件夹下创建auth.js

 

module.exports=(option,app)=>{

    return async function auth(ctx,next){

        //设置模板全局变量
        ctx.state.csrf=ctx.csrf;

        await next();
    }
}

 

 

 

 

 

 

 

 

 

posted on 2020-06-03 21:40  LoaderMan  阅读(1437)  评论(0编辑  收藏  举报

导航