I want to pass a File
object while using loadKeyMaterial()
api in Spring application. I am using a jar file to run my application, and have faced the problem when accessing the File
directly in the jar.
So, I am using InputStream
to access the File from the Resources.
How do I convert this InputStream
to a File
object for the loadKeyMaterial()
?
Asked
Active
Viewed 1,000 times
0

Jiri Tousek
- 12,211
- 5
- 29
- 43

pixelWorld
- 329
- 2
- 17
-
Possible duplicate of [Is it possible to create a File object from InputStream](https://stackoverflow.com/questions/11501418/is-it-possible-to-create-a-file-object-from-inputstream) – Antho Christen Jun 11 '18 at 07:03
-
1Indeed, but creating a temporary file somewhere with the keys sounds less than optimal. Sure, keys bundled in a jar are not very secure either, but one step better than putting them in the temp folder! – ewramner Jun 11 '18 at 07:07
-
@anchreg From the above link : Is it possible to create a File object from InputStream they have mentioned to create File file = new File(); However, I have seen that we cannot access a file directly when we create a jar. So I am not sure where to create that temp file in the resources folder. – pixelWorld Jun 11 '18 at 07:08
-
@ewramner..so how to proceed with that approach ? – pixelWorld Jun 11 '18 at 07:16
-
@pixelWorld, You can read/write files outside the JAR, long as the user running the JVM process has the required permissions. Jiri Tousek's answers seems like a good fit, can you not use the overloaded method? – Antho Christen Jun 11 '18 at 07:25
1 Answers
4
You could use loadKeyMaterial()
overload that uses KeyStore
rather than File
parameter. The KeyStore
may be loaded from a stream, there's an example for that in KeyStore Javadoc.

Jiri Tousek
- 12,211
- 5
- 29
- 43
-
I have to use the SSLContexts.custom().loadKeyMaterial() api in order to initialize the SSLContext object, I don't see with the above workaround I can do that. – pixelWorld Jun 11 '18 at 21:34