php – Symfony安全性返回401响应而不是重定向
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Symfony安全性返回401响应而不是重定向,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1657字,纯文字阅读大概需要3分钟。
内容图文
![php – Symfony安全性返回401响应而不是重定向](/upload/InfoBanner/zyjiaocheng/702/f8d82f911c2a4f6aa3cc52668e633ac7.jpg)
我正在编写一个带有ajax身份验证的ajax应用程序,现在我开始在silex中使用symfony安全组件来处理身份验证/授权.
使用简单配置进行简单测试,我通过防火墙进入受保护区域,我得到的响应是重定向到/ login页面,但我在应用程序中需要的是401响应,可能有其他信息(在标题或关于如何登录的json body.
$app['security.firewalls'] = [
'api' => [
'pattern' => '^/api',
'logout' => ['logout_path'=>'/auth/logout'],
'users' => $app->share(function(Application $app) {
return new MyUserProvider();
})
]
];
编辑:我有一个提示,但我不知道如何使用它.使用AuthenticationEntryPointInterface实现入口点我可以告诉api如何回答未经身份验证的请求并向用户提供身份验证所需的指令.这可能是我的401响应登录说明.
解决方法:
您需要的是AuthenticationEntryPoint处理程序.
简单的例子:
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;
}
}
将类注册为services.xml文件中的服务:
<parameters>
<parameter key="authentication_entry_point.class">YourNameSpace\AuthenticationEntryPoint</parameter>
</parameters>
<services>
<service id="authentication_entry_point" class="%authentication_entry_point.class%"/>
</services>
并在security.yml文件中进行一些小改动:
security:
firewalls:
somename:
entry_point: authentication_entry_point
内容总结
以上是互联网集市为您收集整理的php – Symfony安全性返回401响应而不是重定向全部内容,希望文章能够帮你解决php – Symfony安全性返回401响应而不是重定向所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。