简体   繁体   中英

CsrfToken elfinder ckEditor in Laravel 5.2

My problem is that when uploading files the following error is displayed. How do I solve this problem?

this my code in template:

<script src="/vendor/unisharp/laravel-ckeditor/ckeditor.js"></script>
                    <script src="/vendor/unisharp/laravel-ckeditor/adapters/jquery.js"></script>
                    <textarea name="content" class="form-control my-editor"></textarea>
                    <script>
                        $('textarea.my-editor').ckeditor({
                            filebrowserImageBrowseUrl: '{!! route('elfinder.ckeditor') !!}',
                        });
                    </script>

在此处输入图片说明

I do not know why, although again there are tokens operation can not be performed and the amount of error shows

ckeditor file :

$().ready(function() {
        var elf = $('#elfinder').elfinder({
// set your elFinder options here
            <?php if($locale){ ?>
            lang: '<?= $locale ?>', // locale
            <?php } ?>
            <?php if($csrf){ ?>
            data: { _token: '<?php echo csrf_field(); ?>' },
            <?php } ?>
            url: '<?= route('elfinder.connector') ?>', // connector URL
            getFileCallback : function(file) {
                window.opener.CKEDITOR.tools.callFunction(funcNum,              file.url);
                window.close();
            }
        }).elfinder('instance');
    });

this not work :(

Laravel 5 added a VerifyCSRF token in the web middleware. What it basically does is injecting a token to be verified later when the user submit the form to prevent cross-site request forgery (CSRF) attacks.

You can disabled it in app/Http/Kernel.php but it is not recommended.

The other options is to include the token in your form to be send to the server when request is made.

Add this within your form.

{{ csrf_field() }}

It will generates the following HTML chunk:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

Refer: Laravel docs on this

change you elfinder option for sending additional POST data to "customData".

$().ready(function () {
            var elf = $('#elfinder').elfinder({
                // set your elFinder options here
                customData: { 
                    _token: '<?php echo  csrf_token() ?>'
                },
                dialog: {width: 900, modal: true, title: 'Select a file'},
                resizable: false,
                commandsOptions: {
                    getfile: {
                        oncomplete: 'destroy'
                    }
                },
                getFileCallback: function (file) {
                    $('<?php echo $input_id?>').summernote('editor.insertImage',files.url);
                }
            }).elfinder('instance');
        });

your option is data, change it into customData. Also just send csrf token value by using csrf_token()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM