Symfony security return 401 response instead of redirect

前端 未结 2 508

I\'m writing an ajax application with ajax authentication and now I started using the symfony security component in silex to handle authentication/authorization.
Doing a

2条回答
  •  执笔经年
    2020-12-28 09:50

    What you need is a AuthenticationEntryPoint handler. Simple example:

    class AuthenticationEntryPoint implements AuthenticationEntryPointInterface {
    
    /**
     * Starts the authentication scheme.
     *
     * @param Request $request The request that resulted in an AuthenticationException
     * @param AuthenticationException $authException The exception that started the authentication process
     *
     * @return Response
     */
    public function start(Request $request, AuthenticationException $authException = null)
    {
        $array = array('success' => false);
        $response = new Response(json_encode($array), 401);
        $response->headers->set('Content-Type', 'application/json');
    
        return $response;
    }
    }
    

    Register class as a service in services.xml file:

    
        YourNameSpace\AuthenticationEntryPoint
    
    
    
        
    
    

    and make a small change in security.yml file:

    security:
      firewalls:
        somename:
          entry_point: authentication_entry_point
    

提交回复
热议问题