我有多个线程并行执行API调用。每当jwt令牌过期时,我希望所有线程都等待,直到调用刷新令牌API并返回有效的更新的jwt令牌。我有一个单例类,该类具有刷新令牌方法,该方法将进行调用并更新令牌。如何确保我的所有其他线程将等待令牌获取完成?
public class JWTTokenManager
{
private static JWTTokenManager _tokenManager;
private string _token;
private bool _refreshingToken;
public static JWTTokenManager GetManager()
{
if (_tokenManager == null)
_tokenManager = new JWTTokenManager();
return _tokenManager;
}
public void UpdateToken(string token)
{
_token = token;
}
public string GetToken()
{
return _token;
}
public async Task<bool> ValidateRefreshTocken()
{
UserInfo userdata = JsonConvert.DeserializeObject<UserInfo>(GetUserInfo(_token), new Helper.DefaultJsonSetting());
if (!string.IsNullOrWhiteSpace(userdata.Exp) && TokenExpired(long.Parse(userdata.Exp)))
{
_refreshingToken = true;
JWTToken jwtToken = Database.DBService.GetDB().FetchJWTToken();
RefreshToken requestRefresh = new RefreshToken
{
ExpiredTocken = jwtToken.Token,
RefreshTocken = jwtToken.RefreshToken
};
HttpClient httpClient = CloudService.GetCloudService().GetHttpClient();
HttpResponseMessage response = await httpClient.PostAsync($"account/v1/tokenRefresh", new StringContent(JsonConvert.SerializeObject(requestRefresh), Encoding.UTF8, "application/json"));
bool responseStatus = await ParseTokenResponseAsync(response);
_refreshingToken = false;
return responseStatus;
}
else
{
return true;
}
}
private string GetUserInfo(string key)
{
string[] base64Url = key.Split('.');
if (base64Url.Length > 1)
{
string userinfo = base64Url[1];
userinfo = userinfo.Replace(" ", "+");
int mod4 = userinfo.Length % 4;
if (mod4 > 0)
{
userinfo += new string('=', 4 - mod4);
}
var base64EncodedBytes = System.Convert.FromBase64String(userinfo);
return Encoding.UTF8.GetString(base64EncodedBytes);
}
else
{
return "";
}
}
public bool TokenExpired(long unixTimeStamp)
{
DateTime tokenExpiryDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
tokenExpiryDateTime = tokenExpiryDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
DateTime currentDateTime = DateTime.Now;
return DateTime.Compare(tokenExpiryDateTime, currentDateTime) <= 0;
}
public async Task<bool> ParseTokenResponseAsync(HttpResponseMessage httpResponse)
{
if (httpResponse.IsSuccessStatusCode == true)
{
string responseString = await httpResponse.Content.ReadAsStringAsync();
Newtonsoft.Json.Linq.JObject responsedataObject = Newtonsoft.Json.Linq.JObject.Parse(responseString);
string token = responsedataObject["data"]["token"].ToString();
string refreshToken = responsedataObject["data"]["refreshToken"].ToString();
_token = token;
JWTToken updatedToken = new JWTToken()
{
Token = _token,
RefreshToken = refreshToken
};
Database.DBService.GetDB().InsertOrUpdateJWTToken(updatedToken);
return true;
}
else
{
return false;
}
}
}
public class CloudService
{
private const int TIME_OUT = 50;
private const int HTTP_GET = 0;
private const int HTTP_PUT = 1;
private const int HTTP_POST = 2;
private const int HTTP_DELETE = 3;
private static CloudService _serviceInstance;
public static CloudService GetCloudService()
{
if (_serviceInstance == null)
_serviceInstance = new CloudService();
return _serviceInstance;
}
private async Task<HttpResponseMessage> ExecuteHttpTask(int taskType, string url, StringContent content = null)
{
HttpClient httpClient = GetHttpClient();
switch (taskType)
{
case HTTP_GET:
return await httpClient.GetAsync(url);
case HTTP_PUT:
return await httpClient.PutAsync(url, content);
case HTTP_POST:
return await httpClient.PostAsync(url, content);
case HTTP_DELETE:
return await httpClient.DeleteAsync(url);
default:
return null;
}
}
public async Task<Response> HTTPTask(string url, int taskType, StringContent content = null, bool login = false)
{
bool refreshTocken = await JWTTokenManager.GetManager().ValidateRefreshTocken();
Response httpResponse = new Response();
try
{
HttpResponseMessage response = await ExecuteHttpTask(taskType, url, content);
string responseString = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
httpResponse.status = "error";
else
httpResponse.status = "data";
httpResponse.data = ParseResponseData(httpResponse.status, responseString);
}
catch (Exception e)
{
httpResponse = GenericErrorResponse(e.Message);
}
return httpResponse;
}
public async Task<Response> GetSectionAsync(string id)
{
string url = $"catalog/v2/homepageSections/{id}?order-by=name,asc";
return await HTTPTask(url, Constants.HTTP_GET);
}
public async Task<Response> GetProductAsync(string id)
{
string url = $"catalog/v2/products/{id}";
return await HTTPTask(url, Constants.HTTP_GET);
}
public async Task<Response> GetCourseDetailsAsync(string id)
{
string url = $"catalog/v2/products/{id}/courseDetails";
return await HTTPTask(url, Constants.HTTP_GET);
}
}
不同的线程将调用ClouService中的方法,而ClouService中的方法又并行调用不同的API,并且所有这些都将通过HTTPTask方法进行,该方法中的令牌经过验证,如果无效,则调用API以获取更新的令牌。如何使所有API(线程)从令牌无效的那一刻起一直等待,直到API返回有效令牌为止?
根据评论,我更新了两个类。请看看。
public sealed class JWTTokenManager
{
private static readonly JWTTokenManager _tokenManager = new JWTTokenManager();
private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private string _token;
// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
static JWTTokenManager()
{
}
private JWTTokenManager()
{
}
public static JWTTokenManager GetManager()
{
return _tokenManager;
}
public void UpdateToken(string token)
{
_token = token;
}
public string GetToken()
{
return _token;
}
public async Task<bool> ValidateRefreshTocken(bool forceRefresh = false)
{
bool validToken;
await _semaphore.WaitAsync();
try
{
UserInfo userdata = JsonConvert.DeserializeObject<UserInfo>(GetUserInfo(_token), new DefaultJsonSetting());
if (forceRefresh || !string.IsNullOrWhiteSpace(userdata.Exp) && TokenExpired(long.Parse(userdata.Exp)))
{
validToken = await RefreshToken();
}
else
{
validToken = true;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
validToken = false;
}
finally
{
_semaphore.Release();
}
return validToken;
}
private async Task<bool> RefreshToken()
{
JWTToken jwtToken = Database.DBService.GetDB().FetchJWTToken();
RefreshToken requestRefresh = new RefreshToken
{
ExpiredTocken = jwtToken.Token,
RefreshTocken = jwtToken.RefreshToken
};
HttpClient httpClient = CloudService.GetCloudService().GetHttpClient();
HttpResponseMessage response = await httpClient.PostAsync($"account/v1/tokenRefresh", new StringContent(JsonConvert.SerializeObject(requestRefresh), Encoding.UTF8, "application/json"));
bool status = await ParseTokenResponseAsync(response);
return status;
}
private string GetUserInfo(string key)
{
string[] base64Url = key.Split('.');
if (base64Url.Length > 1)
{
string userinfo = base64Url[1];
userinfo = userinfo.Replace(" ", "+");
int mod4 = userinfo.Length % 4;
if (mod4 > 0)
{
userinfo += new string('=', 4 - mod4);
}
var base64EncodedBytes = System.Convert.FromBase64String(userinfo);
return Encoding.UTF8.GetString(base64EncodedBytes);
}
else
{
return "";
}
}
public bool TokenExpired(long unixTimeStamp)
{
DateTime tokenExpiryDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
tokenExpiryDateTime = tokenExpiryDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
DateTime currentDateTime = DateTime.Now;
return DateTime.Compare(tokenExpiryDateTime, currentDateTime) <= 0;
}
public async Task<bool> ParseTokenResponseAsync(HttpResponseMessage httpResponse)
{
if (httpResponse.IsSuccessStatusCode == true)
{
string responseString = await httpResponse.Content.ReadAsStringAsync();
Newtonsoft.Json.Linq.JObject responsedataObject = Newtonsoft.Json.Linq.JObject.Parse(responseString);
string token = responsedataObject["data"]["token"].ToString();
string refreshToken = responsedataObject["data"]["refreshToken"].ToString();
_token = token;
JWTToken updatedToken = new JWTToken()
{
Token = _token,
RefreshToken = refreshToken
};
Database.DBService.GetDB().InsertOrUpdateJWTToken(updatedToken);
return true;
}
else
{
return false;
}
}
}
public sealed class CloudService
{
private const int TIME_OUT = 50;
private const int HTTP_GET = 0;
private const int HTTP_PUT = 1;
private const int HTTP_POST = 2;
private const int HTTP_DELETE = 3;
private static readonly CloudService _serviceInstance = new CloudService();
static CloudService()
{
}
private CloudService()
{
}
public static CloudService GetCloudService()
{
return _serviceInstance;
}
public HttpClient GetHttpClient()
{
HttpClient httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(TIME_OUT),
BaseAddress = new Uri($"{AppConst.ServerBaseURL}/"),
};
httpClient.DefaultRequestHeaders.Add("X-Jwt-Token", JWTTokenManager.GetManager().GetToken());
httpClient.DefaultRequestHeaders.Add("tenantId", AppConst.TenanatID);
return httpClient;
}
private async Task<HttpResponseMessage> ExecuteHttpTask(int taskType, string url, StringContent content = null)
{
HttpClient httpClient = GetHttpClient();
switch (taskType)
{
case HTTP_GET:
return await httpClient.GetAsync(url);
case HTTP_PUT:
return await httpClient.PutAsync(url, content);
case HTTP_POST:
return await httpClient.PostAsync(url, content);
case HTTP_DELETE:
return await httpClient.DeleteAsync(url);
default:
return null;
}
}
public async Task<Response> HTTPTask(string url, int taskType, StringContent content = null, bool login = false)
{
Response httpResponse = new Response();
try
{
HttpResponseMessage response = await ExecuteHttpTask(taskType, url, content);
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized && !login)
{
bool refreshTocken = await JWTTokenManager.GetManager().ValidateRefreshTocken(true);
if (refreshTocken == true)
{
response = await ExecuteHttpTask(taskType, url, content);
}
else
{
httpResponse = GenericErrorResponse();
return httpResponse;
}
}
string responseString = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
httpResponse.status = "error";
else
httpResponse.status = "data";
httpResponse.data = ParseResponseData(httpResponse.status, responseString);
}
catch (Exception e)
{
httpResponse = GenericErrorResponse(e.Message);
}
return httpResponse;
}
}
答案 0 :(得分:0)
您可以使用ManualResetEvent。假设您有三个线程A,B(并行调用api调用)和C(执行令牌刷新)。
private static ManualResetEvent mre = new ManualResetEvent(true); // event is always set except when refreshing the token
Code on thread A,B
mre.WaitOne() // it blocks whenever the event is reset
...
Code on thread C
mre.Reset() // this blocks all the waiting threads
perform the token refresh
mre.Set() // this frees all the waiting threads
答案 1 :(得分:0)
您应该使style="display: none;"
返回令牌,而不是需要令牌。
如果令牌是有效的,它将返回带有有效令牌的已完成任务,如果不是,则将返回在检索令牌时将完成的任务。多个并发线程可以等待同一任务。
JWTTokenManager
无锁版本:
public class JWTTokenManager
{
private Task<string> tokenTask;
private readonly object sync = new object();
public Task<string> GetTokenAsync()
{
lock (sync)
{
if (tokenTask.IsCompleted && !IsTokenValid(tokenTask.Result))
{
tokenTask = GetNewTokenAsync();
}
return tokenTask;
}
}
}