more refactoring and token is now deleted from database on logout
This commit is contained in:
parent
5e80815e53
commit
130525738a
@ -12,6 +12,7 @@ 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.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
@ -26,9 +27,13 @@ import lombok.Setter;
|
|||||||
public class LoggedUser implements HttpSessionBindingListener {
|
public class LoggedUser implements HttpSessionBindingListener {
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
private UserService userService;
|
||||||
|
private UserTokenService userTokenService;
|
||||||
|
|
||||||
public LoggedUser(String username) {
|
public LoggedUser(String username) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
|
userService = BeanUtil.getBean(UserService.class);
|
||||||
|
userTokenService = BeanUtil.getBean(UserTokenService.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoggedUser() {
|
public LoggedUser() {
|
||||||
@ -85,5 +90,7 @@ public class LoggedUser implements HttpSessionBindingListener {
|
|||||||
userSession.setTimeStamp(instant);
|
userSession.setTimeStamp(instant);
|
||||||
userSession.setNumSessions(numSessions);
|
userSession.setNumSessions(numSessions);
|
||||||
userSessionRepository.save(userSession);
|
userSessionRepository.save(userSession);
|
||||||
|
|
||||||
|
userTokenService.deleteToken(chatUser.getUserName());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,18 +2,21 @@ package org.ros.chatto.repository;
|
|||||||
|
|
||||||
import org.ros.chatto.model.UserToken;
|
import org.ros.chatto.model.UserToken;
|
||||||
import org.springframework.cache.annotation.CacheConfig;
|
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.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
@CacheConfig(cacheNames = "userTokenCache")
|
@CacheConfig(cacheNames = "userTokenCache")
|
||||||
public interface TokenRepository extends JpaRepository<UserToken, Long> {
|
public interface TokenRepository extends JpaRepository<UserToken, Long> {
|
||||||
@Cacheable(value = "userTokenCache", key = "#token")
|
|
||||||
@Query("select t from UserToken t where t.tokenContent = ?1")
|
@Query("select t from UserToken t where t.tokenContent = ?1")
|
||||||
public UserToken findByToken(String token);
|
public UserToken findByToken(String token);
|
||||||
@Cacheable(value = "userTokenCache", key = "#userName")
|
|
||||||
@Query("select t from UserToken t where t.userName = ?1")
|
@Query("select t from UserToken t where t.userName = ?1")
|
||||||
public UserToken findByUserName(String userName);
|
public UserToken findByUserName(String userName);
|
||||||
|
@Modifying
|
||||||
|
@Query("delete from UserToken t where t.userName = ?1")
|
||||||
|
public void deleteByUserName(String userName);
|
||||||
}
|
}
|
||||||
|
@ -5,21 +5,22 @@ import javax.transaction.Transactional;
|
|||||||
import org.ros.chatto.model.UserToken;
|
import org.ros.chatto.model.UserToken;
|
||||||
import org.ros.chatto.repository.TokenRepository;
|
import org.ros.chatto.repository.TokenRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
|
||||||
public class UserTokenService {
|
public class UserTokenService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private TokenRepository tokenRepository;
|
private TokenRepository tokenRepository;
|
||||||
|
|
||||||
|
@Cacheable(value = "userTokenCache", key = "#userName", unless="#result == null")
|
||||||
// @Cacheable
|
|
||||||
public UserToken getTokenByUserName(String userName)
|
public UserToken getTokenByUserName(String userName)
|
||||||
{
|
{
|
||||||
return tokenRepository.findByUserName(userName);
|
return tokenRepository.findByUserName(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Cacheable(value = "userTokenCache", key = "#tokenString", unless="#result == null")
|
||||||
public UserToken getTokenByTokenString(String tokenString)
|
public UserToken getTokenByTokenString(String tokenString)
|
||||||
{
|
{
|
||||||
return tokenRepository.findByToken(tokenString);
|
return tokenRepository.findByToken(tokenString);
|
||||||
@ -35,4 +36,10 @@ public class UserTokenService {
|
|||||||
}
|
}
|
||||||
tokenRepository.save(userToken);
|
tokenRepository.save(userToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteToken(String userName)
|
||||||
|
{
|
||||||
|
tokenRepository.deleteByUserName(userName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user