Token is delete and evicted from cache when num sessions hits 0. Also cleaned up loggeduser and added some logging

This commit is contained in:
Rohan Sircar 2019-11-21 18:12:59 +05:30
parent ed82bea521
commit 80c7c18881

View File

@ -1,22 +1,19 @@
package org.ros.chatto.logged;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.UserSession;
import org.ros.chatto.repository.UserSessionRepository;
import org.ros.chatto.service.UserService;
import org.ros.chatto.service.UserTokenService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import lombok.Getter;
import lombok.Setter;
@ -27,8 +24,9 @@ import lombok.Setter;
public class LoggedUser implements HttpSessionBindingListener {
private final String username;
private UserService userService;
private UserTokenService userTokenService;
private final UserService userService;
private final UserTokenService userTokenService;
private final Logger logger = LoggerFactory.getLogger(LoggedUser.class);
public LoggedUser(String username) {
this.username = username;
@ -38,6 +36,8 @@ public class LoggedUser implements HttpSessionBindingListener {
public LoggedUser() {
username = null;
userService = BeanUtil.getBean(UserService.class);
userTokenService = BeanUtil.getBean(UserTokenService.class);
}
@Override
@ -68,8 +68,8 @@ public class LoggedUser implements HttpSessionBindingListener {
public void valueUnbound(HttpSessionBindingEvent event) {
LoggedUser user = (LoggedUser) event.getValue();
UserService userService = BeanUtil.getBean(UserService.class);;
UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class);;
UserService userService = BeanUtil.getBean(UserService.class);
UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class);
Instant instant = Instant.now();
ChatUser chatUser = userService.findByUserName(user.getUsername());
@ -78,13 +78,18 @@ public class LoggedUser implements HttpSessionBindingListener {
if (userSession == null) {
// userSession = new UserSession();
throw new UsernameNotFoundException("User session not found");
logger.error("User session is somehow null for user: " + username);
throw new InternalAuthenticationServiceException("User session not found");
}
int numSessions = userSession.getNumSessions();
if (--numSessions == 0) {
logger.info("Num sessions is 0 so setting user to offline");
logger.info("Deleting token and evicting cache for user: " + chatUser.getUserName());
userSession.setOnline(false);
userTokenService.deleteToken(chatUser.getUserName());
TokenCacheUtil.evictSingleTokenValue(chatUser.getUserName());
}
userSession.setUser(chatUser);
@ -92,7 +97,5 @@ public class LoggedUser implements HttpSessionBindingListener {
userSession.setNumSessions(numSessions);
userSessionRepository.save(userSession);
userTokenService.deleteToken(chatUser.getUserName());
TokenCacheUtil.evictSingleTokenValue(chatUser.getUserName());
}
}