我正在symfony 3开发我的第一个项目。我实现了一个自定义用户提供程序,如symfony文档中所述。登录工作正常,但当我输入一个不存在的用户时,总是会收到错误消息“坏凭证”。我很想显示usernamenotfound验证中声明的消息,但它不起作用。如果我在我的security.yml中添加“hide\u user\u not\u found:false”,我会收到消息“username not found”,但我想在代码中显示我的消息(用户名“%s”不存在)。我还想实现CustomUserMessageAuthenticationException,但这是问题的第二部分。我希望有人能帮助我!这是代码。。。谢谢
代码与此处完全相同:http://symfony.com/doc/current/security/custom_provider.html
File: src/AppBundle/Security/User/WebserviceUser.php
namespace AppBundle\Security\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
class WebserviceUser implements UserInterface, EquatableInterface
{
private $username;
private $password;
private $salt;
private $roles;
public function __construct($username, $password, $salt, array $roles)
{
$this->username = $username;
$this->password = $password;
$this->salt = $salt;
$this->roles = $roles;
}
public function getRoles()
{
return $this->roles;
}
public function getPassword()
{
return $this->password;
}
public function getSalt()
{
return $this->salt;
}
public function getUsername()
{
return $this->username;
}
public function eraseCredentials()
{
}
public function isEqualTo(UserInterface $user)
{
if (!$user instanceof WebserviceUser) {
return false;
}
if ($this->password !== $user->getPassword()) {
return false;
}
if ($this->salt !== $user->getSalt()) {
return false;
}
if ($this->username !== $user->getUsername()) {
return false;
}
return true;
}
}
File: src/AppBundle/Security/User/WebserviceUserProvider.php
namespace AppBundle\Security\User;
use AppBundle\Security\User\WebserviceUser;
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;
class WebserviceUserProvider implements UserProviderInterface
{
public function loadUserByUsername($username)
{
// make a call to your webservice here
$userData = ...
// pretend it returns an array on success, false if there is no user
if ($userData) {
$password = '...';
// ...
return new WebserviceUser($username, $password, $salt, $roles);
}
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))
);
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class)
{
return WebserviceUser::class === $class;
}
}
文件:src\AppBundle\Controller\SecurityController.php
namespace AppBundle\Controller;
use AppBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Security\User\WebserviceUser;
class SecurityController extends BaseController
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error
));
}
}
文件:app\Resources\views\security\login.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<div class="wrapper fullscreen">
<div class="image_full">
</div>
<div class="small_content">
<a href="" class="logo_small"></a>
<form action="{{ path('login') }}" method="post">
{% if error %}
<p class="text-danger text-center">{{ error.messageKey|trans(error.messageData, 'security') }}</p>
{% endif %}
<div class="form-group">
<label for="exampleInputEmail1">Email-Adresse</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="_username" value="{{ last_username }}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Passwort</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Passwort" name="_password">
</div>
<div class="col-md-6 left">
<button type="submit" class="btn btn-default green btn_full">Anmelden</button>
<!--<a class="btn btn-default green btn_full" href="">Anmelden</a>-->
</div>
<div class="col-md-6 right">
<a class="btn btn-default green btn_full" href="{{ path('register') }}">Registrieren</a>
</div>
<!--<button type="submit" class="btn btn-default green btn_full">Einloggen</button>-->
</form>
</div>
</div>
{% endblock %}
文件:app\config\security.yml
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
#in_memory:
#memory: ~
webservice:
id: app.webservice_user_provider
hide_user_not_found: false
#erase_credentials: true
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
#dev:
# pattern: ^/(_(profiler|wdt)|css|images|js)/
# security: false
main:
anonymous: ~
form_login:
login_path: login
check_path: login
logout:
path: /logout
target: /
# activate different ways to authenticate
# 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
encoders:
AppBundle\Security\User\WebserviceUser: plaintext
我知道有两种方法可以更改错误凭据。
错误消息:
1)
# AppBundle/Resources/translations/messages.en.yml:
# or you can put this in app/Resources/translations/message.en.yml - has the same effect
"Bad credentials.": "Your custom error message here."
# app/Resources/Security/login.html.twig
...
{% if error %}
<div class="error">{{ error.message|trans }}</div>
{% endif %}
# app/config/config.yml, where %locale%, in this case, is set to "en"
framework:
translator: { fallback: "%locale%" }
2)
# login.html.twig
{% if error %}
<div>
{{ error.message|replace({"Bad credentials.":"Your custom error message here."}) }}
</div>
{% endif %}
你可以试试这两种方法中的一种,看看哪一种适合你的需要。
我正在使用Symfony Security和系统中的自定义用户提供程序。它通过web服务为用户提供服务。 我根据本教程配置提供程序(http://symfony.com/doc/current/cookbook/security/custom_provider.html). 这里是检查用户的功能: 这很好,函数使用用户名调用一个web服务,然后返回一个包含用户数据的数组。但是现在我需要通过另一个w
我将为我的网站创建自定义用户提供程序,对于用户来说,没有“用户名”和“密码”这样的概念(实际上有类似于密码的东西,但它的名称不同)。在文档中,用户实体必须实现来自安全包的UserInterface,该安全包具有诸如getUsername、getPassword之类的方法。我能用我自己的领域吗?或者我应该使用名称冲突(例如,getUsername将返回我的唯一字段)来实现我的行为吗?
我正在尝试创建一个自定义的KeyClope提供程序,它将为登录逻辑添加一些内容。我已经读过如何为KeyClope创建提供者(或插件),我正在与之合作的项目中已经有一个提供者(或插件),但我对它们知之甚少。 我需要为用户身份验证/授权添加自定义逻辑:我希望能够检查内部数据库中的一些字段来验证人员帐户。但是我没有找到任何关于类似情况的指南或好文章。有人能给我提供一些关于从什么开始的链接吗?为了实现这样
你好我按照这个教程:http://symfony.com/doc/current/cookbook/security/entity_provider.html My security.yml: 在数据库中,我有: 用户: 角色: 用户角色: 每次我尝试用DB中的一个用户登录时,我都会得到“坏凭据”。但是,如果我使用内存中定义的“test”用户登录,没有问题 我想这是我的安全问题。但我做错了什么?
我想将Firebase用于我的网络应用程序,该应用程序适用于养老院中的痴呆症患者。他们没有电子邮件或社交网络帐户,因此需要简单的用户名/密码注册/登录。 最简单的方法是什么?从我在文档中看到的情况来看,我必须使用自定义的身份验证流,但我没有现有的身份验证服务器。 如果我确实需要这样做,那么提供代币的最简单方法是什么?Azure中有函数,AWS有Lambda,但我看这里没有Firebase
因此,使用授权授予类型的基本OAuth2流通常如下所示(例如,假设OAuth客户端=Quora,OAuth服务器=Google): 用户转到客户端,被重定向到服务器登录页面进行身份验证。 用户登录到服务器,服务器返回authorization_code到客户端。 客户端然后调用client_id、client_secret和authorization_code服务器来获取令牌。 服务器验证并回复令