我正在尝试从 Mac OSX 10.6 上的 Java KeyStore 检索条目。我的代码在 Windows 和 Linux 上运行良好,但是当我在 OSX 上运行它时,出现以下异常:
java.lang.UnsupportedOperationException
at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:466)
at java.security.KeyStore.getEntry(KeyStore.java:1261)
这是我的代码:
String keyStorePath = ...
PasswordProtection pp = new PasswordProtection("password".toCharArray());
CallbackHandlerProtection chp = new CallbackHandlerProtection(
new CallbackHandler() {
@Override
public void handle(Callback[] callbacks)
throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callbacks[i];
pc.setPassword("password".toCharArray());
}
}
}
});
try {
KeyStore.Builder kb = Builder.newInstance("JCEKS", null, new File(
keyStorePath), chp);
KeyStore ks = kb.getKeyStore();
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
KeyStore.Entry entry = ks.getEntry(alias, chp);
}
} catch (Exception e) {
e.printStackTrace();
}
知道为什么 OSX 上会抛出这个异常吗?这是该操作系统上 JVM 中的错误吗?有人以前见过这个吗?
最佳答案
查看第 466 行处的 KeyStoreSpi.java
的实现,可以发现以下内容:
public KeyStore.Entry engineGetEntry(String alias, ...) throws ... {
...
if (protParam instanceof KeyStore.PasswordProtection) {
if (engineIsCertificateEntry(alias)) {
throw new UnsupportedOperationException
("trusted certificate entries are not password-protected");
} else if ...
}
...
}
这里您可以了解抛出异常的确切条件。
那么engineIsCertificateEntry(alias)
什么时候返回true?
根据documentation确实如此...
... if the entry identified by the given alias was created by a call to
setCertificateEntry
, or created by a call tosetEntry
with aTrustedCertificateEntry
然而,该方法是抽象的,因此在不知道所使用的确切实现的情况下很难进一步挖掘。根据您的描述,实现之间的逻辑似乎略有不同。
关于java - 使用 KeyStore.getEntry() 时出现 UnsupportedOperationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6433748/