访问被拒绝后出现重定向时如何获取引荐页面?

How to get referrer page when there's a redirect after access denied?

每当权限不足的用户试图访问页面时,我都会通过在 security.yml

中将 access_denied_url 设置为 /login 字段将他重定向到登录页面

我的security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt
    access_denied_url: /login

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        secured_area:
            pattern:    ^/
            form_login:
                login_path: /login
                check_path: /login_check
                default_target_path: /
            logout:
                path:   /logout
                target: /
            anonymous: ~
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/tc, role: ROLE_TC }
        - { path: ^/operations, role: ROLE_OPERATIONS }

在我的 twig 模板中,我想获取用户尝试访问的页面,我该怎么做?

我尝试如下获取目标路径和referer路径,但它们都是空的

app.session.get('_security.secured_area.target_path')
app.request.headers.get('referer') 

你必须使用 use_referer

这是我的 security.yml 文件:

firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: security.csrf.token_manager
                use_referer: true
            logout:    true
            anonymous: true