0

我正在使用 LexikJWTAuthenticationBundle 使用 REST Web 服务在我的 Web 应用程序中进行身份验证。

我想将我的应用程序分为两个部分:

  • 公共部分,每个人都可以看到内容 - 无需登录
  • 一个私人部分,您必须登录才能编辑内容,用户

等等。

这个想法是,通过 url 做到这一点:

/api       #reach the public content of the website
/api/admin #reach private admin content, if not logged in -> loginpage

我在security.yaml中试过这个:

    access_control:
    - { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api/admin,       roles: IS_AUTHENTICATED_FULLY }

但是当我尝试像这样加载内容时:

curl -X GET <baseurl-backend>/api/content/list #generic example

我得到:

{code: 401, message: "JWT Token not found"}

这是包含所有配置的 security.yaml:

security:
    encoders:
        App\Entity\User:
            algorithm: argon2i

    providers:
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        login:
            pattern:  ^/api/login
            stateless: true 
            anonymous: true 
            json_login:
                check_path: /api/login_check #path for checking
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
        api:
            pattern:   ^/api
            stateless: true
            guard:
                authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator
        main: 
            anonymous: true

    access_control:
    - { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api/admin,       roles: IS_AUTHENTICATED_FULLY }

感谢您的帮助!

4

1 回答 1

3

你应该添加anonymous: true到你的 api 防火墙。

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

如果你想阻止访问api/admin你应该在你的 api 防火墙之上添加另一个防火墙:

api_admin:
    pattern:   ^/api/admin
    stateless: true
    guard:
        authenticators:
        - lexik_jwt_authentication.jwt_token_authenticator
api:
    pattern:   ^/api
    stateless: true
    anonymous: true
    guard:
        authenticators:
        - lexik_jwt_authentication.jwt_token_authenticator
于 2019-05-16T11:15:55.937 回答