From c69efa101f18699048a1693c89a485cfe0915030 Mon Sep 17 00:00:00 2001 From: wangliwen Date: Tue, 5 Jul 2022 09:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=B8=E7=94=A8=E6=A0=A1=E9=AA=8Ctoken?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E4=BA=8C=E7=BA=A7=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- renren-admin/src/main/resources/ehcache.xml | 9 ++ .../renren/service/impl/TokenServiceImpl.java | 117 +++++++++--------- 2 files changed, 70 insertions(+), 56 deletions(-) diff --git a/renren-admin/src/main/resources/ehcache.xml b/renren-admin/src/main/resources/ehcache.xml index 1bb993cb..746310c3 100644 --- a/renren-admin/src/main/resources/ehcache.xml +++ b/renren-admin/src/main/resources/ehcache.xml @@ -57,4 +57,13 @@ overflowToDisk="false" memoryStoreEvictionPolicy="LRU"/> + + + \ No newline at end of file diff --git a/renren-api/src/main/java/io/renren/service/impl/TokenServiceImpl.java b/renren-api/src/main/java/io/renren/service/impl/TokenServiceImpl.java index f5c87ed5..77c930aa 100644 --- a/renren-api/src/main/java/io/renren/service/impl/TokenServiceImpl.java +++ b/renren-api/src/main/java/io/renren/service/impl/TokenServiceImpl.java @@ -4,6 +4,8 @@ import io.renren.common.service.impl.BaseServiceImpl; import io.renren.dao.TokenDao; import io.renren.entity.TokenEntity; import io.renren.service.TokenService; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.Date; @@ -12,73 +14,76 @@ import java.util.UUID; @Service public class TokenServiceImpl extends BaseServiceImpl implements TokenService { - /** - * 12小时后过期 - */ - private final static int EXPIRE = 3600 * 12; + private static final String getByTokenKey = "getByToken"; + /** + * 12小时后过期 + */ + private final static int EXPIRE = 3600 * 12; - @Override - public TokenEntity getByToken(String token) { - return baseDao.getByToken(token); - } + @Override + @Cacheable(value = getByTokenKey, key = "#p1") + public TokenEntity getByToken(String token) { + return baseDao.getByToken(token); + } - @Override - public TokenEntity createToken(Long userId) { - //当前时间 - Date now = new Date(); - //过期时间 - Date expireTime = new Date(now.getTime() + EXPIRE * 1000); + @Override + @CacheEvict(key = getByTokenKey, allEntries = true) + public TokenEntity createToken(Long userId) { + //当前时间 + Date now = new Date(); + //过期时间 + Date expireTime = new Date(now.getTime() + EXPIRE * 1000); - //用户token - String token; + //用户token + String token; - //判断是否生成过token - TokenEntity tokenEntity = baseDao.getByUserId(userId); - if(tokenEntity == null){ - //生成一个token - token = generateToken(); + //判断是否生成过token + TokenEntity tokenEntity = baseDao.getByUserId(userId); + if (tokenEntity == null) { + //生成一个token + token = generateToken(); - tokenEntity = new TokenEntity(); - tokenEntity.setUserId(userId); - tokenEntity.setToken(token); - tokenEntity.setUpdateDate(now); - tokenEntity.setExpireDate(expireTime); + tokenEntity = new TokenEntity(); + tokenEntity.setUserId(userId); + tokenEntity.setToken(token); + tokenEntity.setUpdateDate(now); + tokenEntity.setExpireDate(expireTime); - //保存token - this.insert(tokenEntity); - }else{ - //判断token是否过期 - if(tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()){ - //token过期,重新生成token - token = generateToken(); - }else { - token = tokenEntity.getToken(); - } + //保存token + this.insert(tokenEntity); + } else { + //判断token是否过期 + if (tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()) { + //token过期,重新生成token + token = generateToken(); + } else { + token = tokenEntity.getToken(); + } - tokenEntity.setToken(token); - tokenEntity.setUpdateDate(now); - tokenEntity.setExpireDate(expireTime); + tokenEntity.setToken(token); + tokenEntity.setUpdateDate(now); + tokenEntity.setExpireDate(expireTime); - //更新token - this.updateById(tokenEntity); - } + //更新token + this.updateById(tokenEntity); + } - return tokenEntity; - } + return tokenEntity; + } - @Override - public void expireToken(Long userId){ - Date now = new Date(); + @Override + public void expireToken(Long userId) { + Date now = new Date(); - TokenEntity tokenEntity = new TokenEntity(); - tokenEntity.setUserId(userId); - tokenEntity.setUpdateDate(now); - tokenEntity.setExpireDate(now); + TokenEntity tokenEntity = new TokenEntity(); + tokenEntity.setUserId(userId); + tokenEntity.setUpdateDate(now); + tokenEntity.setExpireDate(now); - this.updateById(tokenEntity); - } + this.updateById(tokenEntity); + } - private String generateToken(){ - return UUID.randomUUID().toString().replace("-", ""); - } + private String generateToken() { + return UUID.randomUUID().toString().replace("-", ""); + } }