简体   繁体   中英

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

Whenever a user with insufficient privileges tries to access a page I redirect him to the login page by setting the access_denied_url to /login field in my security.yml

My 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 }

In my twig template I want to get the page the user tried to access so how can I do that?

I've tried to get the target path and the referer path as follows but both of them are empty

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

You have to use use_referer .

This is my security.yml file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM