Php Symfony2防火墙:重定向到注册表而不是登录
我正在开发一个基于Symfony2的简单商店(用于编译报价) 将项目添加到购物车后,用户可以继续查看其报价摘要,然后请求已编译的报价 摘要页面受以下防火墙保护:Php Symfony2防火墙:重定向到注册表而不是登录,php,security,symfony,authentication,Php,Security,Symfony,Authentication,我正在开发一个基于Symfony2的简单商店(用于编译报价) 将项目添加到购物车后,用户可以继续查看其报价摘要,然后请求已编译的报价 摘要页面受以下防火墙保护: security: firewalls: secured_area: pattern: ^/ anonymous: ~ provider: default form_login: login_path: acme_security_login
security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
provider: default
form_login:
login_path: acme_security_login_route
check_path: acme_security_login_check_route
csrf_provider: form.csrf_provider
logout: ~
default:
anonymous: ~
access_control:
- { path: ^/request-offer, roles: ROLE_CLIENT }
providers:
default:
entity: { class: AcmeShopBundle:User }
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Acme\ShopBundle\Entity\User:
algorithm: bcrypt
cost: 15
这意味着,如果客户机已登录,他将直接进入摘要,如果未登录,他将被重定向到登录页面
现在,由于客户更有可能成为新客户,我想改为转到注册表
中描述的选项不允许这样做。
当然,更改登录路径也不是解决方案
最好的解决方案是什么?在我看来,一个好的解决方案是添加自己的AccessDeniedExceptionHandler,这里将解释如何做到这一点
此外,还可以通过配置组件对服务进行配置,以便将路由作为参数传递给重定向
如果您这样做,您可以更改,如果您有更多的用户,重定向回登录页面,而不编辑任何类。引导我找到解决方案
引述:
只有当用户没有足够的权限访问资源时,才会调用access\u denied\u处理程序所指向的服务。如果用户在任何时候都没有经过身份验证,则永远不会调用已授权的处理程序。为security.yml中的入口点提供服务实际上解决了这个问题
所以我就这样结束了:
#services.yml
acme.security.entry\u point.class:ArtCube\ShopBundle\Service\EntryPointHandler
服务:
acme.security.entry\u point\u处理程序:
类:%acme.security.entry\u point.class%
论据:
路由器:@路由器
然后我将此服务添加到我的security.yml
,就在注销:~
行之后(请参见初始问题):
并创建了服务:
只有当用户没有足够的权限访问资源时,才会调用access\u denied\u处理程序,但如果用户根本没有经过身份验证,则不会调用该处理程序。但是你的回答让我找到了答案,谢谢你!
entry_point: art_cube_shop.security.entry_point_handler
// Acme/ShopBundle/Service/EntryPointHandler.php
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
class EntryPointHandler implements AuthenticationEntryPointInterface {
protected $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function start(Request $request, AuthenticationException $authException = null)
{
if($authException instanceof InsufficientAuthenticationException)
{
return new RedirectResponse($this->router->generate('acme_security_registration_route'));
}
else
{
return new RedirectResponse($this->router->generate('acme_security_login_route'));
}
}
}