我正在用symfony2为移动应用程序构建api。我已经安装了FOSAuthServerBundle&正确配置。
http://blog.tankist.de/blog/2013/07/18/oauth2-explained-part-3-using-oauth2-with-your-bare-hands/
我还能够用客户端凭据生成tokan。我还可以将用户重定向到Outh登录页面。我陷入了身份验证&授权部分。作为当前网站作为其自己的实体成员作为网站用户。有人能发布一些身份验证的例子吗;授权过程与FOSAuth一起工作。我是symfony的新手。
任何帮助都将不胜感激。
我刚刚为post-api进行了自定义登录操作。我没有使用任何第三方捆绑包进行后api授权。然而,我在我的项目中使用了以下第三方捆绑包来服务rest api,我只是在这里分享了我的自定义登录操作,这可能会对你有所帮助。我使用过以下捆绑包:JMSSerializerBundle,FOSRestBundle,NelmioApiDocBundle,NelmioCorsBundle,FOSUserBundle
<?php
namespace YoursiteApiBundleController;
use FOSRestBundleViewView;
use NelmioApiDocBundleAnnotationApiDoc;
use FOSUserBundleFOSUserEvents;
use SymfonyComponentHttpFoundationRequest;
class UserApiController extends Controller
{
/**
* Response header status code
*
* @var int
*/
private $statusCode = 200;
/**
* This action is used for user signin through api
*
* @ApiDoc(
* resource=true,
* description="User Post action for signin"
* )
*
* @param $request
* @return View view instance
*/
public function postAuthSigninAction(Request $request) {
$responseArr= array();
$view = new View();
$this->statusCode = 200;
$userData = json_decode($request->getContent(), true);
$email = $userData['email'];
$password = $userData['password'];
$um = $this->get('fos_user.user_manager');
$user = $um->findUserByUsernameOrEmail($email);
if (!$user instanceof YourSiteUserBundleEntityUser) {
$responseArr['meta']['error'] = "The email address or password is incorrect.";
$this->statusCode = 401;
return $view->create($responseArr, $this->statusCode);
}
$encoder_service = $this->get('security.encoder_factory');
$encoder = $encoder_service->getEncoder($user);
$encoded_pass = $encoder->encodePassword($password, $user->getSalt());
if ($encoded_pass != $user->getPassword()) {
$responseArr['meta']['error'] = "The email address or password is incorrect.";
$this->statusCode = 401;
return $view->create($responseArr, $this->statusCode);
}
$user->setToken(md5($password.time()));
$this->getDoctrine()->getManager()->persist($user);
$this->getDoctrine()->getManager()->flush();
$responseArr['data'] = $user;
return $view->create($responseArr, $this->statusCode);
}
}
?>