我尝试使用access_denied_url
security.yml
参数
问题是......它什么也没做。当我以匿名方式访问/mon-equipement
时,它会将我重定向到/login
这是我的security.yml
文件:
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
always_use_default_target_path: false
default_target_path: /mon-equipement
target_path_parameter: _target_path
use_referer: false
logout: true
anonymous: true
access_denied_url: /
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/mon-equipement, role: ROLE_USER }
- { path: ^/admin/, role: ROLE_ADMIN }
我使用FOSUserBundle和Symfony2.3.16
答案 0 :(得分:3)
我认为您的access_controll
部分应如下所示:
access_control:
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
# ... here the other routes
您的问题:它会在/
页面上重定向您,但您无法访问此页面。因此,它会在登录页面上重定向您。
<强>更新强>
您还可以为所有防火墙定义access_denied_url
:
# app/config/security.yml
security:
access_denied_url: /
答案 1 :(得分:3)
首先,您必须知道 access_denied_url 仅重定向非匿名用户。例如,如果用户尝试访问其路径仅用于ROLE_ADMIN的页面,它将使用ROLE_MEMBER重定向用户。
这里的解决方案:
您必须创建一个服务( my_entry_point ),该服务将在入口点侦听器处触发(请参阅下面的security.yml)并将用户重定向到您的页面想要( target_page_for_redirection )
# app/config/security.yml
security:
firewalls:
main:
entry_point: my_entry_point # listener triggered if no token is set while an authentification is needed (access_control)
pattern: ^/
#src/Acme/UserBundle/Ressources/Config/services.yml
service
my_entry_point:
class: Acme\UserBundle\Redirection\EntryPointRedirection
arguments: [@router] #needed for URL redirection
<?php
namespace Acme\UserBundle\Redirection;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface,
Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\RedirectResponse;
class EntryPointRedirection implements AuthenticationEntryPointInterface
{
protected $router;
public function __construct($router)
{
$this->router = $router;
}
public function start(Request $request, AuthenticationException $authException = null)
{
return new RedirectResponse($this->router->generate('target_page_for_redirection'));
}
}