I am following a tutorial to implement JWT authentication with spring boot and java. But in my case I want to do it with Kotlin.
I am able to generate a jwt token but having issues when extracting claims from a jwt token.
In this tutorial there is a generic function to extract claims from a token.
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver {
final Claims claims = extractAllClaims(token);
return claimResolver.apply(claims);
}
private Claims extractAllClaims(String token) {
return Jwts.parser().setSigningKey(KEY).parseClaimsJws(token).getBody();
}
public String extractUsername(String token) {
return extractClaim(token, Claims::getSubject);
}
I have written this in kotlin as follows.
fun <T> extractClaim(token: String, claimResolver: Function<Claims, T>): T {
val claims = extractAllClaims(token)
return claimResolver.apply(claims)
}
private fun extractAllClaims(token: String): Claims {
return Jwts.parser().setSigningKey(key).parseClaimsJws(token).body
}
fun extractUsername(token: String): String {
return extractClaim(token, Claims::getSubject)
}
I'm getting an error from Function<Claims, T> saying
One type argument expected for interface Function
Also I can see there are other options such as KFunction and KFunction1.
I am not much experienced in Kotlin and can someone help me to figure out the issue here or suggest a better way of doing this.