【问题标题】:Angular5 with Symfony3 Authorization带有 Symfony3 授权的 Angular5
【发布时间】:2018-01-29 09:02:01
【问题描述】:

我有 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

有什么问题吗?

【问题讨论】:

  • 只检查请求头,希望你能得到答复
  • 字段名称可能有误。

标签: php symfony lexikjwtauthbundle


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你的 Symfony 配置看起来不错。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2022-08-02
      • 2014-12-25
      相关资源
      最近更新 更多