我已经为自定义数据库查询进行了登录,存储库,它正在以其他方式工作,但没有进行数据库查询。使用 Symfony 3
在"提供程序"下的防火墙中:
providers:
in_memory:
memory: ~
database:
entity:
class: AppBundle:Customer
property: customer_email
encoders:
Symfony'Component'Security'Core'User'User:
algorithm: bcrypt
cost: 20
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_firewall:
pattern: ^/login$
anonymous: ~
main:
pattern: ^/
# activate different ways to authenticate
provider: database
form_login:
login_path: /login
check_path: /login_check
csrf_token_generator: security.csrf.token_manager
target_path_parameter: /dashboard
always_use_default_target_path: true
# http_basic: ~
# http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
# form_login: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
logout:
path: /logout
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
role_hierarchy:
ROLE_ADMIN: [ROLE_USER]
带查询的存储库:
<?php
# src/AppBundle/Entity/CustomerRepository.php
namespace AppBundle'Entity;
use Symfony'Bridge'Doctrine'Security'User'UserLoaderInterface;
use Symfony'Component'Security'Core'User'UserInterface;
use Symfony'Component'Security'Core'Exception'UsernameNotFoundException;
use Doctrine'ORM'EntityRepository;
class CustomerRepository extends EntityRepository implements UserLoaderInterface
{
public function loadUserByUsername($email)
{
$user = $this->createQueryBuilder('c')
->where('c.customer_email = :email')
->setParameter('email', $email)
->getQuery()
->getOneOrNullResult();
if(null === $user)
{
$message = sprintf(
'Unable to find an active user AppBundle:User object identified by "%s".',
$email
);
throw new UsernameNotFoundException($message);
print_r('eac');
}
return $user;
}
}
但是,调试器仍然说没有执行任何查询,它会抛出错误,并认为登录详细信息无效。
如果你在多个防火墙上检查Symfony文档,你将看到以下内容:
如果使用多个防火墙,并且针对一个防火墙进行身份验证,则不会自动针对任何其他防火墙进行身份验证。不同的防火墙就像不同的安全系统。为此,您必须为不同的防火墙显式指定相同的防火墙上下文。但通常对于大多数应用程序,拥有一个主防火墙就足够了。
实际发生的事情,您处于login_firewall
并试图登录 main
,这实际上应该永远不起作用。
对我有帮助的是:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
provider: database
anonymous: ~
form_login:
login_path: /login
check_path: /login
看到区别了吗?我删除了整个login_firewall
部分,并将anonymous: ~
添加到防火墙部分main
。
现在,如果您正确配置了access_control
部分,则除/login
之外的任何内容都不应匿名访问:
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }