more refactoring and token is now deleted from database on logout

This commit is contained in:
Rohan Sircar 2019-11-19 14:34:26 +05:30
parent 5e80815e53
commit 130525738a
3 changed files with 24 additions and 7 deletions

View File

@ -12,6 +12,7 @@ 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.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
@ -26,9 +27,13 @@ import lombok.Setter;
public class LoggedUser implements HttpSessionBindingListener {
private String username;
private UserService userService;
private UserTokenService userTokenService;
public LoggedUser(String username) {
this.username = username;
userService = BeanUtil.getBean(UserService.class);
userTokenService = BeanUtil.getBean(UserTokenService.class);
}
public LoggedUser() {
@ -85,5 +90,7 @@ public class LoggedUser implements HttpSessionBindingListener {
userSession.setTimeStamp(instant);
userSession.setNumSessions(numSessions);
userSessionRepository.save(userSession);
userTokenService.deleteToken(chatUser.getUserName());
}
}

View File

@ -2,18 +2,21 @@ package org.ros.chatto.repository;
import org.ros.chatto.model.UserToken;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
@CacheConfig(cacheNames = "userTokenCache")
public interface TokenRepository extends JpaRepository<UserToken, Long> {
@Cacheable(value = "userTokenCache", key = "#token")
@Query("select t from UserToken t where t.tokenContent = ?1")
public UserToken findByToken(String token);
@Cacheable(value = "userTokenCache", key = "#userName")
@Query("select t from UserToken t where t.userName = ?1")
public UserToken findByUserName(String userName);
@Modifying
@Query("delete from UserToken t where t.userName = ?1")
public void deleteByUserName(String userName);
}

View File

@ -5,21 +5,22 @@ import javax.transaction.Transactional;
import org.ros.chatto.model.UserToken;
import org.ros.chatto.repository.TokenRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserTokenService {
@Autowired
private TokenRepository tokenRepository;
// @Cacheable
@Cacheable(value = "userTokenCache", key = "#userName", unless="#result == null")
public UserToken getTokenByUserName(String userName)
{
return tokenRepository.findByUserName(userName);
}
@Cacheable(value = "userTokenCache", key = "#tokenString", unless="#result == null")
public UserToken getTokenByTokenString(String tokenString)
{
return tokenRepository.findByToken(tokenString);
@ -35,4 +36,10 @@ public class UserTokenService {
}
tokenRepository.save(userToken);
}
@Transactional
public void deleteToken(String userName)
{
tokenRepository.deleteByUserName(userName);
}
}