Symfony - Setting Cookie onKernelRequest

本秂侑毒 提交于 2020-01-23 17:34:07

问题


i want to be able to set a Cookie onKernelRequest Method, but the cookie is not beeing set, Everything else is working fine, what am missing here? What i want to achieve is that if the user is not logged in and doesnt have the cookie he should see the http basic auth headers. If the user is logged in or does have the cookie he has access to the preview domains without having to enter their user credentials in http basic auth.

const AUTH_USER = 'myuser';
const AUTH_PW = 'mypass';

public function sendAuthorizationHeader()
{
    header('WWW-Authenticate: Basic realm="Preview Domain"');
    header('HTTP/1.0 401 Unauthorized');
    die();
}

public function onKernelRequest(GetResponseEvent $event)
{
    if (!$event->isMasterRequest()) {
        return;
    }

    $request = $event->getRequest();
    $host = $request->getHost();
    $loginSuccessful = false;

    // check if we are on a preview domain
    if (preg_match('/^preview-\d+/', $host)) {
        $user = $request->getUser();
        $cookie = $request->cookies->get('preview_user');
        $phpAuthUser = $request->server->get('PHP_AUTH_USER');
        $phpAuthPw = $request->server->get('PHP_AUTH_PW');

        if (isset($phpAuthUser) && isset($phpAuthPw)) {
            if ($phpAuthUser == self::AUTH_USER && $phpAuthPw == self::AUTH_PW) {
                $loginSuccessful = true;
            }
        } else if ($user === null && $cookie === null) {
            $this->sendAuthorizationHeader();
        }

        if (!$loginSuccessful) {
            $this->sendAuthorizationHeader();
        } else {
            $cookie = new Cookie('preview_user', true, 86400, '/', null, false, false);

            $response = new Response();
            $response->headers->setCookie($cookie);
            $response->sendHeaders();
        }
    }
}

回答1:


Setting a cookie on a response object doesn't do anything but adding a cookie to that request. You need to return the same response object, so Symfony renders it back to the client. Rendering it yourself is not a good idea, as there might be contents send later and it's not really testable.

It's easier done in a kernel.response event listener, since you already have a response there. Remember to use the response that your application creates. Do not create it yourself.

If you set the cookie based on logic that should also be available during the request, you can split it into two event listener methods. One will set a request attribute on kernel.request, and the other one will set the cookie on the response on kernel.response:

public function onKernelRequest(GetResponseEvent $event)
{
    // your logic goes here. calculate the $result
    // ...

    $event->getRequest()->attributes->set('my_result', $result);
}

public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();
    $request = $event->getRequest();

    $myResult = $request->attributes->get('my_result');
    $cookie = new Cookie(/* ... */);

    $response->headers->setCookie($cookie);
}


来源:https://stackoverflow.com/questions/35984666/symfony-setting-cookie-onkernelrequest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!