php - How do I change security redirect behavior in Silex? -
php - How do I change security redirect behavior in Silex? -
if create ajax request secured area, sends 302 header , redirects login page in request. there way configure firewall give different header , not redirect? need handle in ->before call? or improve way override authentication entry point, suggested in symfony security homecoming 401 response instead of redirect? symfony not sure if silex has improve sugar.
tried , other things:
$app->register(new securityserviceprovider(), array( 'security.firewalls' => array( 'default' => array( 'pattern' => '^/', 'anonymous' => true, 'oauth' => array( 'failure_path' => '/login', 'with_csrf' => true ), 'logout' => array( 'logout_path' => '/logout', 'with_csrf' => true ), ) ), )); $app['security.authentication.entry_point.default.oauth'] = $app->share(function ($app) { homecoming new authenticationentrypointcomponent(); }); $app['security.authentication.entry_point.default.form'] = $app->share(function ($app) { homecoming new authenticationentrypointcomponent(); }); default name of key in security.firewalls. still getting 302.
i had dig around much code got it:
my controller:
$app['security.entry_point.form._proto'] =$app->protect(function ($name, array $options) utilize ($app) { homecoming $app->share(function () utilize ($app, $options) { $login_path = isset($options['login_path']) ? $options['login_path'] : '/login'; homecoming new mycustomclass($app['security.http_utils'], $login_path); }); }); the class:
namespace yours; utilize symfony\component\security\http\entrypoint\authenticationentrypointinterface; utilize symfony\component\security\core\exception\authenticationexception; utilize symfony\component\security\http\httputils; utilize symfony\component\httpfoundation\request; utilize symfony\component\httpfoundation\response; class mycustomclass implements authenticationentrypointinterface { protected $_login_path = '/login'; protected $_http_tools; /** * @param string $loginpath path login form */ public function __construct(httputils $http_utils, $login_path = '') { $this->_http_tools = $http_utils; if ( $login_path ) { $this->_login_path = $login_path; } } /** * {@inheritdoc} */ public function start(request $request, authenticationexception $authexception = null) { if ($request->isxmlhttprequest()) { $response = new response(json_encode([]), 401); $response->headers->set('content-type', 'application/json'); homecoming $response; } homecoming $this->_http_tools->createredirectresponse($request, $this->_login_path); } } php symfony2 silex
Comments
Post a Comment