1 Star 0 Fork 2

赵先生 / think-csrf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

think-csrf

是基于thinkphp6.x封装的一个防止csrf攻击的composer包

为什么还要封装think-csrf

thinkphp官方的表单令牌其实从实际开发角度来说,它只适合用来防止表单重复提交(虽然官方文档上说可以防止csrf攻击)。 因为框架在验证表单令牌通过后会立马删除session中的token,这样对于ajax提交的方式是非常不友好的, 因为页面没有刷新而session中的表单令牌已经更新导致再次提交表单会失败, 该依赖包就是用来解决这个问题,且验证csrf的token值是长时间保存在cookie中的, 相对于官方表单令牌保存在session中有不会过期、和减轻服务端压力的特点。

防止表单重复提交和防止csrf攻击应该分开来做,你在使用think-csrf的同时也不影响你使用tp官方的表单令牌,它 们不会产生冲突

安装

composer require qiansion/think-csrf

配置

安装完毕后会自动在app/config目录下生成csrf.php配置文件。

<?php
return [
    //cookie中的key(可自行更换)
    'cookie_key' => '0cc175b9c0f1b6a831c399e269772661',
    //传递的csrf参数名称
    'param_key' => '_token',
];

使用

think-csrf默认是没有开启的(API应用通常不需要使用think-csrf),如果你需要使 用think-csrf,需要在全局的中间件定义文件中加上下面的中间件定义:

\qiansion\middleware\VerifyCsrfToken::class

Tips:如果是多应用模式,并且你只是用于部分应用,那么也可以在应用中间件定义文件中单独开启

表单提交

下面是示例,是基于think-smarty模板引擎来定义的, 如果你是tp框架自带的模板引擎,那么调用函数的语法应该是{:csrf_field()}

<form action="" method="post">
    <{ csrf_field() }>
  <div class="form-group">
  <label for="email">Email address:</label>
  <input type="email" class="form-control" placeholder="Enter email" id="email">
  </div>
  <div class="form-group">
  <label for="pwd">Password:</label>
  <input type="password" class="form-control" placeholder="Enter password" id="pwd">
  </div>
  <div class="form-group form-check">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox"> Remember me
  </label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

AJAX提交

如果是AJAX提交的表单,可以将token设置在meta

<meta name="csrf-token" content="<{ csrf_token() }>">

或直接在视图文件中调用csrf_meta()函数也能生成上面的meta标签

然后在全局Ajax中使用这种方式设置X-CSRF-Token请求头并提交:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

助手函数

函数名 描述
csrf_field 返回一个携带token的隐藏域
csrf_meta 返回一个携带token的meta标签
csrf_token 返回token值

推荐项目

MIT License Copyright (c) 2022 x852 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于thinkphp6的防止csrf攻击的中间件 展开 收起
MIT
取消

发行版 (2)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/qiansion/think-csrf.git
git@gitee.com:qiansion/think-csrf.git
qiansion
think-csrf
think-csrf
master

搜索帮助