在FOSUserBundle中登录后,哪种方法可以实现基于角色的重定向?
我应该使用成功处理程序并在onAuthenticationSuccess中编写重定向逻辑
或者我应该使用安全侦听器并在onSecurityInteractiveLogin中编写重定向逻辑
我使用Symfony 2.1 RC1与FOSUserbundleWindows服务器上运行PHP 5.3.13.
我已按照此处的说明进行操作, 但Doctrine不会在数据库中为从基本FOS User类继承的属性创建字段(仅限我的类中的字段).
尝试使用FOS登录表单登录会产生错误:
Unrecognized field: usernameCanonical
Run Code Online (Sandbox Code Playgroud)
我有以下Doctrine配置:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
Run Code Online (Sandbox Code Playgroud)
FOSUserBundle配置如下所示:
fos_user:
db_driver: orm
firewall_name: main
user_class: SP\PickList\UserBundle\Entity\User
Run Code Online (Sandbox Code Playgroud)
我的用户实体:
namespace SP\PickList\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Document\User as BaseUser;
/**
* SP\PickList\UserBundle\Entity\User
*
* @ORM\Table(name="fos_user")
* @ORM\Entity
*/
class User extends BaseUser
{
/**
* @var integer $id
* …Run Code Online (Sandbox Code Playgroud) 我遇到了关于FOSUserBundle最重要的可能性的无数问题,并且在使用Twig的继承机制时发现了一些"设计不连贯",我想澄清一下,因为它在某些项目中真的令人不安......
从我读过的内容来看,我们应该如何覆盖模板.比方说,登录模板(Security/login.html.twig).首先,我需要覆盖全局FOSUser布局(layout.html.twig').
全球FOSUser布局
{% extends "::layout.html.twig" %}
{% block title %}Page title{% endblock title %}
{% block body %}
<div id="container">
{% block fos_user_content %}{% endblock %}
</div>
{% endblock body %}
Run Code Online (Sandbox Code Playgroud)
在title和body块到HTML引用<title>和<body>标签(全球).现在,谈到登录本身,这就是我写的内容.
登录表格
{% extends "MyUserBundle::layout.html.twig" %}
{% block fos_user_content %}
{% if error %}
<div class="error">{{ error|trans({}, 'FOSUserBundle') }}</div>
{% endif %}
<form action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<label …Run Code Online (Sandbox Code Playgroud) 我们为我们的用户使用 Symfony2 框架和 FOSUserBundle。所以我们有我们自己的 UserBundle,它继承自 FOSUserBundle。问题是:当我们使用错误的密码发送用于编辑用户的表单时,由于表单不正确,显示在标题中的 app.user.username 在不应该更改时发生了变化。然后当我们重新加载页面时, app.user.username 具有正常的用户名(因为它在数据库中没有改变)。问题是,为什么 app.user.username 会临时更改我们刚刚使用错误密码发送的表单(编辑用户)的值?
我们使用 FOSUser 的正常路由:
fos_user_security:
resource: "@FOSUserBundle/Resources/config/routing/security.xml"
fos_user_profile:
resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile
fos_user_register:
resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /register
fos_user_resetting:
resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
prefix: /resetting
fos_user_change_password:
resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /profile
Run Code Online (Sandbox Code Playgroud)
FOSUser 的默认控制器:
public function editAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request); …Run Code Online (Sandbox Code Playgroud) 我将 Synfony2 与 FOSUserBundle 一起使用,并且我有一个自定义 userChecker,我想在其中验证用户的主机(我们有多个主机指向同一 IP)。我的问题是,在我的自定义 userChecker 中,我无法访问 REQUEST,因此无法访问请求的主机。
这是我的用户检查代码
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//Override by Mattias
namespace BizTV\UserBundle\Controller;
//namespace Symfony\Component\Security\Core\User;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\LockedException;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserChecker as OriginalUserChecker;
use Symfony\Component\HttpFoundation\Request as Request; //ADDED BY MW
/**
* …Run Code Online (Sandbox Code Playgroud) 我将此 LexikJWTAuthenticationBundle 与 FosUserBundle 一起使用。
我在 security.yml 中有这个:
firewalls:
app:
pattern: ^/api
stateless: true
anonymous: true
lexik_jwt: ~
Run Code Online (Sandbox Code Playgroud)
具有以下 access_control :
- { path: ^/api/user/action1, roles: IS_AUTHENTICATED_FULLY }
- { path: ^/api/user/action2, roles: IS_AUTHENTICATED_ANONYMOUSLY }
Run Code Online (Sandbox Code Playgroud)
我期望 /api/user/action2 的行为是无论请求标头内有什么内容都可以访问。但是,在设置了授权承载但无效的情况下,我收到 401(使用有效令牌或根本没有授权承载都可以)。
我的用例是我需要检查我的控制器是否用户已登录,但如果没有,我仍然想让该匿名用户访问该路由。
我正在使用 Symfony 2.8.2 和 FOSUserBundle。当我尝试注销时,出现以下错误:
您必须在安全防火墙配置中激活注销
这是我的 security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/login
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
anonymous: true
logout:
path: /logout
target: /login
access_control:
- { path: ^/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/, role: ROLE_USER } …Run Code Online (Sandbox Code Playgroud) 我想根据用户的角色在登录后自定义重定向.
仅供参考:我用 symfony 2.8
我创建这个类:
<?php
namespace Users\UsersBundle\Redirection;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class AfterLoginRedirection implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
/**
* AfterLoginRedirection constructor.
* @param Router $router
* @param Security $security
*/
public function __construct(Router $router, Security $security)
{
$this->router = $router;
$this->security = $security;
}
/**
* @param Request $request
* @param TokenInterface $token
* @return RedirectResponse
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
$response …Run Code Online (Sandbox Code Playgroud) 我试图了解Symfony 2.8使用FOSUserBundle. 设置没有问题,一切正常,我只是想了解它是如何工作的。
登录表单将用户名+密码发送到定义在以下路径中的fos_user_security_check路由( ) :/login_checkFOSUserBunde\Resources\config\routing\security.xml
<route id="fos_user_security_check" path="/login_check" methods="POST">
<default key="_controller">FOSUserBundle:Security:check</default>
</route>
Run Code Online (Sandbox Code Playgroud)
所以FOSUserBundle:Security:checkaction负责处理请求。然而,实现看起来像这样:
public function checkAction() {
throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
}
Run Code Online (Sandbox Code Playgroud)
所以我查看了防火墙配置/app/config/security.yml:
security:
...
firewalls:
...
main:
...
form_login:
provider: fos_userbundle
csrf_provider: security.csrf.token_manager
login_path: fos_user_security_login
check_path: fos_user_security_check
Run Code Online (Sandbox Code Playgroud)
这里check_path也指fos_user_security_check... 那么,认证实际上是在哪里处理的呢?
首先,我尝试了与该主题相关的所有问题和答案。此外,我尝试了相关问题并尝试解决它,但没有成功。所以请仔细阅读我的问题。
我想覆盖 FOS 控制器。我成功重定向但没有得到完美的 /register/confirm/{token} 工作。我收到令牌错误。
出现错误: 无法解析“App\Controller\RegistrationController::confirmaction()”的参数 $token,也许您忘记将控制器注册为服务或错过使用“controller.service_arguments”对其进行标记?
我的代码
注册控制器.php
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route; …Run Code Online (Sandbox Code Playgroud) fosuserbundle ×10
symfony ×9
php ×5
forms ×2
doctrine-orm ×1
editing ×1
firewall ×1
jwt ×1
logout ×1
overriding ×1
security ×1
symfony-2.1 ×1
symfony4 ×1