【问题标题】:How can I force authenticated user to HTTPS in Symfony2 after fully authentication only?仅在完全身份验证后,如何在 Symfony2 中强制经过身份验证的用户使用 HTTPS?
【发布时间】:2014-07-31 06:02:07
【问题描述】:

我需要在 Symfony 2.2 中使用 https,但前提是用户完全通过身份验证。

【问题讨论】:

    标签: symfony symfony-2.2


    【解决方案1】:

    刚刚使用 onKernalRequest 事件侦听器获得了解决方案。

    我的 onKernelRequest 事件服务:

    <?php
    
    namespace Acme\DemoBundle\Listener;
    
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    
    /**
     * force https for fully authenticated user
     */
    class ForceHttpsListner
    {
    
        /**
         * container
         * 
         * @var type 
         */
        private $container;
    
        /**
         * 
         * @param type $container
         */
        public function __construct($container)
        {
            $this->container = $container;
        }
    
        /**
         * on kernel request
         * 
         * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
         */
        public function onKernelRequest(GetResponseEvent $event)
        {
    
            //all urls which always use https, specefied using requires_channel in security.yml
            $httpsPaths= [
                '/login',
                '/resetting/request',
                '/register/',
            ];
    
            if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST ) {
    
                $request = $event->getRequest();
    
                if ($this->container->get('security.context')->getToken()
                        && $this->container->get('security.context')
                                ->isGranted('IS_AUTHENTICATED_FULLY')) {
    
                    if (!$request->isSecure()) {
    
                        $request->server->set('HTTPS', true);
                        $request->server->set('SERVER_PORT', 443);
                        $event->setResponse(new RedirectResponse($request->getUri()));
    
                    }
    
                } else {
    
                    //echo $request->getPathInfo(); exit;                
                    if ($request->isSecure() 
                            && !in_array($request->getPathInfo(), $httpsPaths)) {
    
                        $request->server->set('HTTPS', false);
                        $request->server->set('SERVER_PORT', 80);
                        $event->setResponse(new RedirectResponse($request->getUri()));
                    }
    
                }
    
            }
    
        }
    }
    

    注册服务

    服务: kernel.listener.forcehttps: 类:Acme\DemoBundle\Listener\ForceHttpsListner 标签: - {名称:kernel.event_listener,事件:kernel.request,方法:onKernelRequest} 参数:[@service_container]

    security.yml 中的access_control

    安全: 访问控制: - {路径:^/_wdt,角色:IS_AUTHENTICATED_ANONYMOUSLY } - {路径:^/_profiler,角色:IS_AUTHENTICATED_ANONYMOUSLY } - { 路径:^/login$,角色:IS_AUTHENTICATED_ANONYMOUSLY,requires_channel:https } - { 路径:^/login_check$,角色:IS_AUTHENTICATED_ANONYMOUSLY,requires_channel:https } - { 路径:^/register,角色:IS_AUTHENTICATED_ANONYMOUSLY,requires_channel:https } - {路径:^/resetting,角色:IS_AUTHENTICATED_ANONYMOUSLY,requires_channel:https} # 网站的安全部分 # 此配置需要为整个站点记录并具有管理部分的管理员角色。 # 更改这些规则以使其适应您的需求 - {路径:^/admin,角色:[ROLE_ADMIN,ROLE_SONATA_ADMIN,ROLE_SUPER_EDITOR]} - {路径:^/.*,角色:IS_AUTHENTICATED_ANONYMOUSLY }

    【讨论】:

      【解决方案2】:

      您需要创建一个 RequestEventListener,在路由和安全之后触发,以便在其中处理

      1) 安全上下文(查看用户是否完全通过身份验证)
      2)路由器组件中实际询问的路由(并与配置数组匹配)

      然后决定是否强制重定向到 https,如果还没有的话。

      【讨论】:

        猜你喜欢
        • 2017-01-23
        • 1970-01-01
        • 2020-11-15
        • 2016-01-17
        • 2016-01-15
        • 2019-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多