Angular5与Symfony3授权

时间:2018-01-29 09:02:02

标签: php symfony lexikjwtauthbundle

我有Apache服务器和Postman来检查api url是否正常工作,我有奇怪的行为,这意味着当我在Postman上使用正确的凭据http://localhost/xxx/web/app_dev.php/api/login_check调用时,LexikJwtBundle正在返回令牌,所以我工作正常。 但是在mozilla或Chrome中执行此操作我得到401 Bad Credentials,我的security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

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

        api:
            pattern:   ^/api
            stateless: true
            lexik_jwt: ~

        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                # csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
                csrf_token_generator: security.csrf.token_manager

            logout:       true
            anonymous:    true

在mozilla中请求:

Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: http://sprawy.com
Connection: keep-alive
什么可能是错的?

2 个答案:

答案 0 :(得分:0)

你的Symfony配置看起来很好。

使用您的身份验证请求设置此标头:

'Accept': 'application/json',
'Content-Type': 'application/json'

答案 1 :(得分:0)

Angular没有发送合适的标题。在创建帖子调用时添加headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),解决了问题。