簡體   English   中英

如何防止 Symfony 2.8 中的瀏覽器后退按鈕?

[英]How to prevent browser back button in Symfony 2.8?

處理 security.yml 文件以盡可能創建保留區域。 如何防止瀏覽器的返回按鈕?

這是我的 security.yml 文件的內容:


# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:

    # https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded

    encoders:
        AppBundle\Entity\User: bcrypt
        Symfony\Component\Security\Core\User\User: bcrypt

    providers:
        my_provider:
           entity:
              class: AppBundle:User
              property: username

        in_memory:
            memory:
                users:
                    admin: { password: $2y$13$voW4Dn5zM/uCMVcDM16KKeupoIMg2uf6t34SIhlZ6F7aIxEUKovk. }
    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs


        secured_area:
            anonymous: ~
            http_basic: ~
            pattern:    ^/
            form_login:
                login_path: /login
                check_path: /login
                username_parameter: _username
                password_parameter: _password
                always_use_default_target_path: true
                default_target_path: /home
                failure_path: /login
                remember_me: false
            logout:
                path: /logout
                target: /login
                invalidate_session: true
            access_denied_handler: app.security.access_denied_handler

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

        main:
            anonymous: ~
            # activate different ways to authenticate

            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
            #http_basic: ~

            # https://symfony.com/doc/current/security/form_login_setup.html
            #form_login: ~

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/$, roles: IS_AUTHENTICATED_FULLY }
        - { path: ^/home, roles: [ROLE_ADMIN, ROLE_TEACHER] }
        - { path: ^/prodotti, roles: ROLE_ADMIN }

這是我的控制器文件:


class SecurityController extends Controller {


      public function homeAction(Request $request) {

       if($this->get('security.context')->isGranted('ROLE_TEACHER')) {


       }else {

            return $this->redirect('http://symfony3.loc/login');  

       }

       die();
       return $this->render('AppBundle:Default:home.html.twig');

      }

      public function loginAction() {

            $authenticationUtils = $this->get('security.authentication_utils');
            $error = $authenticationUtils->getLastAuthenticationError();
            $lastUsername = $authenticationUtils->getLastUsername();          


            return $this->render('AppBundle:Default:alogin.html.twig',                   array('last_username' => $lastUsername, 'error' => $error));
      }

      public function login_checkAction() {

      }

      public function logoutAction(Request $request) {

           $session = new Session();
           $session->clear();

           return $this->redirect('http://symfony3.loc/login');

    }

這是路由文件:


home_page:
   path: /home
   defaults: { _controller: AppBundle:Security:home }

login:
   path: /login
   defaults: { _controller: AppBundle:Security:login }

logout:
   path: /logout
   defaults: { _controller: AppBundle:Security:logout }


login_check:
    path: /login_check

您可以在樹枝中編寫 JavaScript 代碼以防止點擊瀏覽器的后退按鈕

   <script type="text/javascript">
         history.pushState(null, null, document.URL);
         window.addEventListener('popstate', function () {
         history.pushState(null, null, document.URL);
         });
   </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM