最近我在使用react
+react-router
开发spa,后台使用的是yii2
。nignx
设置的是当404
就返回index.html
。但是这样子有个问题是,我没有办法使用csrf
的保护,这样子要怎么解决呢?
我看到了一个网站使用的技术栈和我的差不多,我看了它是在head标签上面写了一个含有token
值的meta标签,而它的每一个请求都会将这个token
作为header
的值发回去。这样子要怎么做?要怎么将token
值渲染到这个index.html
里头?
回复内容:
最近我在使用react
+react-router
开发spa,后台使用的是yii2
。nignx
设置的是当404
就返回index.html
。但是这样子有个问题是,我没有办法使用csrf
的保护,这样子要怎么解决呢?
我看到了一个网站使用的技术栈和我的差不多,我看了它是在head标签上面写了一个含有token
值的meta标签,而它的每一个请求都会将这个token
作为header
的值发回去。这样子要怎么做?要怎么将token
值渲染到这个index.html
里头?
ajax 提交的
<code>$.ajax({ url: 你的url type:依什么方式 dataType:数据类型 data : headers:{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') ? $('meta[name="csrf-token"]').attr('content') : ''}, beforeSend:function(msg){ alert('等待回调'); }, })</code>
将输出部分放在header头里
<code><?php // +---------------------------------------------------------------------- // | CSRF安全验证类 @pushaowei // +---------------------------------------------------------------------- // | [Usage] // | // 后端 // | use library\Base\NoCSRF; // | session_start(); // | if ($this->getRequest()->isPost()) { // | // | try { // | ##验证TOKEN // | NoCSRF::check( 'csrf_token', $_POST, true, 60*10, false ); //60*10为10分钟(null为不验证时间) // | $result = 'CSRF check passed. Form parsed.'; // | //$this->getRequest()->getPost('field'); // | echo $result; // | } catch ( Exception $e ) { // | echo $e->getMessage() . ' Form ignored.'; // | } // | } else { // | #生成TOKEN // | $token = NoCSRF::generate( 'csrf_token' ); // | $this->getView()->assign('token', $token); // | $this->getView()->display('页面'); // | } // | // 前端 // | <meta name="csrf-token" content="<?php echo library\Base\NoCSRF::generate( 'csrf_token' );?>"> // +---------------------------------------------------------------------- class NoCSRF { protected static $doOriginCheck = false; /** * Check CSRF tokens match between session and $origin. * Make sure you generated a token in the form before checking it. * * @param String $key The session and $origin key where to find the token. * @param Mixed $origin The object/associative array to retreive the token data from (usually $_POST). * @param Boolean $throwException (Facultative) TRUE to throw exception on check fail, FALSE or default to return false. * @param Integer $timespan (Facultative) Makes the token expire after $timespan seconds. (null = never) * @param Boolean $multiple (Facultative) Makes the token reusable and not one-time. (Useful for ajax-heavy requests). * * @return Boolean Returns FALSE if a CSRF attack is detected, TRUE otherwise. */ public static function check( $key, $origin, $throwException=false, $timespan=null, $multiple=false ) { $session = Session::getInstance(); if ( !$session->has( 'csrf_' . $key ) ) if($throwException) throw new \Exception( 'Missing CSRF session token.' ); else return false; if ( !isset( $origin[ $key ] ) ) if($throwException) throw new \Exception( 'Missing CSRF form token.' ); else return false; // Get valid token from session $hash = $session->get('csrf_' . $key); // Free up session token for one-time CSRF token usage. if(!$multiple) $session->forget('csrf_' . $key); // Origin checks if( self::$doOriginCheck && sha1( $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT'] ) != substr( base64_decode( $hash ), 10, 40 ) ) { if($throwException) throw new \Exception( 'Form origin does not match token origin.' ); else return false; } // Check if session token matches form token if ( $origin[ $key ] != $hash ) if($throwException) throw new \Exception( 'Invalid CSRF token.' ); else return false; // Check for token expiration if ( $timespan != null && is_int( $timespan ) && intval( substr( base64_decode( $hash ), 0, 10 ) ) + $timespan put('csrf_' . $key, $token); return $token; } /** * Generates a random string of given $length. * * @param Integer $length The string length. * @return String The randomly generated string. */ protected static function randomString( $length ) { $seed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijqlmnopqrtsuvwxyz0123456789'; $max = strlen( $seed ) - 1; $string = ''; for ( $i = 0; $i </code>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

调用方法:1、类组件中的调用可以利用React.createRef()、ref的函数式声明或props自定义onRef属性来实现;2、函数组件、Hook组件中的调用可以利用useImperativeHandle或forwardRef抛出子组件ref来实现。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1
好用且免费的代码编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),