我正在扩展身份验证失败处理程序,一切都主要工作正常,但一个小问题.
这是我的services.yml:
http.utils.class:
class: Symfony\Component\Security\Http\HttpUtils
auth.fail:
class: Acme\MyBundle\AuthenticationFailure
arguments:
- @http_kernel
- @http.utils.class
- []
Run Code Online (Sandbox Code Playgroud)
我已将此设置为在security.yml中使用:
failure_handler: auth.fail
Run Code Online (Sandbox Code Playgroud)
这是我的Acme\MyBundle\AuthenticationFailure.php:
namespace Acme\MyBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\HttpFoundation\Response;
class AuthenticationFailure extends DefaultAuthenticationFailureHandler
{
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我在security.yml中设置的选项被忽略了.我知道类的_construct方法的第三个参数是$ options数组,我没有作为第三个参数(在services.yml中)传递任何东西,所以我猜这是问题,解决方案可以只是传递值.我猜我也可以这样做:
arguments:
- @http_kernel
- @http.utils.class
- %security.firewalls.secure_area.form_login%
Run Code Online (Sandbox Code Playgroud)
....我没有测试,因为问题是这是在services.yml中的硬编码,并且它不理想,好像我更改了secure_area的名称,它会破坏.当然,这些价值观是以更好的方式提供的吗?
我看到你正试图将login_path传递给你的身份验证失败处理程序......
...你应该注入@router,调整__construct方法并使用auth故障处理程序内防火墙使用的路由名称(不是模式)生成URL .然后将用户重定向到那里......
login_path: your_login_route_name # <- not a pattern like /login
Run Code Online (Sandbox Code Playgroud)
这样改变防火墙的名称不会破坏你的应用程序!
如果你甚至不想在更改路由名称时破坏应用程序,你可以使这个可配置:
config.yml
parameters:
login.route_name: my_login_route_name
Run Code Online (Sandbox Code Playgroud)
使用routing.yml
%login.route_name%:
pattern: /login
Run Code Online (Sandbox Code Playgroud)
security.yml
security:
firewalls:
your_firewall_name:
failure_handler: auth.fail
login_path: %login.route_name%
Run Code Online (Sandbox Code Playgroud)
services.yml
auth.fail:
arguments:
- # ...
- @router
- %login.route_name%
Run Code Online (Sandbox Code Playgroud)
ACME\MyBundle \有authenticationFailure
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
// ...
protected $router;
protected $loginRoute;
public function __construct(
/** ... other arguments **/,
RouterInterface $router,
$loginRoute
)
{
// ..
$this->router = $router;
$this->loginRoute = $loginRoute;
}
public function onAuthenticationFailure(
Request $request,
AuthenticationException $exception
)
{
// ...
return new RedirectResponse( $this->router->generate($this->loginRoute) );
}
Run Code Online (Sandbox Code Playgroud)
提示您的建议
(...使用类似的东西%security.firewalls.secure_area.form_login%)
你不能直接访问安全配置(这些不是参数 - 你不能使用%security.firewall.whatever%!)...
$options传递给的默认值__construct必须是一个数组......
...因此,[]如果这些参数不是数组,您可能需要包围传递的参数.
arguments:
- [ %parameter1% , %parameter2% ]
Run Code Online (Sandbox Code Playgroud)