Spring security failing to convert String to RSA keys
getting the following error: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'privateKey': Failed to convert value of type 'java.lang.String' to required type 'java.security.interfaces.RSAPrivateKey'; Failed to convert from type [java.lang.String] to type [@org.springframework.beans.factory.annotation.Value java.security.interfaces.RSAPrivateKey] for value [${jwt.private.key]
not sure what to do and havent been able to find much on this, anyone have any idea? thanks
33 Replies
⌛
This post has been reserved for your question.
Hey @Victor! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
i have generated both private and public keys and have them in these files under resources
Can you show your
SecurityConfig
class?
or the class of the securityConfig
beanyea
Instead of injecting an
RSAPrivateKey
object, you can inject a resource
and then load ityeah so
Value("${jwt.public.key}")
doesn't work for RSAPublicKey
objectshow come? im pretty sure i have done exactly the same in a previous project
you can probably inject it using a constructor like that:
oh?
i have seen it in spring security tutorials too
same Spring version?
hmm maybe not but it wasnt too long ago
i'll try this
that's the manual approach
you can also try this: https://stackoverflow.com/a/65745676/10871900
Stack Overflow
Spring unable to convert from String to RSAPublicKey/RSAPrivateKey ...
I have a microservice with Spring Security and I have the contents of the private and public keys in application.yml (please don't judge me). I also have a @ConfigurationProperties file with this
Maybe doing it automatically without any configuration requires some dependency
You are using Spring Boot, right?
yea
you might need spring-security-rsa or similar, idk
am i missing something?
maybe it only happens automatically if spring-boot-starter-security or similar is in the dependencies
Did you also use Spring Resource Server in the project where it works?
But I would try that
But why can't you use the RSA support of Spring resource server directly?
actually I found it
https://docs.spring.io/spring-security/reference/reactive/oauth2/resource-server/jwt.html#webflux-oauth2resourceserver-jwt-decoder-public-key-boot
that's the documentation of the thing you are using
or if you are using the imperitive thing and not reactive, it's https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html#oauth2resourceserver-jwt-decoder-public-key-builder
@Victor
or you might also follow that
I think that's actually the thing you need
(it's a bit above in the docs)
i'll give it a read
thanks
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
that should figure out the part about converting the keys - it just doesn't happen automatically without you telling it to
If this doesn't work like you want to, please check whether you have a
RsaKeyConversionServicePostProcessor
bean registered in your application
Are you using Eclipse with Spring Tools 4, VSC with Spring Tools 4, any other Spring Tools 4 IDE or IntelliJ Ultimate?how do i check that?
im using intellij community
ok that doesn't have direct Spring support and doesn't tell you about the registered beans
so you'd need to check in the application
Do you have Spring actuator enabled for testing?
In any way, you can just try autowiring an
RsaKeyConversionServicePostProcessor
instance and you'll notice it if it doesn't work (unless the application crashes before that, then you wouldn't notice)
btw you might also need the spring-security-oauth2-jose
for some JWT stuff (if you want to let Spring do the JWT verification etc): https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html#oauth2resourceserver-jwt-minimaldependencies
essentially the docs should be useful@dan1st i think the issue was that i was missing a "}" at the end of the private key here lol
ffs
thanks a lot for the help anyway
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
ohhhh
wow
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.