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:
parent
ed82bea521
commit
80c7c18881
@ -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,9 +24,10 @@ 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;
|
||||
userService = BeanUtil.getBean(UserService.class);
|
||||
@ -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,21 +78,24 @@ 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);
|
||||
userSession.setTimeStamp(instant);
|
||||
userSession.setNumSessions(numSessions);
|
||||
userSessionRepository.save(userSession);
|
||||
|
||||
userTokenService.deleteToken(chatUser.getUserName());
|
||||
TokenCacheUtil.evictSingleTokenValue(chatUser.getUserName());
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user