Hi.
I manage users current status (online/offline) in my application, and I use
andpublic class AccountAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Inject
private AccountActiveStatusService accountActiveStatusService;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
Account account = (Account) authentication.getPrincipal();
accountActiveStatusService.setOnline(account);
super.onAuthenticationSuccess(request, response, authentication);
}
}
where security config is:public class AccountLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
@Inject
private AccountActiveStatusService accountActiveStatusService;
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
Account account = (Account) authentication.getPrincipal();
accountActiveStatusService.setOffline(account);
super.onLogoutSuccess(request, response, authentication);
}
}
It does what I want. But I also would like to set user offline when session times out.<http use-expressions="true">
<form-login login-page="/signin"
login-processing-url="/signin/authenticate"
authentication-success-handler-ref="accountAuthenticationSuccessHandler"
authentication-failure-url="/signin?error=1"/>
<logout logout-url="/signout"
delete-cookies="JSESSIONID"
success-handler-ref="accountLogoutSuccessHandler"/>
...
</http>
I tried several approaches:
- Implemented my web application listener class and registered it through web.xml
But when<listener>
<listener-class>com.myappl.web.listeners.AccountOfflineSessi onLifecycleListener</listener-class>
</listener>method is calledsessionDestroyedreturns null, so I can't define the account for which session timed out, so I can change user's status to offline.SecurityContextHolder.getContext().getAuthenticati on();
Also I tried to implement Spring:ApplicationListener
But in this casepublic class HttpSessionDestroyedApplicationEventListener implements ApplicationListener<HttpSessionDestroyedEvent> {
@Override
public void onApplicationEvent(HttpSessionDestroyedEvent event) {
event.getSecurityContexts();
}
}is empty, andevent.getSecurityContexts()also returns null.SecurityContextHolder.getContext().getAuthenticati on();
HttpSessionEventPublisher is registered in my web.xml:
So, there is the question how I can obtain Authentication object on the session timeout. And maybe there is a better way to manage user's status than the one I described above.<listener>
<listener-class>org.springframework.security.web.session.Htt pSessionEventPublisher</listener-class>
</listener>
Thank you.


Reply With Quote