Merge pull request #2508 from guusdk/KEYCLOAK-2731
KEYCLOAK-2731: Improve thread safety of TokenManager
This commit is contained in:
commit
6dc1194247
1 changed files with 11 additions and 8 deletions
|
@ -53,7 +53,7 @@ public class TokenManager {
|
||||||
return getAccessToken().getToken();
|
return getAccessToken().getToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
public AccessTokenResponse getAccessToken(){
|
public synchronized AccessTokenResponse getAccessToken(){
|
||||||
if(currentToken == null){
|
if(currentToken == null){
|
||||||
grantToken();
|
grantToken();
|
||||||
}else if(tokenExpired()){
|
}else if(tokenExpired()){
|
||||||
|
@ -73,9 +73,10 @@ public class TokenManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
int requestTime = Time.currentTime();
|
int requestTime = Time.currentTime();
|
||||||
currentToken = tokenService.grantToken(config.getRealm(), form.asMap());
|
synchronized (this) {
|
||||||
expirationTime = requestTime + currentToken.getExpiresIn();
|
currentToken = tokenService.grantToken( config.getRealm(), form.asMap() );
|
||||||
|
expirationTime = requestTime + currentToken.getExpiresIn();
|
||||||
|
}
|
||||||
return currentToken;
|
return currentToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,20 +91,22 @@ public class TokenManager {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int requestTime = Time.currentTime();
|
int requestTime = Time.currentTime();
|
||||||
currentToken = tokenService.refreshToken(config.getRealm(), form.asMap());
|
|
||||||
expirationTime = requestTime + currentToken.getExpiresIn();
|
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
currentToken = tokenService.refreshToken( config.getRealm(), form.asMap() );
|
||||||
|
expirationTime = requestTime + currentToken.getExpiresIn();
|
||||||
|
}
|
||||||
return currentToken;
|
return currentToken;
|
||||||
} catch (BadRequestException e) {
|
} catch (BadRequestException e) {
|
||||||
return grantToken();
|
return grantToken();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMinTokenValidity(long minTokenValidity) {
|
public synchronized void setMinTokenValidity(long minTokenValidity) {
|
||||||
this.minTokenValidity = minTokenValidity;
|
this.minTokenValidity = minTokenValidity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean tokenExpired() {
|
private synchronized boolean tokenExpired() {
|
||||||
return (Time.currentTime() + minTokenValidity) >= expirationTime;
|
return (Time.currentTime() + minTokenValidity) >= expirationTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue