|
18 | 18 |
|
19 | 19 | import java.beans.PropertyEditor;
|
20 | 20 | import java.beans.PropertyEditorSupport;
|
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.UncheckedIOException; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.security.Key; |
21 | 27 | import java.security.interfaces.RSAPrivateKey;
|
22 | 28 | import java.security.interfaces.RSAPublicKey;
|
23 | 29 |
|
|
27 | 33 | import org.springframework.core.convert.ConversionService;
|
28 | 34 | import org.springframework.core.convert.converter.Converter;
|
29 | 35 | import org.springframework.core.convert.converter.ConverterRegistry;
|
| 36 | +import org.springframework.core.io.DefaultResourceLoader; |
| 37 | +import org.springframework.core.io.Resource; |
30 | 38 | import org.springframework.core.io.ResourceLoader;
|
31 |
| -import org.springframework.security.converter.ResourceKeyConverterAdapter; |
32 | 39 | import org.springframework.security.converter.RsaKeyConverters;
|
33 | 40 | import org.springframework.util.Assert;
|
34 | 41 | import org.springframework.util.StringUtils;
|
@@ -104,4 +111,66 @@ public void setAsText(String text) throws IllegalArgumentException {
|
104 | 111 |
|
105 | 112 | }
|
106 | 113 |
|
| 114 | + static class ResourceKeyConverterAdapter<T extends Key> implements Converter<String, T> { |
| 115 | + |
| 116 | + private ResourceLoader resourceLoader = new DefaultResourceLoader(); |
| 117 | + |
| 118 | + private final Converter<String, T> delegate; |
| 119 | + |
| 120 | + /** |
| 121 | + * Construct a {@link ResourceKeyConverterAdapter} with the provided parameters |
| 122 | + * @param delegate converts a stream of key material into a {@link Key} |
| 123 | + */ |
| 124 | + ResourceKeyConverterAdapter(Converter<InputStream, T> delegate) { |
| 125 | + this.delegate = pemInputStreamConverter().andThen(autoclose(delegate)); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * {@inheritDoc} |
| 130 | + */ |
| 131 | + @Override |
| 132 | + public T convert(String source) { |
| 133 | + return this.delegate.convert(source); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Use this {@link ResourceLoader} to read the key material |
| 138 | + * @param resourceLoader the {@link ResourceLoader} to use |
| 139 | + */ |
| 140 | + void setResourceLoader(ResourceLoader resourceLoader) { |
| 141 | + Assert.notNull(resourceLoader, "resourceLoader cannot be null"); |
| 142 | + this.resourceLoader = resourceLoader; |
| 143 | + } |
| 144 | + |
| 145 | + private Converter<String, InputStream> pemInputStreamConverter() { |
| 146 | + return (source) -> source.startsWith("-----") ? toInputStream(source) |
| 147 | + : toInputStream(this.resourceLoader.getResource(source)); |
| 148 | + } |
| 149 | + |
| 150 | + private InputStream toInputStream(String raw) { |
| 151 | + return new ByteArrayInputStream(raw.getBytes(StandardCharsets.UTF_8)); |
| 152 | + } |
| 153 | + |
| 154 | + private InputStream toInputStream(Resource resource) { |
| 155 | + try { |
| 156 | + return resource.getInputStream(); |
| 157 | + } |
| 158 | + catch (IOException ex) { |
| 159 | + throw new UncheckedIOException(ex); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private <T> Converter<InputStream, T> autoclose(Converter<InputStream, T> inputStreamKeyConverter) { |
| 164 | + return (inputStream) -> { |
| 165 | + try (InputStream is = inputStream) { |
| 166 | + return inputStreamKeyConverter.convert(is); |
| 167 | + } |
| 168 | + catch (IOException ex) { |
| 169 | + throw new UncheckedIOException(ex); |
| 170 | + } |
| 171 | + }; |
| 172 | + } |
| 173 | + |
| 174 | + } |
| 175 | + |
107 | 176 | }
|
0 commit comments