roy*_*roy 6 ajax symfony symfony-2.4 symfony-security
我试图阻止重定向登录页面,当用户试图访问没有令牌的页面时,我有单页应用程序,我只在防火墙下放置ajax请求,当用户正在执行没有令牌的ajax时,我希望ajax返回禁止例外所以我将能够在客户端捕获它
目前,ajax返回"Found",因为请求被重定向到登录页面
到目前为止我还没有在cookbook中找到解决方案我不想使用api令牌,只发送异常而不是重定向到登录
您需要为entry_point防火墙定义一个,以便您返回未经授权的响应.有关入口点的信息可以在此处的文档中找到.如果将来要求,我会复制该段落.
当用户根本没有被认证时(即当令牌存储器还没有令牌时),将调用防火墙的入口点以"开始"认证过程.入口点应该实现AuthenticationEntryPointInterface,它只有一个方法:start().此方法接收当前Request对象以及触发异常侦听器的异常.该方法应返回一个Response对象.例如,这可以是包含登录表单的页面,或者在基本HTTP身份验证的情况下,可以是具有WWW-Authenticate标头的响应,该响应将提示用户提供其用户名和密码.
因此,为了让您这样做,您必须创建一个将被定义为服务的类.
它应该如下所示:
namespace MyBundle\Service;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
class CustomEntryPoint implements AuthenticationEntryPointInterface
{
public function start(Request $request, AuthenticationException $authException = null)
{
$response = new Response("", Response::HTTP_UNAUTHORIZED);
return $response;
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的services.yml文件中
services:
service.entry_point:
class: MyBundle\Service\CustomEntryPoint
Run Code Online (Sandbox Code Playgroud)
最后将服务ID传递service.entry_point给文件部分中的entry_point选项.firewallsecurity.yml
这应该可以解决问题.
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |