Symfony2 - 访问此资源需要完全身份验证.

Hel*_*ert 3 security redirect symfony

我想将匿名用户重定向到登录页面,但(显然)遇到了问题.我收到一个错误:

访问此资源需要完全身份验证.

这是内部服务器错误.我可以通过添加form_login来解决这个问题,但是我编写了一个自定义身份验证提供程序,并且使用form_login会导致我的自定义ldap身份验证提供程序不再被使用.(这意味着用户不能再登录)

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

    login:
      pattern:  ^/login$
      security: false

    api:
      pattern:  ^/api
      security: false

    secured_area:
      pattern:    ^/
      anonymous: true
      ldap: true
      logout:
        path:   /logout
        target: /login

  providers:
    chain_provider:
      chain:
          providers: [in_memory, ldap]
    in_memory:
      memory:
          users:
              admin: { password: adminpass }
    ldap:
      id: ldap_user_provider


  encoders:
    Symfony\Component\Security\Core\User\User: plaintext
    Prophets\ParkingBundle\Entity\User: plaintext

  access_control:
    - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: IS_AUTHENTICATED_REMEMBERED }
    - { path: ^/_wdt, roles: 'IS_AUTHENTICATED_ANONYMOUSLY' }
Run Code Online (Sandbox Code Playgroud)

任何人?

och*_*tos 6

这是一个老问题,但无论如何都值得回答.我有几乎相同的问题,但不是重定向用户,我只是想显示403 json页面.问题是当没有form_login或没有默认功能不完整时,SF2不提供默认功能,以进一步研究这个检查SF2的ExceptionListener类的handleAccessDeniedException方法.

解决方法是在防火墙上实现entry_point. 安全配置文档以及防火墙中的更多信息.

您只需在服务中实现Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface接口,并指向entry_point上的该服务.所以我假设你想要HTTP.302

<?php
namespace Acme\ApiBundle\Security\Firewall;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;

class EntryPoint implements AuthenticationEntryPointInterface{
  private $url;
  public function __construct($url){
    $this->url = $url;
  }
  public function start(Request $request, AuthenticationException $authException = null){
      $response = new Response(
        '',
        Response::HTTP_FOUND, //for 302 and Response::HTTP_TEMPORARY_REDIRECT for HTTP307 read about in [Response][4]
        array('Location'=>$this->url)); 
      return $response;
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我必须补充一点,这是一个已知问题,并且有些设计特定/意图错误:https://github.com/symfony/symfony/issues/8467#issuecomment-163670549