# security.yml
security:
encoders:
ClAdminBundleEntityAdmin:
algorithm: sha1
encode_as_base64: false
iterations: 1
role_hierarchy:
ROLE_ADMIN: ROLE_ADMIN
providers:
cl_admin_security:
id: cl_admin_security_provider
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
admin_area:
pattern: ^/
provider: cl_admin_security
anonymous: ~
form_login:
login_path: /security
check_path: /security_check
default_target_path: /admin
username_parameter: _useremail
password_parameter: _userpassword
logout:
path: _demo_logout
target: _demo
access_control:
- { path: ^/security, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
# routing.yml
_security_check:
path: /security_check
_security:
path: /security
defaults: { _controller: ClSecurityBundle:Login:index }
// ClCommonBundleEntityAdminClSecurityProvider.php
namespace ClSecurityBundleSecurity;
use SymfonyComponentSecurityCoreUserUserProviderInterface;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSecurityCoreExceptionUsernameNotFoundException;
use SymfonyComponentSecurityCoreExceptionUnsupportedUserException;
use ClCommonBundleEntityAdmin;
use DoctrineORMEntityManager;
use DoctrineORMNoResultException;
class ClSecurityProvider implements UserProviderInterface
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* Loads the user for the given username.
*
* This method must throw UsernameNotFoundException if the user is not
* found.
*
* @throws UsernameNotFoundException if the user is not found
* @param string $username The username
*
* @return UserInterface
*/
public function loadUserByUsername($username)
{
$admin = $this->findUserBy(array("email" => $username));
if (!$admin) {
$message = sprintf(
'Unable to find an active admin ClCommonBundle:Admin object identified by "%s".', $admin
);
throw new UsernameNotFoundException($message);
}
return $admin;
}
public function refreshUser(UserInterface $admin)
{
//return $this->loadUserByUsername($admin->getUsername());
$class = get_class($admin);
if (!$this->supportsClass($class)) { //This should be $class not $user
$message = sprintf('Unsupported class type : %s', $class);
throw new UnsupportedUserException($message);
}
return $this->find($user->getId());
}
/**
* Whether this provider supports the given user class
*
* @param string $class
*
* @return Boolean
*/
public function supportsClass($class)
{
return $class == "ClCommonBundleEntityAdmin";
//return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
}
/**
* findUserBy
*
* @param array $criteria
*
* @return mixed
*/
protected function findUserBy(array $criteria)
{
$repository = $this->em->getRepository('ClCommonBundleEntityAdmin');
return $repository->findOneBy($criteria);
}
}
我发现没有为帐户"Cl\CommonBundle\Entity\Admin"配置编码器的问题。
我还在SecurityBundle\Security\ClsecurityProvider中实现了ClSecurityProvider.php
谁能帮我解决这个问题,我被击中了 3-4 天。我还没有完成自定义身份验证模块。我想使用我的自定义身份验证代码。
您有一个拼写错误:
encoders:
ClAdminBundleEntityAdmin
应该是
encoders:
ClCommonBundleEntityAdmin
我还应该指出,你的头衔有"Collegelife\CommonBundle\Entity\Admin"。
浪费时间编码车轮:)...试试这个适用于 Symfony2 的管理捆绑包,这真的很棒....
这是一个演示:
http://demo.sonata-project.org/admin/login ( 登录 : 管理员/通行证 : 管理员 (
这是官方文档
http://sonata-project.org/
我们在所有项目中都使用它,并使用fosUserBundle管理身份验证,这也是"必备"的捆绑包!!
https://github.com/FriendsOfSymfony/FOSUserBundle