🎨 优化记录最后在线时间代码
This commit is contained in:
parent
fa235a84e6
commit
963cf1d139
@ -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,15 +22,18 @@ 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);
|
||||||
@ -56,6 +60,8 @@ public class RedisTokenManager implements TokenManager {
|
|||||||
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user