🎨 优化记录最后在线时间代码

This commit is contained in:
ronger 2021-12-15 09:03:09 +08:00
parent fa235a84e6
commit 963cf1d139

View File

@ -3,13 +3,14 @@ package com.rymcu.forest.jwt.service;
import com.rymcu.forest.jwt.def.JwtConstants; import com.rymcu.forest.jwt.def.JwtConstants;
import com.rymcu.forest.jwt.model.TokenModel; import com.rymcu.forest.jwt.model.TokenModel;
import com.rymcu.forest.service.UserService;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -21,17 +22,20 @@ import java.util.concurrent.TimeUnit;
*/ */
@Component @Component
public class RedisTokenManager implements TokenManager { public class RedisTokenManager implements TokenManager {
@Autowired
@Resource
private StringRedisTemplate redisTemplate; private StringRedisTemplate redisTemplate;
@Resource
private UserService userService;
/** /**
* 生成TOKEN * 生成TOKEN
*/ */
@Override @Override
public String createToken(String id) { public String createToken(String id) {
//使用uuid作为源token //使用 account 作为源 token
String token = Jwts.builder().setId(id).setSubject(id).setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS256, JwtConstants.JWT_SECRET).compact(); String token = Jwts.builder().setId(id).setSubject(id).setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS256, JwtConstants.JWT_SECRET).compact();
//存储到redis并设置过期时间 //存储到 redis 并设置过期时间
redisTemplate.boundValueOps(id).set(token, JwtConstants.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS); redisTemplate.boundValueOps(id).set(token, JwtConstants.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
return token; return token;
} }
@ -50,12 +54,14 @@ public class RedisTokenManager implements TokenManager {
if (token == null || !token.equals(model.getToken())) { if (token == null || !token.equals(model.getToken())) {
return false; return false;
} }
//如果验证成功说明此用户进行了一次有效操作延长token的过期时间 // 如果验证成功说明此用户进行了一次有效操作延长 token 的过期时间
redisTemplate.boundValueOps(model.getUsername()).expire(JwtConstants.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS); redisTemplate.boundValueOps(model.getUsername()).expire(JwtConstants.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
StringBuilder key = new StringBuilder(); StringBuilder key = new StringBuilder();
key.append(JwtConstants.LAST_ONLINE).append(model.getUsername()); key.append(JwtConstants.LAST_ONLINE).append(model.getUsername());
String result = redisTemplate.boundValueOps(key.toString()).get(); String result = redisTemplate.boundValueOps(key.toString()).get();
if (StringUtils.isBlank(result)) { if (StringUtils.isBlank(result)) {
// 更新最后在线时间
userService.updateLastOnlineTimeByEmail(model.getUsername());
redisTemplate.boundValueOps(key.toString()).set(LocalDateTime.now().toString(), JwtConstants.LAST_ONLINE_EXPIRES_MINUTE, TimeUnit.MINUTES); redisTemplate.boundValueOps(key.toString()).set(LocalDateTime.now().toString(), JwtConstants.LAST_ONLINE_EXPIRES_MINUTE, TimeUnit.MINUTES);
} }
return true; return true;