UserBundle Symfony 2
UserBundle Symfony 2
我试图自己解决这个问题,但三天后,我放弃并寻求帮助。
我收到 "Bad credentials." 个错误(详细信息:pastebin)
这是我的 security.yml:
security:
encoders:
Common\UserBundle\Entity\User:
algorithm: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
request_users:
entity:
class: CommonUserBundle:User
property: username
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
request_security:
pattern: ^/
anonymous: ~
form_login:
check_path: _check_path
login_path: login_page
default_target_path: gemmi_start_homepage
logout:
path: _logout
target: signup_page
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
还有我的登录控制器:
<?php
namespace Common\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\Request;
class LoginController extends Controller
{
public function indexAction(Request $Request)
{
$Session = $this->get('session');
if($Request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$loginError = $Request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
} else {
$loginError = $Session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
}
$userName = $Session->get(SecurityContextInterface::LAST_USERNAME);
echo $loginError;
return $this->render('CommonUserBundle::login.html.twig', array(
'loginError' => $loginError,
'userName' => $userName
));
}
}
还有我的routing.yml:
login_page:
path: /login
defaults: { _controller: CommonUserBundle:Login:index }
signup_page:
path: /signup
defaults: { _controller: CommonUserBundle:Login:signup }
_check_path:
path: /login-check
_logout:
path: /logout
和:
gemmi_start_homepage:
path: /
defaults: { _controller: GemmiStartBundle:Default:index }
我的表格:
<form method="post" action="{{ path('_check_path') }}" name="contactform" id="contactform">
<fieldset>
<label for="login" accesskey="E"></label>
<input name="_username" type="text" id="login" size="30" value="{{ userName }}" placeholder="{% trans %} nickname {% endtrans %}" />
<br> <br>
<label for="passwd" accesskey="E"></label>
<input name="_password" type="password" id="passwd" size="30" value="" placeholder="{% trans %} password {% endtrans %}" />
<br> <br>
<button type="submit" class="submit btn btn-send" id="submit" value="Submit">
<i class="fa fa-user"></i>
{% trans %} log_in {% endtrans %}
</button>
</fieldset>
</form>
用户实体:pastebin
最后一个 - UserFixtures:pastebin
我知道有很多东西需要检查,但有人可以帮助我吗?
您是否注意到 security.yml
文件中缺少一个名为 firewalls:
的特定选项?或者您在粘贴代码时不小心遗漏了它?语法应该是这样的:
providers:
request_users:
entity:
class: CommonUserBundle:User
property: username
firewalls:
# your firewalls definitions goes here.
我很快应用了你的配置,我成功地通过了表单。虽然我没有完全通过身份验证,但它并没有给我抛出 Bad credentials
的错误。尝试编辑您的文件(如果确实缺少该行)并查看会发生什么。如果不是这样,我们将继续进一步调查问题。
我试图自己解决这个问题,但三天后,我放弃并寻求帮助。
我收到 "Bad credentials." 个错误(详细信息:pastebin)
这是我的 security.yml:
security:
encoders:
Common\UserBundle\Entity\User:
algorithm: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
request_users:
entity:
class: CommonUserBundle:User
property: username
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
request_security:
pattern: ^/
anonymous: ~
form_login:
check_path: _check_path
login_path: login_page
default_target_path: gemmi_start_homepage
logout:
path: _logout
target: signup_page
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
还有我的登录控制器:
<?php
namespace Common\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\Request;
class LoginController extends Controller
{
public function indexAction(Request $Request)
{
$Session = $this->get('session');
if($Request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$loginError = $Request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
} else {
$loginError = $Session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
}
$userName = $Session->get(SecurityContextInterface::LAST_USERNAME);
echo $loginError;
return $this->render('CommonUserBundle::login.html.twig', array(
'loginError' => $loginError,
'userName' => $userName
));
}
}
还有我的routing.yml:
login_page:
path: /login
defaults: { _controller: CommonUserBundle:Login:index }
signup_page:
path: /signup
defaults: { _controller: CommonUserBundle:Login:signup }
_check_path:
path: /login-check
_logout:
path: /logout
和:
gemmi_start_homepage:
path: /
defaults: { _controller: GemmiStartBundle:Default:index }
我的表格:
<form method="post" action="{{ path('_check_path') }}" name="contactform" id="contactform">
<fieldset>
<label for="login" accesskey="E"></label>
<input name="_username" type="text" id="login" size="30" value="{{ userName }}" placeholder="{% trans %} nickname {% endtrans %}" />
<br> <br>
<label for="passwd" accesskey="E"></label>
<input name="_password" type="password" id="passwd" size="30" value="" placeholder="{% trans %} password {% endtrans %}" />
<br> <br>
<button type="submit" class="submit btn btn-send" id="submit" value="Submit">
<i class="fa fa-user"></i>
{% trans %} log_in {% endtrans %}
</button>
</fieldset>
</form>
用户实体:pastebin
最后一个 - UserFixtures:pastebin
我知道有很多东西需要检查,但有人可以帮助我吗?
您是否注意到 security.yml
文件中缺少一个名为 firewalls:
的特定选项?或者您在粘贴代码时不小心遗漏了它?语法应该是这样的:
providers:
request_users:
entity:
class: CommonUserBundle:User
property: username
firewalls:
# your firewalls definitions goes here.
我很快应用了你的配置,我成功地通过了表单。虽然我没有完全通过身份验证,但它并没有给我抛出 Bad credentials
的错误。尝试编辑您的文件(如果确实缺少该行)并查看会发生什么。如果不是这样,我们将继续进一步调查问题。