I'm working on a small Spring Boot application in Kotlin and now I want to secure it using JJWT. Roughly speaking, I am translating this tutorial to my use case: https://jakublesko.com/spring-security-with-jwt/
In the project I have this AuthenticationFilter:
class JwtAuthenticationFilter(val authManager: AuthenticationManager) : UsernamePasswordAuthenticationFilter() {
// other stuff omitted for brevity
override fun successfulAuthentication(request: HttpServletRequest?, response: HttpServletResponse?, chain: FilterChain?, authResult: Authentication?) {
val user = authResult?.principal as User
val roles = user.authorities
.stream()
.map(GrantedAuthority::getAuthority)
val signingKey = JWT_SECRET.toByteArray()
val token = Jwts.builder()
.signWith(SignatureAlgorithm.HS512, Keys.hmacShaKeyFor(signingKey))
.setHeaderParam("typ", TOKEN_TYPE) // validate that "typ" does not actually mean "type"
.setIssuer(TOKEN_ISSUER)
.setAudience(TOKEN_AUDIENCE)
.setSubject(user.username)
.setExpiration(Date(System.currentTimeMillis() + 864000000))
.claim("rol", roles)
.compact()
response?.addHeader(TOKEN_HEADER, TOKEN_PREFIX + token)
}
}
When I post to the authentication URL that is supposed to issue a token though, I receive:
java.lang.NoSuchMethodError: io.jsonwebtoken.SignatureAlgorithm.getMinKeyLength()I
at io.jsonwebtoken.security.Keys.hmacShaKeyFor(Keys.java:69) ~[jjwt-api-0.10.7.jar:na]
I can debug into the successfulAuthentication
method and see that it is called with reasonable parameters. What catches my eye is the "I" after the parenthesis at the end of the getMinKeyLength()I
. My googling skills apparently do not suffice to find a reason why it is there, but I strongly suspect it is related to reflection & calling Java libs from Kotlin code.
Is anyone around who can tell me how to fix this? I have run out of guesses.