Php 如何从安全组件覆盖类?
我在API(Silex)中使用Basic Auth,端点从客户端接收user+pw,通过Basic Auth验证用户,然后返回令牌以用于进一步请求。现在,当我的应用程序调用AJAX时,如果凭据正确,一切都会顺利进行。如果凭据错误,API将返回401和set WWW Authenticate头。这会使浏览器自动显示默认的浏览器登录表单 我不想那样。在StackOverflow中,他们说仅有的两种解决方案是要么返回400而不是401,要么将WWW认证头更改为类似“基于表单”的内容 在安全组件的BasicAuthenticationEntryPoint.php中,statusCode设置为401,WWW Authentication设置为“Basic…”Php 如何从安全组件覆盖类?,php,symfony,authentication,silex,Php,Symfony,Authentication,Silex,我在API(Silex)中使用Basic Auth,端点从客户端接收user+pw,通过Basic Auth验证用户,然后返回令牌以用于进一步请求。现在,当我的应用程序调用AJAX时,如果凭据正确,一切都会顺利进行。如果凭据错误,API将返回401和set WWW Authenticate头。这会使浏览器自动显示默认的浏览器登录表单 我不想那样。在StackOverflow中,他们说仅有的两种解决方案是要么返回400而不是401,要么将WWW认证头更改为类似“基于表单”的内容 在安全组件的Bas
如果我在那里应用更改,它会起作用。。。但我需要把它作为我的C项目的一部分。。。我应该如何覆盖Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint.php以使其适应我的需要?知道有没有解决办法?我知道这应该是一个非常常见的问题,通常是如何解决的?好的,如果有人想知道,我会这样做: 首先,在我的安全文件夹中,我创建了自己的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;
}
}
“main”是我的防火墙名称
显然,我还在Application.php的顶部添加了用法:
use multikanban\multikanban\Security\Http\EntryPoint\BasicAuthenticationEntryPoint;
所以你不想在Web服务器上使用http身份验证,而是想在你自己的登录系统上使用?我想在Web服务器上使用基本身份验证。但我只是为了请求返回令牌。给定凭据,我正在验证用户并返回令牌,这样我就不需要在后端保留状态,但js应用程序只需要在将来的每个请求中添加令牌。好的,听起来你只需要创建自己的令牌,然后使用它设置一个允许你定义自己的入口点类的入口点。我不确定编排是如何运作的,所以我现在不能给你提供详细信息,我只能说这是可能的,并为你指出了这一总体方向。谢谢,这非常有帮助!看起来这正是我需要的,但我不知道该把“$this['security.entry\u point.main.http']=$this->share…”代码行放在哪里?AppKernel.php?对不起,我错过了什么。我唯一拥有的Application.php是vendor/symfony中的Application.php,我相信您永远都不想更改此文件……以防有人在寻找这个问题的详细答案:TL;DR:您可以通过防火墙下的入口点配置设置自己的“入口点”来完成此操作:。只需创建一个实现AuthenticationEntryPointInterface的类,将其注册为服务,并将id放在那里
use multikanban\multikanban\Security\Http\EntryPoint\BasicAuthenticationEntryPoint;