AuthenticationCredentialsNotFoundException: The se

2019-06-24 02:29发布

问题:

I am getting the following error

AuthenticationCredentialsNotFoundException: The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.

I have tried solutions because i know this error occurs when there is no security firewall configured for the route, however i cant seem to solve the error.

This is my security.yml

security:
  access_decision_manager:
    # strategy can be: affirmative, unanimous or consensus
    strategy: unanimous
  encoders:
    "MyName\MyBundle\Entity\User": { algorithm: sha1, encode_as_base64: false, iterations: 0 }   # You'll set this to sha1 when you have everything working.

  providers:
    default:    
      entity: { class: MyName\MyBundle\Entity\User, property: username } # This is an ORM entity and username is just the username property of the entity.
  role_hierarchy:
    ROLE_MEMBER: ROLE_GUEST
    ROLE_STAFF: ROLE_MEMBER
    ROLE_ADMIN: ROLE_STAFF

  firewalls:
    dev:
      pattern:  ^/(_(profiler|wdt)|css|images|js)/
      security: false
      anonymous: true

    main:
      pattern: ^/
      anonymous: ~
      form_login:
        login_path: /login
        check_path: /login_check
        success_handler: authentication_handler
        failure_handler: authentication_handler
        use_referer: true
        default_target_path: /
      remember_me:
        key:      "%secret%"
        lifetime: 31536000 # 365 days in seconds
        path:     /
        domain:   ~ # Defaults to the current domain from $_SERVER
        always_remember_me: true
      logout:
        path: /logout
        target: /

  access_control:
    - { path: ^/admin, roles: ROLE_STAFF }
    - { path: ^/admin2, roles: ROLE_STAFF }
    - { path: ^/members, roles: ROLE_MEMBER }
    - { path: ^/shop/ipn/paypal, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/shop/step, roles: IS_AUTHENTICATED_FULLY }
    - { path: ^/shop/confirm, roles: IS_AUTHENTICATED_FULLY }
    - { path: ^/application, roles: IS_AUTHENTICATED_FULLY }
    - { path: ^/account, roles: IS_AUTHENTICATED_FULLY }
    - { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/home, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    #- { path: ^/foo, access: "hasRole('FOO') and hasRole('BAR')" }
    #- { path: ^/forum, access: "!hasRole('ROLE_BANNED')"}

So ive added default routes and anonymous allowed but the error still persists

回答1:

Try something like this:

secure:
  pattern: ^/(admin|admin2|members|shop|application|account)
  anonymous: true
  form_login:
    login_path: /login
    check_path: /login_check
    success_handler: authentication_handler
    failure_handler: authentication_handler
    use_referer: true
    default_target_path: /
  remember_me:
    key:      "%secret%"
    lifetime: 31536000 # 365 days in seconds
    path:     /
    domain:   ~ # Defaults to the current domain from $_SERVER
    always_remember_me: true
  logout:
    path: /logout
    target: /

main:
    pattern:   ^/
    anonymous: true