Я разработал код, чтобы проверить, найден ли на компьютере определенный токен безопасности перед определенной операцией.Код, который я сейчас использую для получения хранилища ключей:
private KeyStore getKeyStore() {
try {
KeyStore keyStore = KeyStore.getInstance("PKCS11",getProvider()); //$NON-NLS-1$
char [] pin = "mypin".toCharArray();
keyStore.load(null, pin);
return keyStore;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Provider getProvider() throws ClassNotFoundException,
NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> lclass = ClassLoader.getSystemClassLoader().getParent().loadClass("sun.security.pkcs11.SunPKCS11"); //$NON-NLS-1$
@SuppressWarnings("rawtypes")
Constructor constructor = lclass.getConstructor(String.class);
return (Provider) constructor.newInstance(configName);
}
Если токен безопасности (смарт-карта USB) присутствует при первой попытке приложения получить доступ к хранилищу ключей, он работает без ошибок;но когда смарт-карта отсутствует, когда приложения пытаются получить токен в первый раз, KeyStore.getInstance
вызывает следующее исключение:
java.security.KeyStoreException: PKCS11 not found
at java.security.KeyStore.getInstance(Unknown Source)
at es.giro.girlabel.token.Token.getKeyStore(Token.java:120)
at es.giro.girlabel.token.Token.getCertificate(Token.java:156)
at es.giro.girlabel.token.Token.hasToken(Token.java:150)
at es.giro.girlabel.jobs.JobList.getInstance(JobList.java:73)
at es.giro.girlabel.editor.view.LabelListView$10.run(LabelListView.java:499)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at es.giro.girlabel.Application.start(Application.java:39)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
at org.eclipse.equinox.launcher.Main.main(Main.java:1384)
Caused by: java.security.NoSuchAlgorithmException: no such algorithm: PKCS11 for provider SunPKCS11-eToken
at sun.security.jca.GetInstance.getService(Unknown Source)
at sun.security.jca.GetInstance.getInstance(Unknown Source)
at java.security.Security.getImpl(Unknown Source)
... 35 more
После первой попытки, если я снова подключу токен безопасности, иПовторите операцию, я получаю точно такую же ошибку.
Кэширует ли класс SunPKCS11 некоторые данные?Если так, есть ли способ очистить этот кеш?
Спасибо за ваше внимание и, пожалуйста, простите мое правописание.Английский не мой родной язык.
РЕДАКТИРОВАТЬ:
Полная трассировка стека:
java.security.KeyStore.getInstance(Unknown Source)
es.giro.girlabel.token.Token.getKeyStore(Token.java:119)
es.giro.girlabel.token.Token.getCertificate(Token.java:160)
es.giro.girlabel.token.Token.hasToken(Token.java:154)
es.giro.girlabel.jobs.JobList.getInstance(JobList.java:73)
es.giro.girlabel.editor.view.LabelListView$10.run(LabelListView.java:499)
org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
es.giro.girlabel.Application.start(Application.java:39)
org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
org.eclipse.equinox.launcher.Main.run(Main.java:1408)
org.eclipse.equinox.launcher.Main.main(Main.java:1384)