1

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

配置.yml:

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

路由.yml:

login_check:
        path: /login_check
        methods: [POST]

安全.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 }

在主机 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'
})

运行此 vue.js 方法后,我收到错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

我使用了来自http://www.jakelitwicki.com/tags/headers/的解决方案,但随后我在响应消息中收到“网络错误”。我使用了 StackOverflow 的许多解决方案,但没有积极的结果。我做错了什么?我怎样才能同时使用 NelmioCore 和 Vue.js (axios)?

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

curl -X POST http://srv.local/login_check -d _username=admin -d _password=admin
4

1 回答 1

0

正如@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 发布的。“卷曲”仍然按预期工作,所以我知道凭据是正确的。我是 vue.js 的新手,我应该为 axios 设置“数据”属性以外的东西吗?

于 2017-04-16T10:22:47.360 回答