【问题标题】:AuthenticationCredentialsNotFoundException: The security context contains no authentication tokenAuthenticationCredentialsNotFoundException:安全上下文不包含身份验证令牌
【发布时间】:2013-12-08 09:39:19
【问题描述】:

我收到以下错误

AuthenticationCredentialsNotFoundException:安全上下文 不包含身份验证令牌。一个可能的原因可能是 没有为此 URL 配置防火墙。

我已经尝试了解决方案,因为我知道当没有为路由配置安全防火墙时会发生此错误,但是我似乎无法解决该错误。

这是我的 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')"}

所以我添加了默认路由并允许匿名,但错误仍然存​​在

【问题讨论】:

  • 是错字吗? - { path: /^, role: IS_AUTHENTICATED_ANONYMOUSLY }你得到异常的网址是什么?
  • localhost/app_dev.php/homeewfwefwe 我试图让它显示一个 404 页面,在不存在的页面上有一个侧边栏,但是侧边栏包含一个 is_granted("IS_AUTHENTICATED_REMEMBERED") 引用来检查用户是否是否登录
  • 是不是因为你有2个default_target_path?
  • 我删除了 1 个 default_target_path(更新了上面的代码),但它并没有解决问题

标签: php security symfony


【解决方案1】:

试试这样的:

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

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2017-06-02
    • 2013-02-12
    • 2017-04-27
    • 2017-07-20
    • 2014-10-13
    • 2010-10-24
    • 2013-08-19
    • 2011-06-08
    相关资源
    最近更新 更多