第三章 Enhancing the User Experience(Logout)

[size=large]Understanding logout functionality[/size]

Adding a Log Out link to the site header

The signature URL for logging the user out is /j_spring_security_logout. Adding a logout link is really as simple as putting an anchor tag with the appropriate href in the header.jsp file:

<c:url value="/j_spring_security_logout" var="logoutUrl"/>
<li><a href="${logoutUrl}">Log Out</a></li>

[size=medium][color=blue]How logout works[/color][/size]

Remember that every request for a URL goes through the entire Spring Security filter chain, before being resolved to a servlet request. So, although the URL request for /j_spring_security_logout doesn't correspond to a JSP in our system, it doesn't have to be a real JSP or Spring MVC destination in order to be handled. These types of URLs are often referred to as [color=red]virtual URLs[/color].

The URL request for [color=red]/j_spring_security_logout is intercepted by the o.s.s.web.authentication.logout.LogoutFilter[/color]. One of the many filters in the default Spring Security filter chain, the LogoutFilter looks for this particular virtual URL and takes action.

Let's quickly examine the configuration that the security namespace provides us with regards to [color=red]default logout functionality[/color]:

<http auto-config="true" use-expressions="true">
<logout invalidate-session="true"
logout-success-url="/"
logout-url="/j_spring_security_logout"/>
</http>


This baseline configuration will look for the logout URL specified in the logout url attribute and log the user out. [color=red]Logging the user out involves the following 3 steps:[/color]

1. Invalidating the HTTP session (if invalidate-session is set to true).
2. Clearing the SecurityContext (effectively logging the user out).
3. Redirecting to the URL specified in logout-success-url.


[img]http://dl.iteye.com/upload/attachment/0063/1593/43c21be0-2bb7-3a87-b3ae-2f1f3fbecfcd.png[/img]

o.s.s.web.authentication.logout.LogoutHandler is an interface whose implementation classes can [color=red]be invoked upon user logout by the LogoutFilter[/color]. It is possible (although complex) to implement your own LogoutHandler that will be tied into the LogoutFilter lifecycle. The default set of LogoutHandlers that are configured with the LogoutFilter are responsible for clearing the session and cleaning up the remember me feature so that the user's session is now operating with no remaining authentication associated. Finally, the redirection to a URL after logout is performed by a default implementation of the interface o.s.s.web. authentication.logout.LogoutSuccessHandler. This default implementation simply redirects to the success URL configured (the default is /), but can be updated to perform anything else that your application needs to be done after the user is logged out. It is important to note that logout handlers should not throw exceptions, as it's important for all of them to execute to avoid potential inconsistency in the user's secured session. Take care that exceptions are properly handled and logged when implementing your own logout handlers.

[color=red][size=medium]Changing the logout URL[/size][/color]

Let's test overriding the default logout URL to provide a simple example of modifying the automatically configured behavior. We'll change the logout URL to /logout.

<http auto-config="true" use-expressions="true">
...
..<logout invalidate-session="true"
logout-success-url="/"
logout-url="/logout"/>
</http>


Restart the application and try it out! You'll observe that instead of /j_spring_ security_logout, the /logout URL will be used to log the user out. You may also notice that if you try /j_spring_security_logout, you'll get a Page not Found (404) error, because the URL doesn't correspond to an actual servlet resource and is no longer handled by a request filter.

[size=medium][color=red]Logout configuration directives[/color][/size]

[img]http://dl.iteye.com/upload/attachment/0063/1597/5a9b2e22-00af-3e6d-b584-ecae81d13e4b.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值