@Override @Nonnull public AuthenticationDetails login(Credentials credentials) throws LoginException { checkNotNullArgument(credentials, "credentials should not be null"); SecurityContext previousSecurityContext = AppContext.getSecurityContext(); AppContext.setSecurityContext(new SecurityContext(serverSession)); AuthenticationDetails authenticationDetails = null; try { try (Transaction tx = persistence.createTransaction()) { publishBeforeLoginEvent(credentials); authenticationDetails = authenticateInternal(credentials); tx.commit(); userSessionManager.clearPermissionsOnUser(authenticationDetails.getSession()); setTimeZone(credentials, authenticationDetails); setSessionAttributes(credentials, authenticationDetails); storeSession(credentials, authenticationDetails); log.info("Logged in: {}", authenticationDetails.getSession()); publishUserLoggedInEvent(credentials, authenticationDetails); return authenticationDetails; } finally { publishAfterLoginEvent(credentials, authenticationDetails); } } finally { AppContext.setSecurityContext(previousSecurityContext); } }