• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

javascript – 如何给被视作为静态资源的spa加上csrf保护?

php 搞代码 4年前 (2022-01-23) 19次浏览 已收录 0个评论
文章目录[隐藏]

最近我在使用react+react-router开发spa,后台使用的是yii2nignx设置的是当404就返回index.html。但是这样子有个问题是,我没有办法使用csrf的保护,这样子要怎么解决呢?

我看到了一个网站使用的技术栈和我的差不多,我看了它是在head标签上面写了一个含有token值的meta标签,而它的每一个请求都会将这个token作为header的值发回去。这样子要怎么做?要怎么将token值渲染到这个index.html里头?

回复内容:

最近我在使用react+react-router开发spa,后台使用的是yii2nignx设置的是当404就返回index.html。但是这样子有个问题是,我没有办法使用csrf的保护,这样子要怎么解决呢?

我看到了一个网站使用的技术栈和我的差不多,我看了它是在head标签上面写了一个含有token值的meta标签,而它的每一个请求都会将这个token作为header的值发回去。这样子要怎么做?要怎么将token值渲染到这个index.html里头?

ajax 提交的

<code>$.ajax({url: 你的urltype:依什么方式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<span style="color:transparent">~来1源gaodai#ma#com搞*代#码1网</span><abbr>搞代gaodaima码</abbr> 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>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:javascript – 如何给被视作为静态资源的spa加上csrf保护?
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址