I went through the documentation of jwt and implemented a login module which uses jwt to fulfill my authentication and authorisation. The problem is now my module is high load input module where concurrent users will access the module at a time, so I want to know whether jwt is thread safe. If it is not thread safe, what kind of technique can I use to make thread safe and NOT
coarse-grained synchronisation? So, both are important for me.
Here is my code snippet in java
private String generateJWTToken( User user, Long expiryTimeInMillis ) {
try {
Long currentTimeInMillis = currentContext;
Date currentDate = new Date( currentTimeInMillis );
Date expiryDate = new Date( currentTimeInMillis + expiryTimeInMillis );
if ( user.getUsername() == null || user.getUserRoles() == null || user.getUserRoles().isEmpty() || secretKey == null ) {
return null;
}
String roles = prepareRoleIds( user.getUserRoles() );
return Jwts.builder().setSubject( user.getUsername() ).setIssuedAt( currentDate ).claim( "Role", roles ).setExpiration( expiryDate ).signWith( secretKey ).compact();
}
catch ( Exception e ) {
return null;
}
}
I haven’t tried any method to overcome this issue, I want insight from some experienced people where good at technique like Lock striping and related ones. So , I am expecting simple technique to overcome my issue.