加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – symfony2 security – 禁用登录路径并显示禁止

发布时间:2020-12-16 02:57:46 所属栏目:百科 来源:网络整理
导读:我试图阻止重定向登录页面,当用户试图访问没有令牌的页面时,我有单页应用程序,我只在防火墙下放置ajax请求,当用户正在执行没有令牌的ajax时,我希望ajax返回禁止例外所以我将能够在客户端捕获它 目前,ajax返回“Found”,因为请求被重定向到登录页面 到目前为
我试图阻止重定向登录页面,当用户试图访问没有令牌的页面时,我有单页应用程序,我只在防火墙下放置ajax请求,当用户正在执行没有令牌的ajax时,我希望ajax返回禁止例外所以我将能够在客户端捕获它

目前,ajax返回“Found”,因为请求被重定向到登录页面

到目前为止我还没有在cookbook中找到解决方案我不想使用api令牌,只发送异常而不是重定向到登录

解决方法

您需要为防火墙定义一个entry_point,以便您返回未经授权的响应.有关入口点的信息可以在文档 here中找到.我将复制段落,以备将来请求时使用.

When the user is not authenticated at all (i.e. when the token storage has no token yet),the firewall’s entry point will be called to “start” the authentication process. An entry point should implement AuthenticationEntryPointInterface,which has only one method: start(). This method receives the current Request object and the exception by which the exception listener was triggered. The method should return a Response object. This could be,for instance,the page containing the login form or,in the case of Basic HTTP authentication,a response with a WWW-Authenticate header,which will prompt the user to supply their username and password.

因此,为了让您这样做,您必须创建一个将被定义为服务的类.

它应该如下所示:

namespace MyBundleService;

use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationException;

class CustomEntryPoint implements AuthenticationEntryPointInterface
{

    public function start(Request $request,AuthenticationException $authException = null)
    {
        $response = new Response("",Response::HTTP_UNAUTHORIZED);

        return $response;
    }

}

并在您的services.yml文件中

services:
    service.entry_point:
        class: MyBundleServiceCustomEntryPoint

最后将服务ID service.entry_point传递给security.yml文件的防火墙部分中的entry_point选项.

这应该可以解决问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读