Quit Symfony 2

this is my first symfony 2 application and i am trying to log out of the current logged in user.

This is my application /config/security.yml

security: encoders: Symfony\Component\Security\Core\User\User: plaintext role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] providers: in_memory: memory: users: user0: { password: user0, roles: [ 'ROLE_ADMIN' ] } user1: { password: user1, roles: [ 'ROLE_SUPER_ADMIN' ] } firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false login: pattern: ^/demo/secured/login$ security: false secured_area: pattern: ^/ logout: ~ anonymous: ~ http_basic: realm: "Secured Area" access_control: - { path: ^/question/*, roles: ROLE_ADMIN } - { path: ^/questiongroup/*, roles: ROLE_ADMIN } - { path: ^/answer/*, roles: ROLE_ADMIN } - { path: ^/newslettertemplate/*, roles: ROLE_ADMIN } - { path: ^/customer/*, roles: ROLE_SUPER_ADMIN } - { path: ^/statistics/*, roles: ROLE_SUPER_ADMIN } 

I created a logout entry in the routing.yml file as described in the Symfony security documentation:

 logout: path: /logout 

When I create a link to "logout", I get a redirect to "/", which is good. But the user is still authenticated, meaning the actual logout is not working.

+6
source share
3 answers

It does not work with basic HTTP authentication, because the browser remembers your credentials and sends them with every request. You can do nothing with this on the server side.

I believe that in the end you will go to the login form . The logout function will work as expected when you do this.

+11
source

Just use this in security.yml

 logout: path: /logout invalidate_session: false 
+5
source

If you are like me, you are new to symfony and cannot force others to work with outputs (I suppose I missed some configs) there is not an academic, but a functional solution:

when you use the login form , you just need to send an undefined login and password to the 'login_check' route.

ex: login = '*' password = ''

with a button in the template:

 <form action="{{ url('login_check') }}" method="post"> <input type="text" name="_username" value="*" style="display:none;" /> <input type="password" name="_password" style="display:none;" /> <button type="submit">log out</button> </form> 

creating the "logout" template from the controller:

 <script> window.addEventListener("load",function(){ document.getElementById("logout_form").submit(); }); </script> <form action="{{ url('login_check') }}" method="post" id="logout_form"> <input type="text" name="_username" value="*" style="display:none;" /> <input type="password" name="_password" style="display:none;" /> </form> 
0
source

Source: https://habr.com/ru/post/953795/


All Articles