1

I want to remove logged out user from a Hashmap I have for logged in users but I don't find the way to do this as when I press the logout link. It just redirected to login page.

In spring security I have

 <logout invalidate-session="true" 
        logout-success-url="/" 
        logout-url="/logout.htm"/>

logout link is like

 <a href="logout.htm">Logout</a>

When I press this link it just go to my login mapping

 @RequestMapping("login")
public ModelAndView login(){}

and when I try to get user detail using

  SecurityContextHolder.getContext()
            .getAuthentication().getPrincipal();

it returns me anonymous user. So how can I get the logged out user detail.

Please let me know if you need more details.

Harry
  • 4,705
  • 17
  • 73
  • 101

4 Answers4

3

Add an implementation of org.springframework.security.web.authentication.logout.LogoutSuccessHandler interface as a bean to your security context.

Then you can use it:

<logout success-handler-ref="yourLogoutSuccessHandler" />

EDIT. As mentioned by Marcel this solution will not work out of the box because you can't mix success-handler-ref and logout-success-url attributes (reference). I prefer slightly different solution : instead of inheritance, you can use composition:

  1. Prepare configuratio for SimpleUrlLogoutSuccessHandler bean.
  2. Set up logout-success-url via corresponding defaultTargetUrl property.
  3. Inject SimpleUrlLogoutSuccessHandler bean into your CustomUrlLogoutSuccessHandler using LogoutSuccessHandler interface and call it after doing your stuff.

Advantage is that you will be less coupled with a framework code. So you will have less problems in a case of migration from Spring Security 3.1 to Spring Security Y.Y

Community
  • 1
  • 1
Maksym Demidas
  • 7,707
  • 1
  • 29
  • 36
2

The hint about the LogoutSuccessHandler is correct. However, you have to consider that configuring success-handler-ref and logout-success-url are mutually exclusive if I'm not mistaken. Hence, you need to implement the forwarding to URL manually in your success handler. Pointer: https://stackoverflow.com/a/6770785/131929

Community
  • 1
  • 1
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
0
Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
authentication.getName()
swamy
  • 1,200
  • 10
  • 23
0

In your applicationContext-security.xml file add the success-handler like below

< logout logout-url="/resources/j_spring_security_logout" success-handler-ref="com.mycompany.security.SpringSecurityLogoutHandler" />

Create the Class which will be implemneting org.springframework.security.web.authentication.logout.LogoutHandler interface and in it's logout method do all the stuff you want at the time of logout.

rpax
  • 4,468
  • 7
  • 33
  • 57