Symfony-在onKernelRequest上设置Cookie [英] Symfony - Setting Cookie onKernelRequest

查看:49
本文介绍了Symfony-在onKernelRequest上设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够设置Cookie onKernelRequest方法,但未设置cookie,
其他一切工作正常,这里缺少什么?
我要实现的是,如果用户未登录并且没有cookie,则他应该看到http基本身份验证标头。
如果用户已登录或拥有cookie,那么他无需在http basic auth中输入其用户凭据即可访问预览域。

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();
        }
    }
}


推荐答案

在响应对象上设置cookie只是将cookie添加到该请求。您需要返回相同的响应对象,因此Symfony会将其呈现回客户端。自己渲染它不是一个好主意,因为可能稍后会发送内容,并且它实际上不是可测试的。

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.

如果您根据请求期间也应该使用的逻辑设置cookie,则可以将其拆分为两个事件侦听器方法。一个将在 kernel.request 上设置一个请求属性,另一个将在响应中的 kernel.response

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);
}

这篇关于Symfony-在onKernelRequest上设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆