-
Notifications
You must be signed in to change notification settings - Fork 41.1k
Allow Spring Security's RSA key converters to be used when binding configuration properties #24891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
I'm struggling to reproduce this in a test. With a I think we need to look again at the sample that accompanied the security issue to figure out exactly where the Security-registered converters will and will not be used. |
I've figured out the difference in behaviour. It's to do with the way in which Spring Security registers the converters and how the binder copies things over.
Lines 187 to 192 in 713c0fc
Spring Security registers the converters differently depending on whether or not the bean factory has a conversion service: ConversionService service = beanFactory.getConversionService();
if (service instanceof ConverterRegistry) {
ConverterRegistry registry = (ConverterRegistry) service;
registry.addConverter(String.class, RSAPrivateKey.class, this.pkcs8);
registry.addConverter(String.class, RSAPublicKey.class, this.x509);
}
else {
beanFactory.addPropertyEditorRegistrar((registry) -> {
registry.registerCustomEditor(RSAPublicKey.class, new ConverterPropertyEditorAdapter<>(this.x509));
registry.registerCustomEditor(RSAPrivateKey.class, new ConverterPropertyEditorAdapter<>(this.pkcs8));
});
} The bean factory in a typical Spring Boot application will have a conversion service configured. This causes the converters to be registered directly with the conversion service rather than via a property editor registrar. As a result, there's no editor to be copied over and the converters are lost to the binder. When I tried to recreate the problem in a test, I used Here is a fix for the problem. The code itself isn't too bad, but I'm not entirely happy with it for a couple of reasons:
I've opened spring-projects/spring-security#9626 for 2. I'm not sure what, if anything, we can do about 1. |
2 is a problem I've faced before. It's really a weakness with our registration logic. I've opened #26034 to see if we can improve that. 1 is a bit more tricky. I feel like a typical Boot application wouldn't want the |
One other thing that's a bit odd, there's no way to set |
Spring Security ships with converters for reading RSA public and private key files. These are applied to the application context's
ConversionService
through aBeanFactoryPostProcessor
.This allows an application to do things like:
to retrieve keys from configuration.
This doesn't work, though, if a Spring Boot application includes auto-configuration that includes a
@ConfigurationPropertiesBinding
for another set of properties. It appears this may change the loading order such that Spring Security'sRsaKeyConversionServicePostProcessor
doesn't get applied to Boot's conversion service.I believe the correct enhancement is for Spring Boot to add
@ConfigurationPropertiesBinding
@Bean
s to Security's auto configuration like so:The text was updated successfully, but these errors were encountered: