好吧,这是我在万一有人怀疑时所做的:
首先在我的Security文件夹中,我创建了自己版本的BasicAuthenticationEntryPoint.php
<?php
/*
* Redefinition of the Symfony's BasicAuthenticationEntryPoint
*/
namespace multikanban\multikanban\Security\Http\EntryPoint;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
/**
* BasicAuthenticationEntryPoint starts an HTTP Basic authentication.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class BasicAuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
private $realmName;
public function __construct($realmName)
{
$this->realmName = $realmName;
}
/**
* {@inheritdoc}
*/
public function start(Request $request, AuthenticationException $authException = null)
{
$response = new Response();
$response->headers->set('WWW-Authenticate', 'FormBased');
$response->setStatusCode(401);
return $response;
}
}
请注意,我做了两件事:
-
添加AuthenticationEntryPointInterface的用法。
-
将WWW-Authenticate值更改为“FormBased”,这是对原始文件的实际修改,这样当服务器返回401未授权时,浏览器不会显示默认提示。(你也可以返回400,但这样你就不会真正遵守标准了)
其次,我在我的Silex应用程序中这样定义了服务:
$this['security.entry_point.main.http'] = $this->share(function() {
return new BasicAuthenticationEntryPoint('main');
});
“main”是我的防火墙名称。
显然,我还在Application.php的顶部添加了用法:
use multikanban\multikanban\Security\Http\EntryPoint\BasicAuthenticationEntryPoint;