This commit is contained in:
Eduard Tihomirov 2024-11-19 15:29:39 +03:00
parent ac901bf843
commit 1e2985b156
2 changed files with 18 additions and 18 deletions

View file

@ -7,55 +7,55 @@ import java.util.concurrent.ConcurrentHashMap;
* @author Eduard Tihomirov
*/
public class TokensStore {
private static final Map<String, ExpiringToken> accessTokensMap = new ConcurrentHashMap<>();
private static final Map<String, ExpiringToken> refreshTokensMap = new ConcurrentHashMap<>();
private static final Map<String, ExpiringToken> ACCESS_TOKENS_MAP = new ConcurrentHashMap<>();
private static final Map<String, ExpiringToken> REFRESH_TOKENS_MAP = new ConcurrentHashMap<>();
public static void addAccessToken(String prnOid, String token, long expiresIn) {
if (token != null) {
long expiryTime = System.currentTimeMillis() + 1000L * expiresIn;
accessTokensMap.put(prnOid, new ExpiringToken(token, expiryTime));
ACCESS_TOKENS_MAP.put(prnOid, new ExpiringToken(token, expiryTime));
}
}
public static String getAccessToken(String prnOid) {
return accessTokensMap.get(prnOid).getAccessToken();
return ACCESS_TOKENS_MAP.get(prnOid).getAccessToken();
}
public static void removeExpiredAccessToken() {
for (String key : accessTokensMap.keySet()) {
ExpiringToken token = accessTokensMap.get(key);
for (String key : ACCESS_TOKENS_MAP.keySet()) {
ExpiringToken token = ACCESS_TOKENS_MAP.get(key);
if (token != null && token.isExpired()) {
accessTokensMap.remove(key);
ACCESS_TOKENS_MAP.remove(key);
}
}
}
public static void removeExpiredRefreshToken() {
for (String key : refreshTokensMap.keySet()) {
ExpiringToken token = refreshTokensMap.get(key);
for (String key : REFRESH_TOKENS_MAP.keySet()) {
ExpiringToken token = REFRESH_TOKENS_MAP.get(key);
if (token != null && token.isExpired()) {
refreshTokensMap.remove(key);
REFRESH_TOKENS_MAP.remove(key);
}
}
}
public static void removeAccessToken(String prnOid) {
accessTokensMap.remove(prnOid);
ACCESS_TOKENS_MAP.remove(prnOid);
}
public static void addRefreshToken(String prnOid, String token, long expiresIn) {
if (token != null) {
long expiryTime = System.currentTimeMillis() + 1000L * expiresIn;
refreshTokensMap.put(prnOid, new ExpiringToken(token, expiryTime));
REFRESH_TOKENS_MAP.put(prnOid, new ExpiringToken(token, expiryTime));
}
}
public static String getRefreshToken(String prnOid) {
return refreshTokensMap.get(prnOid).getAccessToken();
return REFRESH_TOKENS_MAP.get(prnOid).getAccessToken();
}
public static void removeRefreshToken(String prnOid) {
refreshTokensMap.remove(prnOid);
REFRESH_TOKENS_MAP.remove(prnOid);
}
}

View file

@ -32,14 +32,14 @@ public class JwtTokenService {
@Value("${webbpm.security.token.issuer:#{null}}")
private final String tokenIssuerName =
ResourceMetadataUtils.PROJECT_GROUP_ID + "." + ResourceMetadataUtils.PROJECT_ARTIFACT_ID;
private final SecretKey SIGNING_KEY;
private final SecretKey signingKey;
@Autowired
public JwtTokenService(@Value("${webbpm.security.token.secret.key:ZjE5ZjMxNmYtODViZC00ZTQ5LWIxZmYtOGEzYzE3Yjc1MDVk}")
String secretKey) {
byte[] encodedKey = Base64.getDecoder().decode(secretKey);
this.SIGNING_KEY = Keys.hmacShaKeyFor(encodedKey);
this.signingKey = Keys.hmacShaKeyFor(encodedKey);
}
public Token createAccessToken(String userAccountId, Long expiresIn, String ervuId, Boolean hasRole) {
@ -51,7 +51,7 @@ public class JwtTokenService {
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(expirationDate)
.claim("hasRole", hasRole)
.signWith(SIGNING_KEY)
.signWith(signingKey)
.compact();
return new Token(userAccountId + ":" + ervuId, tokenIssuerName, expirationDate, value, hasRole);
}
@ -71,7 +71,7 @@ public class JwtTokenService {
public Token getToken(String token) {
Claims claims = Jwts.parser()
.setSigningKey(SIGNING_KEY)
.setSigningKey(signingKey)
.parseClaimsJws(token)
.getBody();