Inline ResourceKeyConverterAdapter
Closes gh-9689 Closes gh-9626
This commit is contained in:
+70
-1
@@ -18,6 +18,12 @@ package org.springframework.security.config.crypto;
|
||||
|
||||
import java.beans.PropertyEditor;
|
||||
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.Key;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
|
||||
@@ -27,8 +33,9 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
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.security.converter.ResourceKeyConverterAdapter;
|
||||
import org.springframework.security.converter.RsaKeyConverters;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -104,4 +111,66 @@ public class RsaKeyConversionServicePostProcessor implements BeanFactoryPostProc
|
||||
|
||||
}
|
||||
|
||||
static class ResourceKeyConverterAdapter<T extends Key> implements Converter<String, T> {
|
||||
|
||||
private ResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
|
||||
private final Converter<String, T> delegate;
|
||||
|
||||
/**
|
||||
* Construct a {@link ResourceKeyConverterAdapter} with the provided parameters
|
||||
* @param delegate converts a stream of key material into a {@link Key}
|
||||
*/
|
||||
ResourceKeyConverterAdapter(Converter<InputStream, T> delegate) {
|
||||
this.delegate = pemInputStreamConverter().andThen(autoclose(delegate));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public T convert(String source) {
|
||||
return this.delegate.convert(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this {@link ResourceLoader} to read the key material
|
||||
* @param resourceLoader the {@link ResourceLoader} to use
|
||||
*/
|
||||
void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
Assert.notNull(resourceLoader, "resourceLoader cannot be null");
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user