[英]How to use Route Parameter on login_path / login_check with Symfony Security
I have a project using Silex, and I'm really brand new guy with this symfony framework. 我有一个使用Silex的项目,并且我真的是这个symfony框架的新手。 Silex comes with security plugin, that I believe its the same as symfony libs. Silex带有安全性插件,我相信它与symfony libs相同。 So then the problem is I need to use some route parameter to be used on login_path firewall config, as example below 因此,问题在于我需要在login_path防火墙配置中使用一些路由参数,如下例所示
I set up my route like this, cname will be the route variable, I'm trying to replace {cname} with cname variable from secured_area pattern, 我这样设置路由,cname将是路由变量,我正在尝试用来自secure_area模式的cname变量替换{cname},
$app->match('/{cname}/dashboard/users','Dashboard\Controller\DashboardController::userAction')->method('GET|POST')->bind('dashboard_users');
The idea is to replace "{cname}" with the route variable.. 这个想法是用路由变量替换“ {cname}”。
$app->register(new Silex\Provider\SecurityServiceProvider());
$app['security.firewalls'] = array(
'secured_area' => array(
'pattern' => '^/(\w+)/dashboard/',
'form' => array('login_path' => '/{cname}/login',
'check_path' => '/{cname}/dashboard/login_check',
'default_target_path' => '/{cname}/dashboard/home'
),
'users' => $app->share(function () use ($app) {
return new Dashboard\Service\UserProvider($app['db']);
}),
'logout' => array('logout_path' => '/{cname}/dashboard/logout',
'target' => '/'),
),
);
I've tried that, and it's not working, and then I put some code patch in 4 core files AbstractAuthenticationListener, FormAuthenticationEntryPoint, DefaultAuthenticationSuccessHandler, LogoutListener So I put a few lines of codes just to replace "{cname}" in some methods to those class, 我已经尝试过了,但是它不起作用,然后我在4个核心文件AbstractAuthenticationListener,FormAuthenticationEntryPoint,DefaultAuthenticationSuccessHandler,LogoutListener中放置了一些代码补丁,所以我只用了几行代码来替换其中的“ {cname}”类,
on AbstractAuthenticationListener class,
protected function requiresAuthentication(Request $request)
{
/* PATCH */
if($route_params = $request->attributes->get("_route_params")){
foreach($route_params as $key => $val){
$this->options['check_path'] = str_replace("{".$key."}", $val, $this->options['check_path']);
}
}
/**/
return $this->httpUtils->checkRequestPath($request, $this->options['check_path']);
}
On FormAuthenticationEntryPoint Class
public function start(Request $request, AuthenticationException $authException = null)
{
/* PATCH */
if($route_params = $request->attributes->get("_route_params")){
foreach($route_params as $key => $val){
$this->loginPath = str_replace("{".$key."}", $val, $this->loginPath);
}
}
/**/
if ($this->useForward) {
$subRequest = $this->httpUtils->createRequest($request, $this->loginPath);
return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
return $this->httpUtils->createRedirectResponse($request, $this->loginPath);
}
On DefaultAuthenticationSuccessHandler
protected function determineTargetUrl(Request $request)
{
/* PATCH */
if($route_params = $request->attributes->get("_route_params")){
foreach($route_params as $key => $val){
$this->options['default_target_path'] = str_replace("{".$key."}", $val, $this->options['default_target_path']);
$this->options['login_path'] = str_replace("{".$key."}", $val, $this->options['login_path']);
}
}
/**/
if ($this->options['always_use_default_target_path']) {
return $this->options['default_target_path'];
}
if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
return $targetUrl;
}
if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
$request->getSession()->remove('_security.'.$this->providerKey.'.target_path');
return $targetUrl;
}
if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
return $targetUrl;
}
return $this->options['default_target_path'];
}
on LogoutListener
protected function requiresLogout(Request $request)
{
/* PATCH */
if($route_params = $request->attributes->get("_route_params")){
foreach($route_params as $key => $val){
$this->options['logout_path'] = str_replace("{".$key."}", $val, $this->options['logout_path']);
}
}
/**/
return $this->httpUtils->checkRequestPath($request, $this->options['logout_path']);
}
I know this is not the right way to do it. 我知道这不是正确的方法。 If there is any proper way to do this, I really looking forward for it, thx. 如果有任何适当的方法可以做到这一点,我真的很期待。
If modifying the code like this works, you may just want to extend the modified classes. 如果像这样修改代码,您可能只想扩展修改后的类。 Silex\\Application
is an instance of \\Pimple
, a service container, which allows you to redefine or extend existing services on the fly. Silex\\Application
是\\Pimple
(服务容器)的实例,它使您可以动态地重新定义或扩展现有服务。 I have not tried this myself, but you may very well be able to replace the entry point for your firewall (and other classes you have changed). 我自己还没有尝试过,但是您很可能可以替换防火墙的入口点(以及其他已更改的类)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.