检查基于路径的用户身份验证

检查基于路径的用户身份验证

本文介绍了Symfony2的:检查基于路径的用户身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在Symfony2中,是否有可能检查用户身份验证的访问,他请求的URL。
我想要做的是,我不想让登录的用户回到注册或登录或恢复密码的页面。

这是我的security.yml:

  ACCESS_CONTROL:
     - {路径:^ /注册/,角色:IS_AUTHENTICATED_ANONYMOUSLY&功放;&安培; !IS_AUTHENTICATED_FULLY}
     - {路径:^ /寄存器/,角色:IS_AUTHENTICATED_ANONYMOUSLY&功放;&安培; !IS_AUTHENTICATED_FULLY}
     - {路径:^ /恢复/,角色:IS_AUTHENTICATED_ANONYMOUSLY&功放;&安培; !IS_AUTHENTICATED_FULLY}

但这正显示出,拒绝访问页面,当前用户。所以,我认为这将是很好,如果我也可以把这种要求用户主页,通过检查如果不允许他。我可以通过用户在收听者进行身份验证或不提供路径检查?

 公共职能onKernelResponse(FilterResponseEvent $事件)
    {
     $请求= $事件 - >调用getRequest();
     $ PATH = $请求 - > getPathInfo();     如果($这个 - >盛器>获取('security.context') - GT;!为gettoken()= NULL){
       //要检查用户进行身份验证或匿名
       如果(($这个 - >盛器>获取('security.context') - GT;为gettoken()的instanceof UsernamePasswordToken)及和放大器;
        ($这个 - >盛器>获取('security.context') - GT; isGranted('IS_AUTHENTICATED_FULLY')==真)){
         //如何检查路径?
        //设置响应重定向到主页
      }
    }
  }


解决方案

security.access_map 服务

的配置的 security.access_control 被处理......

  SecurityBundle \\ DependencyInjection \\ SecurityExtension

...这对于路线(路径,主机,IP,...)创建RequestMatchers,然后调用该服务的<$c$c>add()方法与匹配时,允许角色和信道(即HTTPS)。

的服务通常使用由即AccessListener.

The class used for the security.access_map service is defined by the parameter security.access_map.class and defaults to

You can use the parameter security.access_map.class to override the service with a custom class (must implement AccessMapInterface):

# i.e. app/config/config.yml

parameters:
    security.access_map.class: My\Custom\AccessMap


How to access the service

The security.access_map service is a private service as you can see by it's definition here.

This means you can't request it from the container directly like this:

$this->container->get('security.access_map')

You will have to inject it into another service (i.e. a listener service) explicitly to be able to access it.


A listener example

services:
    my_listener:
        class: My\Bundle\MyListenerBundle\EventListener\ForbiddenRouteListener
        arguments: [ @security.access_map ]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

Then you can call the getPatterns() method to obtain the RequestMatchers, allowed roles and required channel from there.

namespace My\Bundle\MyListenerBundle\EventListener;

use Symfony\Component\Security\Http\AccessMapInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class ForbiddenRouteListener
{

    protected $accessMap;

    public function __construct(AccessMapInterface $access_map)
    {
        $this->accessMap = $access_map;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        $patterns = $this->accessMap->getPatterns($request);

        // ...

这篇关于Symfony2的:检查基于路径的用户身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:22