使用 HTTP 4.1.1 的 ClientCustomSSL

发布于 2024-11-18 06:46:14 字数 827 浏览 5 评论 0原文

谁能告诉我我们是否必须创建 my.keystore 文件,否则它将被创建。当我在 jsp 文件所在的同一目录(webapp 目录)中创建此文件时,我在 jsp 文件中使用此代码。

KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());        
FileInputStream instream = new FileInputStream(new File("my.keystore")); 
try {
    trustStore.load(instream, "nopassword".toCharArray());
} finally {
    instream.close();
}

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

我收到错误

java.io.FileNotFoundException: my.keystore (The system cannot find the file spec
ified)

那么我应该把这个文件放在哪里。这是两个文件(即 jsp 文件和 my.keystore)的路径

C:\workspace\search-ui\search-ui\src\main\webapp

Can anybody tell me whether we have to create my.keystore file or it will be created. And when I created this file in the same directory(webapp directory) where my jsp file is and I am using this code in my jsp file.

KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());        
FileInputStream instream = new FileInputStream(new File("my.keystore")); 
try {
    trustStore.load(instream, "nopassword".toCharArray());
} finally {
    instream.close();
}

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

And I got the error

java.io.FileNotFoundException: my.keystore (The system cannot find the file spec
ified)

So where should I put this file. this is the path for both of the file, that jsp file and my.keystore

C:\workspace\search-ui\search-ui\src\main\webapp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-11-25 06:46:14

我建议将密钥库添加到类路径中,然后通过

InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.keystore");

将密钥库放在 webapp 下但不在 WEB-INF 下加载它是不安全的,因为任何人都可以下载它。此外,当使用相对文件路径时,通过 new File() 加载文件会在工作目录中查找 Java 进程(请参阅 JavaDoc)。这很可能位于您正在使用的 Servlet 容器的目录层次结构下的某个位置。

I would recommend adding the keystore to the classpath and then load it via

InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.keystore");

Putting the keystore under webapp but not under WEB-INF is insecure since anyone could just download it. Also, loading a file via new File() looks in the working directory for the Java process when a relative file path is used (see the JavaDoc). This is most likely somewhere under the directory hierarchy of the Servlet container you are using.

鸠魁 2024-11-25 06:46:14

除非您的服务器需要客户端身份验证,否则您根本不需要密钥库。是吗?

You don't need a keystore at all unless your server requires client authentication. Does it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文