4242import org .springframework .security .core .context .SecurityContext ;
4343import org .springframework .security .core .context .SecurityContextHolder ;
4444import org .springframework .security .core .userdetails .User ;
45+ import org .springframework .security .web .session .HttpSessionCreatedEvent ;
4546import org .springframework .security .web .session .HttpSessionDestroyedEvent ;
46- import org .springframework .session .Session ;
47- import org .springframework .session .events .SessionCreatedEvent ;
48- import org .springframework .session .events .SessionExpiredEvent ;
4947import org .springframework .stereotype .Service ;
5048import org .springframework .web .context .request .RequestContextHolder ;
5149import org .springframework .web .context .request .ServletRequestAttributes ;
5755import java .util .List ;
5856import java .util .Set ;
5957
60-
6158@ Service
6259public class UserService {
6360
@@ -188,7 +185,6 @@ public void onAbstractAuthenticationFailureEvent(AbstractAuthenticationFailureEv
188185 }
189186
190187 public void logout (Authentication auth ) {
191- // TODO test for anonymous users
192188 String userId = getUserId (auth );
193189 if (userId == null ) return ;
194190
@@ -198,7 +194,6 @@ public void logout(Authentication auth) {
198194 HttpSession session = ((ServletRequestAttributes ) RequestContextHolder .currentRequestAttributes ()).getRequest ().getSession ();
199195 session .setAttribute (ATTRIBUTE_USER_INITIATED_LOGOUT , "true" ); // mark that the user initiated the logout
200196
201-
202197 String sessionId = RequestContextHolder .currentRequestAttributes ().getSessionId ();
203198 applicationEventPublisher .publishEvent (new UserLogoutEvent (
204199 this ,
@@ -214,7 +209,6 @@ public void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event) {
214209
215210 log .info (String .format ("User logged in [user: %s]" , userName ));
216211
217- // TODO test for anonymous users
218212 String userId = getUserId (auth );
219213 applicationEventPublisher .publishEvent (new UserLoginEvent (
220214 this ,
@@ -245,8 +239,27 @@ public void onHttpSessionDestroyedEvent(HttpSessionDestroyedEvent event) {
245239 event .getSession ().getId (),
246240 true
247241 ));
242+ } else if (authBackend .getName ().equals ("none" )) {
243+ applicationEventPublisher .publishEvent (new UserLogoutEvent (
244+ this ,
245+ event .getSession ().getId (),
246+ event .getSession ().getId (),
247+ true
248+ ));
249+ log .info (String .format ("Anonymous user logged out [user: %s]" , event .getSession ().getId ()));
248250 }
249251 }
250252 }
251253
254+ @ EventListener
255+ public void onHttpSessionCreated (HttpSessionCreatedEvent event ) {
256+ if (authBackend .getName ().equals ("none" )) {
257+ applicationEventPublisher .publishEvent (new UserLoginEvent (
258+ this ,
259+ event .getSession ().getId (),
260+ event .getSession ().getId ()));
261+ }
262+ log .info (String .format ("Anonymous user logged in [user: %s]" , event .getSession ().getId ()));
263+ }
264+
252265}
0 commit comments