使用symonfy作为REST,我希望服务器不要在401上发送这样的头:
WWW-Authenticate : Basic realm=XXX
但就像
WWW-Authenticate : myOwnBasic realm=XXX
我如何重载BasicAuthenticationEntryPoint类或为基本数据创建自己的入口点类?
发布于 2015-08-10 11:14:17
我终于找到了解决办法:
您需要在parameters.yml中覆盖这个参数:
security.authentication.basic_entry_point.class: NAMESAPCE\YOURCUSTOM_CLASS
并在您喜欢的地方创建一个文件(我是在MyBundle\Security\Http\EntryPoint中创建的),如下所示:
<?php
namespace NAMESAPCE;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint;
class CustomBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint
{
private $realmName;
public function __construct($realmName)
{
$this->realmName = 'XXX';
}
/**
* {@inheritdoc}
*/
public function start(Request $request, AuthenticationException $authException = null)
{
$response = new Response();
$response->headers->set('WWW-Authenticate', sprintf('myOwnBasic realm="%s"', $this->realmName));
$response->setStatusCode(401);
return $response;
}
}
https://stackoverflow.com/questions/31915664
复制相似问题