From 130525738a7fca06cdde4982ace2663ce2ae6c5f Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Tue, 19 Nov 2019 14:34:26 +0530 Subject: [PATCH] more refactoring and token is now deleted from database on logout --- .../main/java/org/ros/chatto/logged/LoggedUser.java | 9 ++++++++- .../org/ros/chatto/repository/TokenRepository.java | 9 ++++++--- .../org/ros/chatto/service/UserTokenService.java | 13 ++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java b/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java index ea2dfd5..6e31759 100644 --- a/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java +++ b/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java @@ -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()); } } \ No newline at end of file diff --git a/chatto/src/main/java/org/ros/chatto/repository/TokenRepository.java b/chatto/src/main/java/org/ros/chatto/repository/TokenRepository.java index 572e61a..67c06b6 100644 --- a/chatto/src/main/java/org/ros/chatto/repository/TokenRepository.java +++ b/chatto/src/main/java/org/ros/chatto/repository/TokenRepository.java @@ -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 { - @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); } diff --git a/chatto/src/main/java/org/ros/chatto/service/UserTokenService.java b/chatto/src/main/java/org/ros/chatto/service/UserTokenService.java index 95e2c52..b3ecaba 100644 --- a/chatto/src/main/java/org/ros/chatto/service/UserTokenService.java +++ b/chatto/src/main/java/org/ros/chatto/service/UserTokenService.java @@ -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); + } }