带NelmioCors的Symfony3和带axios的vue.js-错误

带NelmioCors的Symfony3和带axios的vue.js-错误,symfony,nginx,vue.js,axios,nelmiocorsbundle,Symfony,Nginx,Vue.js,Axios,Nelmiocorsbundle,我的web服务器是在Nginx上配置的。在主机srv.local上,我有Symfony3。配置: config.yml: # NelmioCors Configuration nelmio_cors: paths: '^/': allow_origin: ['*'] allow_headers: ['*'] allow_methods: ['POST', 'PUT', 'GET', 'DELETE'] max_age: 36

我的web服务器是在Nginx上配置的。在主机srv.local上,我有Symfony3。配置:

config.yml:

# NelmioCors Configuration
nelmio_cors:
paths:
    '^/':
        allow_origin: ['*']
        allow_headers: ['*']
        allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
        max_age: 3600
routing.yml:

login_check:
        path: /login_check
        methods: [POST]
security.yml:

firewalls:
    login:
        pattern:  ^/login
        stateless: true
        anonymous: true
        form_login:
            check_path: /login_check
            require_previous_session: false
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure

    api:
        pattern: ^/api
        stateless: true
        anonymous: false
        provider: database
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator

access_control:
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
在host client.local上,我有vue.js和方法返回的响应:

return axios({
  method: 'post',
  url: 'http://srv.local/login_check',
  headers: {
    'content-type': 'multipart/form-data'
  },
  data: {
    _username: 'admin',
    _password: 'admin'
  },
  responseType: 'json'
})
curl -X POST http://srv.local/login_check -d _username=admin -d _password=admin
运行此vue.js方法后,我发现错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource.
我使用了来自的解决方案,但在响应消息中出现“网络错误”。我使用了StackOverflow的许多解决方案,但没有得到积极的结果。我做错了什么?如何将NelmioCore和Vue.js(axios)结合使用

使用curl后,我得到了正确的json响应:

return axios({
  method: 'post',
  url: 'http://srv.local/login_check',
  headers: {
    'content-type': 'multipart/form-data'
  },
  data: {
    _username: 'admin',
    _password: 'admin'
  },
  responseType: 'json'
})
curl -X POST http://srv.local/login_check -d _username=admin -d _password=admin

正如@sideshowbarker所写,我评论了内尔米奥,这是我的错。现在我从日志中发现Symfony错误:

[2017-04-16 12:08:22] doctrine.DEBUG: SELECT t0.id AS id_1, t0.username AS username_2, t0.email AS email_3, t0.password AS password_4, t0.password_expires_at AS password_expires_at_5, t0.status AS status_6, t0.last_login_at AS last_login_at_7, t0.last_wrong_login_at AS last_wrong_login_at_8, t0.last_password_changed_at AS last_password_changed_at_9, t0.confirmation_token AS confirmation_token_10, t0.created_at AS created_at_11, t0.updated_at AS updated_at_12 FROM user t0 WHERE t0.username = ? LIMIT 1 ["NONE_PROVIDED"] []
[2017-04-16 12:08:22] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): Bad credentials. at /srv/srv.local/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:73, Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException(code: 0): User \"NONE_PROVIDED\" not found. at /srv/srv.local/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php:61)"} []

似乎vue没有发布用户名和密码。”curl'仍按预期工作,因此我知道凭据是正确的。我是vue.js新手,我是否应该为axios设置“数据”属性以外的内容?

正如@sideshowbarker所写,我对Nelmio的评论是我的错。现在我从日志中发现Symfony错误:

[2017-04-16 12:08:22] doctrine.DEBUG: SELECT t0.id AS id_1, t0.username AS username_2, t0.email AS email_3, t0.password AS password_4, t0.password_expires_at AS password_expires_at_5, t0.status AS status_6, t0.last_login_at AS last_login_at_7, t0.last_wrong_login_at AS last_wrong_login_at_8, t0.last_password_changed_at AS last_password_changed_at_9, t0.confirmation_token AS confirmation_token_10, t0.created_at AS created_at_11, t0.updated_at AS updated_at_12 FROM user t0 WHERE t0.username = ? LIMIT 1 ["NONE_PROVIDED"] []
[2017-04-16 12:08:22] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): Bad credentials. at /srv/srv.local/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:73, Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException(code: 0): User \"NONE_PROVIDED\" not found. at /srv/srv.local/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php:61)"} []
似乎vue没有发布用户名和密码。”curl'仍按预期工作,因此我知道凭据是正确的。我是vue.js新手,我是否应该为axios设置“数据”属性以外的内容?

当您尝试该解决方案时,是否也禁用了NelmioCorsBundle?我认为除非你先去掉内尔米奥科斯邦德,否则解决方案可能不会像预期的那样有效。我注意到他在那篇帖子中说,“尝试nelmio/cors捆绑,但成功率有限,出现了各种奇怪的问题。特别是响应会成功返回,但浏览器的响应仍然会像响应头上有404一样。“,所以也许你不值得花更多的时间让NelmioCorsBundle工作。他的解决方案更简单。当你尝试解决方案时,你是否也禁用了NelmioCorsBundle?我认为除非你先移除NelmioCorsBundle,否则解决方案可能不会像预期的那样工作。我注意到他在那篇帖子中说,“尝试了nelmio/cors捆绑,但成功率有限,并且出现了各种奇怪的问题。特别是响应会成功返回,但浏览器的响应仍然会像响应头上有404一样。”因此,您可能不值得再花时间尝试让NelmioCorsBundle工作。他的解决方案更简单。