|
@@ -18,11 +18,6 @@ package org.springframework.security.config.crypto;
|
|
|
|
|
|
import java.beans.PropertyEditor;
|
|
import java.beans.PropertyEditor;
|
|
import java.beans.PropertyEditorSupport;
|
|
import java.beans.PropertyEditorSupport;
|
|
-import java.io.ByteArrayInputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
-import java.io.UncheckedIOException;
|
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
|
import java.security.interfaces.RSAPrivateKey;
|
|
import java.security.interfaces.RSAPrivateKey;
|
|
import java.security.interfaces.RSAPublicKey;
|
|
import java.security.interfaces.RSAPublicKey;
|
|
|
|
|
|
@@ -32,9 +27,8 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
import org.springframework.core.convert.ConversionService;
|
|
import org.springframework.core.convert.ConversionService;
|
|
import org.springframework.core.convert.converter.Converter;
|
|
import org.springframework.core.convert.converter.Converter;
|
|
import org.springframework.core.convert.converter.ConverterRegistry;
|
|
import org.springframework.core.convert.converter.ConverterRegistry;
|
|
-import org.springframework.core.io.DefaultResourceLoader;
|
|
|
|
-import org.springframework.core.io.Resource;
|
|
|
|
import org.springframework.core.io.ResourceLoader;
|
|
import org.springframework.core.io.ResourceLoader;
|
|
|
|
+import org.springframework.security.converter.ResourceKeyConverterAdapter;
|
|
import org.springframework.security.converter.RsaKeyConverters;
|
|
import org.springframework.security.converter.RsaKeyConverters;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
@@ -50,11 +44,15 @@ public class RsaKeyConversionServicePostProcessor implements BeanFactoryPostProc
|
|
|
|
|
|
private static final String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
|
|
private static final String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
|
|
|
|
|
|
- private ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
|
|
|
|
+ private ResourceKeyConverterAdapter<RSAPublicKey> x509 = new ResourceKeyConverterAdapter<>(RsaKeyConverters.x509());
|
|
|
|
+
|
|
|
|
+ private ResourceKeyConverterAdapter<RSAPrivateKey> pkcs8 = new ResourceKeyConverterAdapter<>(
|
|
|
|
+ RsaKeyConverters.pkcs8());
|
|
|
|
|
|
public void setResourceLoader(ResourceLoader resourceLoader) {
|
|
public void setResourceLoader(ResourceLoader resourceLoader) {
|
|
Assert.notNull(resourceLoader, "resourceLoader cannot be null");
|
|
Assert.notNull(resourceLoader, "resourceLoader cannot be null");
|
|
- this.resourceLoader = resourceLoader;
|
|
|
|
|
|
+ this.x509.setResourceLoader(resourceLoader);
|
|
|
|
+ this.pkcs8.setResourceLoader(resourceLoader);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -62,18 +60,16 @@ public class RsaKeyConversionServicePostProcessor implements BeanFactoryPostProc
|
|
if (hasUserDefinedConversionService(beanFactory)) {
|
|
if (hasUserDefinedConversionService(beanFactory)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- Converter<String, RSAPrivateKey> pkcs8 = pkcs8();
|
|
|
|
- Converter<String, RSAPublicKey> x509 = x509();
|
|
|
|
ConversionService service = beanFactory.getConversionService();
|
|
ConversionService service = beanFactory.getConversionService();
|
|
if (service instanceof ConverterRegistry) {
|
|
if (service instanceof ConverterRegistry) {
|
|
ConverterRegistry registry = (ConverterRegistry) service;
|
|
ConverterRegistry registry = (ConverterRegistry) service;
|
|
- registry.addConverter(String.class, RSAPrivateKey.class, pkcs8);
|
|
|
|
- registry.addConverter(String.class, RSAPublicKey.class, x509);
|
|
|
|
|
|
+ registry.addConverter(String.class, RSAPrivateKey.class, this.pkcs8);
|
|
|
|
+ registry.addConverter(String.class, RSAPublicKey.class, this.x509);
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
beanFactory.addPropertyEditorRegistrar((registry) -> {
|
|
beanFactory.addPropertyEditorRegistrar((registry) -> {
|
|
- registry.registerCustomEditor(RSAPublicKey.class, new ConverterPropertyEditorAdapter<>(x509));
|
|
|
|
- registry.registerCustomEditor(RSAPrivateKey.class, new ConverterPropertyEditorAdapter<>(pkcs8));
|
|
|
|
|
|
+ registry.registerCustomEditor(RSAPublicKey.class, new ConverterPropertyEditorAdapter<>(this.x509));
|
|
|
|
+ registry.registerCustomEditor(RSAPrivateKey.class, new ConverterPropertyEditorAdapter<>(this.pkcs8));
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -83,54 +79,6 @@ public class RsaKeyConversionServicePostProcessor implements BeanFactoryPostProc
|
|
&& beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class);
|
|
&& beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class);
|
|
}
|
|
}
|
|
|
|
|
|
- private Converter<String, RSAPrivateKey> pkcs8() {
|
|
|
|
- Converter<String, InputStream> pemInputStreamConverter = pemInputStreamConverter();
|
|
|
|
- Converter<InputStream, RSAPrivateKey> pkcs8KeyConverter = autoclose(RsaKeyConverters.pkcs8());
|
|
|
|
- return pair(pemInputStreamConverter, pkcs8KeyConverter);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private Converter<String, RSAPublicKey> x509() {
|
|
|
|
- Converter<String, InputStream> pemInputStreamConverter = pemInputStreamConverter();
|
|
|
|
- Converter<InputStream, RSAPublicKey> x509KeyConverter = autoclose(RsaKeyConverters.x509());
|
|
|
|
- return pair(pemInputStreamConverter, x509KeyConverter);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private Converter<String, InputStream> pemInputStreamConverter() {
|
|
|
|
- return (source) -> source.startsWith("-----") ? toInputStream(source)
|
|
|
|
- : toInputStream(this.resourceLoader.getResource(source));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private InputStream toInputStream(String raw) {
|
|
|
|
- return new ByteArrayInputStream(raw.getBytes(StandardCharsets.UTF_8));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private InputStream toInputStream(Resource resource) {
|
|
|
|
- try {
|
|
|
|
- return resource.getInputStream();
|
|
|
|
- }
|
|
|
|
- catch (IOException ex) {
|
|
|
|
- throw new UncheckedIOException(ex);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private <T> Converter<InputStream, T> autoclose(Converter<InputStream, T> inputStreamKeyConverter) {
|
|
|
|
- return (inputStream) -> {
|
|
|
|
- try (InputStream is = inputStream) {
|
|
|
|
- return inputStreamKeyConverter.convert(is);
|
|
|
|
- }
|
|
|
|
- catch (IOException ex) {
|
|
|
|
- throw new UncheckedIOException(ex);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private <S, T, I> Converter<S, T> pair(Converter<S, I> one, Converter<I, T> two) {
|
|
|
|
- return (source) -> {
|
|
|
|
- I intermediary = one.convert(source);
|
|
|
|
- return two.convert(intermediary);
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private static class ConverterPropertyEditorAdapter<T> extends PropertyEditorSupport {
|
|
private static class ConverterPropertyEditorAdapter<T> extends PropertyEditorSupport {
|
|
|
|
|
|
private final Converter<String, T> converter;
|
|
private final Converter<String, T> converter;
|