![](/img/trans.png)
[英]Using the RESTful API package “FOSRestBundle” outside of a Symfony2 application
[英]Symfony2 App with RESTful authentication, using FOSRestBundle and FOSUserBundle
我正在为我的JS驱动的应用程序制作REST API。
在登录期间,登录表单通过AJAX提交到我的API的url /rest/login
。
虽然我已经为API和应用程序本身分离了防火墙,但它们共享相同的上下文,这应该意味着,当用户对API进行身份验证时,他也会针对应用程序进行身份验证。 因此,当服务器返回204时,页面将重新加载,它应该将用户重定向到应用程序,因为他现在已登录。
我尝试使用FOSUserBundle的预制check_login
页面并指向/rest/login
。
login:
path: /rest/login
defaults:
_controller: FOSUserBundle:Security:check
methods: [ POST ]
这不起作用,因为它总是返回重定向,无论如何。 我阅读了symfony的文档,但无法找到如何创建自定义check_login
页面。 我需要的是这样的事情
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use FOS\RestBundle\Controller\Annotations\View;
class SecurityController {
/**
* @View(statusCode=204)
*/
public function loginAction($username, $password) {
/* first I need to somehow authenticate user
using normal authentication, that I've set up */
...
/* Then I need to return 204 or throw exception,
based on result.
This is done using FOSRestBundle and it's
listeners. */
if(!$succesful) {
throw new AuthenticationException();
}
}
}
我不知道该怎么做。 我在任何文档中找不到任何帮助我的东西。 我将感谢能够指出正确方向的任何建议。
编辑:为了进一步简化,我的目标是什么。 我希望我的登录功能与普通的form_login完全相同 。 我只想改变它发回的响应 - 而不是重定向我想成功204和失败401。
我找到了一个简单的解决方案。 我只需要编写一个实现AuthenticationSuccessHandlerInterface
和AuthenticationFailureHandlerInterface
。
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
class AuthenticationRestHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface {
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) {
return new Response('', Response::HTTP_UNAUTHORIZED);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
return new Response('', Response::HTTP_NO_CONTENT);
}
}
然后我将其注册为服务并配置为防火墙的处理程序。
services:
security.authentication_rest_handler:
class: AuthenticationRestHandler
security:
firewalls:
rest:
pattern: ^rest
context: app
form_login:
check_path: /rest/login
provider: fos_userbundle
failure_handler: inspireon.security.authentication_rest_handler
success_handler: inspireon.security.authentication_rest_handler
username_parameter: login
password_parameter: password
问题解决了,无需复杂的身份验证提供程序:-)
我理解你的问题,因为我通过类似的情况,但使用SOAP服务。 在中间,我可以重新搜索安全性wsse,Symfony2已经提供了解决方案
http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html
它使用真实令牌,您可以在FOSUserBundle中与用户匹配。 我认为唯一的想法是你要比较的字段“密码”与数据库中的字段(加密)相同,然后我决定只为此用途创建一个额外的字段。
我希望它对你有所帮助。
问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.