【发布时间】:2015-04-15 08:30:48
【问题描述】:
我正在尝试创建一个应该使用 Web 服务的自定义连接。所以我阅读了security 上的教程和custom provider 上的这个教程。现在我正在尝试使用 3 个字段创建自己的登录表单:电子邮件、密码和号码。验证后,我了解到我的 /login_check 传入了函数 loadUserByUsername($username),但这个函数只接受了 $username 的参数并且不接受我的字段电子邮件和号码。要执行我的网络服务,我需要获取我的 3 个参数。如何自定义我的登录表单?
目标是:当用户提交登录表单时,我想发送一个带有登录表单参数的 Web 服务。如果我的响应没有错误,我想将由 Web 服务加载的用户连接到 symfony2 工具栏,否则我想显示一条错误消息。
你可以在这里看到我的代码:
Security.yml:
security:
encoders:
MonApp\MonBundle\Security\User\WebserviceUser: sha512
#Symfony\Component\Security\Core\User\User: plaintext
# http://symfony.com/doc/current/book/security.html#hierarchical-roles
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# http://symfony.com/doc/current/book/security.html#where-do-users-come- from-user-providers
providers:
#in_memory:
#memory:
#users:
#ryan: { password: ryanpass, roles: 'ROLE_USER' }
#admin: { password: kitten, roles: 'ROLE_ADMIN' }
webservice:
id: webservice_user_provider
# the main part of the security, where you can set up firewalls
# for specific sections of your app
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
area_secured:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
default_target_path: /test
logout:
path: /logout
target: /
# with these settings you can restrict or allow access for different parts
# of your application based on roles, ip, host or methods
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_AUTHENTICATED }
WebserviceUser.php:
<?php
namespace MonApp\MonBundle\Security\User;
use Symfony\Component\Security\Core\User\UserInterface;
class WebserviceUser implements UserInterface
{
private $email;
private $password;
private $num;
private $salt;
private $roles;
public function __construct($email, $password, $num, $salt, array $roles)
{
$this->email = $email;
$this->password = $password;
$this->num = $num;
$this->salt = $salt;
$this->roles = $roles;
}
public function getUsername()
{
return '';
}
public function getEmail()
{
return $this->email;
}
public function getPassword()
{
return $this->password;
}
public function getNum()
{
return $this->num;
}
public function getSalt()
{
return $this->salt;
}
public function getRoles()
{
return $this->roles;
}
public function eraseCredentials()
{}
public function isEqualTo(UserInterface $user)
{
if (!$user instanceof WebserviceUser) {
return false;
}
if ($this->email !== $user->getEmail()) {
return false;
}
if ($this->password !== $user->getPassword()) {
return false;
}
if ($this->num !== $user->getNum()) {
return false;
}
if ($this->getSalt() !== $user->getSalt()) {
return false;
}
return true;
}
}
WebserviceUserProvider.php
<?php
namespace MonApp\MonBundle\Security\User;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use MonApp\MonBundle\Security\User\WebserviceUser;
class WebserviceUserProvider implements UserProviderInterface
{
public function loadUserByUsername($username)
{
//print_r($username);
//die();
// effectuez un appel à votre service web ici
return new WebserviceUser('email', 'password', '45555', 'salt', array('ROLE_USER'));
//throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
public function refreshUser(UserInterface $user)
{
if (!$user instanceof WebserviceUser) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
print_r($user);
die();
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class)
{
return $class === 'MonApp\MonBundle\Security\User\WebserviceUser';
}
}
service.yml
parameters:
webservice_user_provider.class: MonApp\MonBundle\Security\User\WebserviceUserProvider
services:
webservice_user_provider:
class: "%webservice_user_provider.class%"
我不会放所有代码,但是我的登录动作、模板和路由与安全链接完全一样。但是我的用户new WebserviceUser('email', 'password', '45555', 'salt', array('ROLE_USER')) 没有连接到工具栏。所以我想我忘记了什么......
我是否需要使用 Listener、UserToken 和 Factory 来执行此操作?
【问题讨论】:
-
你正在使用匿名防火墙,也许你应该用它自己的工厂创建一个
WebServiceFirewall类。 -
谢谢 Med,你有我可以实现自己的防火墙的链接吗?我放了
anonymous: ~,因为我在食谱中看到了,关于安全的教程。 -
如果你还是卡在这里发帖,我会帮你symfony.com/fr/doc/current/cookbook/security/…
-
好的,我学习了所有的教程,但是,我应该使用哪种登录表单?它与此链接相同:symfony.com/doc/current/book/security.html?对于用户提供者,我应该使用我的自定义用户提供者吗?因为当我执行代码并删除
anonymous: ~时,我收到重定向错误到/login。这真的很难..谢谢 -
你能加入聊天吗?我创建了一个名为 Med 的房间,chat.stackoverflow.com/rooms/75270/med
标签: web-services security symfony authentication connection