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; package org.ros.chatto.logged;
import java.time.Instant; import java.time.Instant;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener; import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.UserSession; import org.ros.chatto.model.UserSession;
import org.ros.chatto.repository.UserSessionRepository; import org.ros.chatto.repository.UserSessionRepository;
import org.ros.chatto.service.UserService; import org.ros.chatto.service.UserService;
import org.ros.chatto.service.UserTokenService; 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.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -27,9 +24,10 @@ import lombok.Setter;
public class LoggedUser implements HttpSessionBindingListener { public class LoggedUser implements HttpSessionBindingListener {
private final String username; private final String username;
private UserService userService; private final UserService userService;
private UserTokenService userTokenService; private final UserTokenService userTokenService;
private final Logger logger = LoggerFactory.getLogger(LoggedUser.class);
public LoggedUser(String username) { public LoggedUser(String username) {
this.username = username; this.username = username;
userService = BeanUtil.getBean(UserService.class); userService = BeanUtil.getBean(UserService.class);
@ -38,6 +36,8 @@ public class LoggedUser implements HttpSessionBindingListener {
public LoggedUser() { public LoggedUser() {
username = null; username = null;
userService = BeanUtil.getBean(UserService.class);
userTokenService = BeanUtil.getBean(UserTokenService.class);
} }
@Override @Override
@ -68,8 +68,8 @@ public class LoggedUser implements HttpSessionBindingListener {
public void valueUnbound(HttpSessionBindingEvent event) { public void valueUnbound(HttpSessionBindingEvent event) {
LoggedUser user = (LoggedUser) event.getValue(); LoggedUser user = (LoggedUser) event.getValue();
UserService userService = BeanUtil.getBean(UserService.class);; UserService userService = BeanUtil.getBean(UserService.class);
UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class);; UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class);
Instant instant = Instant.now(); Instant instant = Instant.now();
ChatUser chatUser = userService.findByUserName(user.getUsername()); ChatUser chatUser = userService.findByUserName(user.getUsername());
@ -78,21 +78,24 @@ public class LoggedUser implements HttpSessionBindingListener {
if (userSession == null) { if (userSession == null) {
// userSession = new UserSession(); // 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(); int numSessions = userSession.getNumSessions();
if (--numSessions == 0) { 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); userSession.setOnline(false);
userTokenService.deleteToken(chatUser.getUserName());
TokenCacheUtil.evictSingleTokenValue(chatUser.getUserName());
} }
userSession.setUser(chatUser); userSession.setUser(chatUser);
userSession.setTimeStamp(instant); userSession.setTimeStamp(instant);
userSession.setNumSessions(numSessions); userSession.setNumSessions(numSessions);
userSessionRepository.save(userSession); userSessionRepository.save(userSession);
userTokenService.deleteToken(chatUser.getUserName());
TokenCacheUtil.evictSingleTokenValue(chatUser.getUserName());
} }
} }