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

时间:2020-06-03 22:38:19   收藏:0   阅读:121

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();
    }
}

 

 

 

 

 

 

 

 

 

原文:https://www.cnblogs.com/loaderman/p/11546985.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!