doudi2833 2017-04-05 09:34
浏览 113
已采纳

Symfony:使用id登录后重定向到用户配置文件

In my application I wish to redirect users to profile after login, so I'm using an hidden input with name _target_path and value /profile/{userid} in my TWIG page (I've tried to make an IF statement to check when the app.user.username variable exist, but this isn't the right logic)

<input type="hidden" name="_target_path" value="/profile/{% if app.user.username is defined %}{{ app.user.username }}{% endif %}" />

This isn't working as expected cause naturally when I render the form I've not already set the username in session.

Here is my loginAction() in AuthController.php

/**
 * @Route("login/", name="login")
 */
public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');

    $error = $authenticationUtils->getLastAuthenticationError();

    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('auth/login.html.twig', array(
        'last_username' => $lastUsername,
        'error'         => $error,
    ));
}

I can reach the profile page using the parameter ID, so the url is like example.com/profile/1 without the ID it's causing a 404.

So I need to redirect the user after login to profile/{id} taking it from autenticationUtils

I'm really sure that my logic is incorrect, but I can't find nothing in the web that can solve my issue.

Here is my security.yml

security:
  providers:
    user_db:
        entity: { class: AppBundle\Entity\User, property: username }

  encoders:
    Symfony\Component\Security\Core\User\User:
        algorithm: bcrypt
        cost: 12

  firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        form_login:
            login_path: login
            check_path: login
        logout: true
        logout:
            csrf_parameter:       _csrf_token
            csrf_token_generator:  ~
            csrf_token_id:        logout
            path:                 /logout
            target:               /
            success_handler:      ~
            invalidate_session:   true
            delete_cookies:
                name:
                    path:                 null
                    domain:               null
            handlers:             []

    admin:
        pattern: ^/
        provider: user_db
        http_basic:
            realm: 'Admin Area'
            provider: in_memory
        form_login: ~



  access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/profilo, roles: [ROLE_USER, ROLE_ADMIN] }
  • 写回答

3条回答 默认 最新

  • douduocuima61392 2017-04-05 15:00
    关注

    What you need is changing the DefaultAuthenticationSuccessHandler (given you use the plain symfony mechanism wihtout any bundles like FOSUserBundle involved).

    First thing make your own Handler (or parts, here especially onAuthenticationSuccess):

    namespace ...
    
    use ...
    
    class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
    {
        /**
         * {@inheritdoc}
         */
        public function onAuthenticationSuccess(Request $request, TokenInterface $token)
        {
            **DO your logic here**
    
            //or call parent if you want default behaviour
            return parent::onAuthenticationSuccess($request, $token);
        }
    }
    

    Inject more services as needed.

    Second overwrite the default service in your DI:

    security.authentication.success_handler:
        class: AppBundle\Handler\AuthenticationSuccessHandler
        arguments: ['@security.http_utils', {}]
        tags:
            - { name: 'monolog.logger', channel: 'security' }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog