|
@@ -6,6 +6,7 @@ import com.github.benmanes.caffeine.cache.Cache;
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
import com.simuwang.base.config.DaqProperties;
|
|
import com.simuwang.base.config.DaqProperties;
|
|
import io.jsonwebtoken.Claims;
|
|
import io.jsonwebtoken.Claims;
|
|
|
|
+import io.jsonwebtoken.ExpiredJwtException;
|
|
import io.jsonwebtoken.Jwts;
|
|
import io.jsonwebtoken.Jwts;
|
|
import io.jsonwebtoken.security.Keys;
|
|
import io.jsonwebtoken.security.Keys;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
@@ -38,7 +39,7 @@ public class JwtContext {
|
|
if (MapUtil.isEmpty(tokenMap)) {
|
|
if (MapUtil.isEmpty(tokenMap)) {
|
|
tokenMap = MapUtil.newConcurrentHashMap(16);
|
|
tokenMap = MapUtil.newConcurrentHashMap(16);
|
|
}
|
|
}
|
|
- tokenMap.putIfAbsent(requestIP, token);
|
|
|
|
|
|
+ tokenMap.put(requestIP, token);
|
|
USER_TOKEN_CACHE.put(username, tokenMap);
|
|
USER_TOKEN_CACHE.put(username, tokenMap);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -75,6 +76,14 @@ public class JwtContext {
|
|
*/
|
|
*/
|
|
public synchronized String generateToken(String username, String requestIp) {
|
|
public synchronized String generateToken(String username, String requestIp) {
|
|
String token = this.getUserCache(username, requestIp);
|
|
String token = this.getUserCache(username, requestIp);
|
|
|
|
+ if (StrUtil.isNotBlank(token)) {
|
|
|
|
+ try {
|
|
|
|
+ // 如果token过期则重新生成
|
|
|
|
+ this.getClaimsByToken(token);
|
|
|
|
+ } catch (ExpiredJwtException e) {
|
|
|
|
+ token = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
if (StrUtil.isBlank(token)) {
|
|
if (StrUtil.isBlank(token)) {
|
|
SecretKey signingKey = Keys.hmacShaKeyFor(this.properties.getTokenSecret().getBytes(StandardCharsets.UTF_8));
|
|
SecretKey signingKey = Keys.hmacShaKeyFor(this.properties.getTokenSecret().getBytes(StandardCharsets.UTF_8));
|
|
//过期时间
|
|
//过期时间
|