我在这里阅读如何在基于Symfony2的网站的树枝模板中检查用户的登录状态.但是,我需要知道如何检查用户是否从控制器内登录.我很确定以下代码是正确的:
$user = $this->get('security.context')->getToken()->getUser();
Run Code Online (Sandbox Code Playgroud)
但它总是返回一些东西,例如记录的用户或匿名用户.
任何的想法?提前致谢.
我想在注册过程后立即登录用户,而不通过登录表单.
这可能吗 ?我找到了一个解决方案FOSUserBundle,但我没有在我正在进行的项目中使用它.
这是我的security.yml,我正在使用两个防火墙.纯文本编码器仅用于测试.
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Ray\CentralBundle\Entity\Client: md5
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
users:
admin: { password: admin, roles: [ 'ROLE_ADMIN' ] }
entity:
entity: { class: Ray\CentralBundle\Entity\Client, property: email }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
user_login:
pattern: ^/user/login$
anonymous: ~
admin_login:
pattern: ^/admin/login$
anonymous: ~
admin:
pattern: ^/admin
provider: in_memory
form_login:
check_path: /admin/login/process
login_path: /admin/login
default_target_path: /admin/dashboard
logout:
path: /admin/logout
target: /
site:
pattern: ^/
provider: entity
anonymous: ~
form_login: …Run Code Online (Sandbox Code Playgroud) 我有两个防火墙:
api (用于API调用)main (其他一切)我的客户端应用程序登录通过main防火墙进行 但是,它确实与api防火墙下的端点交互以获取数据.这里的问题是我不想强迫用户第二次登录以对第二个防火墙进行身份验证.
如何只使用一个登录表单对两个防火墙进行身份验证?
在Symfony 2中,您可以设置注销目标,以便在注销后您将被重定向到/main.但是登录后您将被重定向到/.有没有办法为(成功)登录设置目标?
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/m/login$
security: false
secured_area:
pattern: ^/m
form_login:
check_path: /m/login_check
login_path: /m/login
logout:
path: /m/logout
target: /main
anonymous: ~
Run Code Online (Sandbox Code Playgroud) 我创建服务但它不起作用
services:
redirectionListener:
class: Front\EcommerceBundle\Listener\RedirectionListener
arguments: ["@service_container","@session"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Run Code Online (Sandbox Code Playgroud)
这是我的课
namespace Front\EcommerceBundle\Listener;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class RedirectionListener
{
public function __construct(ContainerBuilder $container, Session $session)
{
$this->session = $session;
$this->router = $container->get('router');
$this->securityContext = $container->get('security.context');
}
public function onKernelRequest(GetResponseEvent $event)
{
$route = $event->getRequest()->attributes->get('_route');
if ($route == 'livraison' || $route == 'validation') {
if ($this->session->has('panier')) {
if (count($this->session->get('panier')) == 0)
$event->setResponse(new RedirectResponse($this->router->generate('panier')));
}
if (!is_object($this->securityContext->getToken()->getUser())) {
$this->session->getFlashBag()->add('notification','Vous devez …Run Code Online (Sandbox Code Playgroud) 首先,我没有使用FOSUserBundle,我不能,因为我正在移植一个遗留系统,它有自己的模型层(没有Doctrine/Mongo /无论在这里)和其他非常自定义的行为.
我正在尝试将我的遗留角色系统与Symfony连接,因此我可以在控制器和视图中使用本机symfony安全性.
我的第一次尝试加载并返回该用户的所有角色在getRoles()从方法Symfony\Component\Security\Core\User\UserInterface.起初,它看起来像是有效的.但在深入了解之后,我注意到这些角色只有在用户登录时才会刷新.这意味着如果我从用户授予或撤消角色,他将必须注销并重新登录才能使更改生效.但是,如果我从用户撤消安全角色,我希望立即应用该角色,这样我就无法接受这种行为.
我希望Symfony做的是在每个请求上重新加载用户的角色,以确保它们是最新的.我已经实现了一个自定义用户提供程序,并且refreshUser(UserInterface $user)每次请求都会调用其方法,但不会以某种方式刷新角色.
在我的UserProvider中加载/刷新用户的代码如下所示:
public function loadUserByUsername($username) {
$user = UserModel::loadByUsername($username); // Loads a fresh user object including roles!
if (!$user) {
throw new UsernameNotFoundException("User not found");
}
return $user;
}
Run Code Online (Sandbox Code Playgroud)
(refreshUser看起来很相似)
有没有办法让Symfony在每个请求上刷新用户角色?
假设我有一个发票实体.发票属于某个用户(invoices.user_id).
如果用户输入myapp.com/invoices/1,则需要登录才能访问其发票.这很正常.
有时invoices.user_id为空(发票所有者在我们的系统中没有帐户),但我们有一个invoices.phone_number列.
目标是为我们系统中没有帐户的用户创建基于SMS代码验证的身份验证系统.如果用户确认他确实拥有与发票相关的电话号码(代码验证),我想授予他临时访问权限(15分钟)到此发票详细信息页面(仅限此页面).
我的第一个想法是使用存储在会话中的JWT令牌.
我的第二个想法是使用自定义防火墙.
有没有更好的方法?
我试图阻止重定向登录页面,当用户试图访问没有令牌的页面时,我有单页应用程序,我只在防火墙下放置ajax请求,当用户正在执行没有令牌的ajax时,我希望ajax返回禁止例外所以我将能够在客户端捕获它
目前,ajax返回"Found",因为请求被重定向到登录页面
到目前为止我还没有在cookbook中找到解决方案我不想使用api令牌,只发送异常而不是重定向到登录
我已经登录到主域。说出example.com(使用旧版kohana开发的应用程序)。我正在尝试登录subdmain,例如foo.bar.example.com。
foo.example.com是symfony应用。下面是我的配置。开发人员太栏显示“匿名”用户。它不会从cookie中的会话ID登录用户。
安全性
# 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: ~
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: ~
# 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
Run Code Online (Sandbox Code Playgroud)
配置文件
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: …Run Code Online (Sandbox Code Playgroud) 我想使用该security.interactive_login事件来更新我的用户的上次登录字段。
活动注册成功:
php bin/console debug:event-dispatcher security.interactive_login
Registered Listeners for "security.interactive_login" Event
===========================================================
------- ------------------------------------------------------------------------ ----------
Order Callable Priority
------- ------------------------------------------------------------------------ ----------
#1 App\EventSubscriber\UserLocaleSubscriber::onSecurityInteractiveLogin() 0
------- ------------------------------------------------------------------------ ----------
Run Code Online (Sandbox Code Playgroud)
但它落在Symfony 分析器中的Not Called Listens上。
这是事件订阅者:
class UserLocaleSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
/** @var User $user */
$user = $event->getAuthenticationToken()->getUser();
$user->setLastLoginAt(new DateTime());
$this->em->persist($user);
$this->em->flush();
}
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
]; …Run Code Online (Sandbox Code Playgroud) events symfony symfony-security symfony-eventdispatcher symfony5
symfony ×10
symfony-security ×10
php ×4
symfony-2.1 ×2
ajax ×1
events ×1
firewall ×1
login ×1
redirect ×1
roles ×1
security ×1
symfony-2.4 ×1
symfony5 ×1