Tre*_*ent 15 security login symfony fosuserbundle
我希望能够通过ws登录.
我试图用curl指向模拟它,/login但它只处理HTML等.顺便说一下,它需要一个我不想要的CSRF.
所以我想要禁用CRSF(来自login_check)或找到自己做的方法.
我可以覆盖路由login_check被捕获时使用的LoginListener(它在哪里?).
有线索吗?
ado*_*uas 22
有许多方法可以为REST Web服务提供身份验证和授权,但最常用的方法似乎是OAuth.Facebook,Twitter,谷歌,Github等使用它.
Friends Of Symfony的人员有一个捆绑在Symfony2上实施OAuth身份验证和授权:https://github.com/FriendsOfSymfony/FOSOAuthServerBundle,我认为这正是你要找的.
编辑:有关Oauth的更多信息,Cloudfoundry的人们几天前发表了一篇有趣的文章.
关于您可以使用的其他选项,一个简单的基本身份验证:
firewalls:
main:
pattern: ^/rest
anonymous: ~
form_login: false
provider: fos_user_bundle
http_basic:
realm: "REST Service Realm"
Run Code Online (Sandbox Code Playgroud)
EDIT2:我看到仍然有人投票给我这个答案我认为需要注意的是,在撰写本答案时,JWT还不是一个选项,但在某些用例上,这可能是比OAuth更好的选择(例如,当您的应用程序将使用API时).所以这里是Symfony2/3的良好JWT实现的链接:https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md
您不应使用CURL通过Web服务对用户进行身份验证.
看一下ResettingController.php(在FOSUserBundle/Controller中)和LoginManager.php(在安全性中),有一个例子如何使用Symfony Security对用户进行身份验证:
控制器/ ResettingController.php
/**
* Authenticate a user with Symfony Security
*
* @param \FOS\UserBundle\Model\UserInterface $user
* @param \Symfony\Component\HttpFoundation\Response $response
*/
protected function authenticateUser(UserInterface $user, Response $response)
{
try {
$this->container->get('fos_user.security.login_manager')->loginUser(
$this->container->getParameter('fos_user.firewall_name'),
$user,
$response);
} catch (AccountStatusException $ex) {
// We simply do not authenticate users which do not pass the user
// checker (not enabled, expired, etc.).
}
}
Run Code Online (Sandbox Code Playgroud)
并在Security/LoginManager.php中
final public function loginUser($firewallName, UserInterface $user, Response $response = null)
{
$this->userChecker->checkPostAuth($user);
$token = $this->createToken($firewallName, $user);
if ($this->container->isScopeActive('request')) {
$this->sessionStrategy->onAuthentication($this->container->get('request'), $token);
if (null !== $response) {
$rememberMeServices = null;
if ($this->container->has('security.authentication.rememberme.services.persistent.'.$firewallName)) {
$rememberMeServices = $this->container->get('security.authentication.rememberme.services.persistent.'.$firewallName);
} elseif ($this->container->has('security.authentication.rememberme.services.simplehash.'.$firewallName)) {
$rememberMeServices = $this->container->get('security.authentication.rememberme.services.simplehash.'.$firewallName);
}
if ($rememberMeServices instanceof RememberMeServicesInterface) {
$rememberMeServices->loginSuccess($this->container->get('request'), $response, $token);
}
}
}
$this->securityContext->setToken($token);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20944 次 |
| 最近记录: |