2017-04-15 119 views
1

我的網絡服務器在Nginx上配置。在主機srv.local上我有Symfony3。配置:Symfony3與Nelmio/Cors和vue.js與axios - 錯誤

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 } 

在主機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)?

當我用捲曲,我得到了正確的JSON響應:

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

當您嘗試http://www.jakelitwicki.com/tags/headers/解決方案你也禁用NelmioCorsBundle?我認爲http://www.jakelitwicki.com/tags/headers/解決方案可能無法按預期工作,除非您先刪除NelmioCorsBundle。我注意到在這發帖,他說,*「試圖nelmio/CORS束了有限的成功和各種奇怪的問題。特別是響應會回來的成功,但是瀏覽器仍然會作出反應,如果有對響應報頭404。「*,所以也許你試圖讓NelmioCorsBundle工作是不值得更多的時間。他的解決方案更簡單。 – sideshowbarker

+0

謝謝@sideshowbarker。 Nelmio現在評論。 – Maciej

回答

0

我評論Nelmio爲@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崗位。 '捲曲'仍在按預期工作,所以我知道憑據是正確的。我是新來vue.js,我應該設置比「數據」屬性的東西Axios公司其他?