ssl 使用PKCS11密钥库类型在Sping Boot 上的Web应用程序中建立TLS连接

68bkxrlz  于 2023-03-03  发布在  其他
关注(0)|答案(2)|浏览(234)

我试着用Sping Boot 为我的Web应用程序构建设置TLS连接。application.properties

server.port=9999
server.ssl.key-store-password=password
server.ssl.key-store=NONE
server.ssl.trust-store=C:/newcert/truststore.keystore
server.ssl.trust-store-password=password
server.ssl.key-store-provider=SunPKCS11-Thales
server.ssl.key-store-type=PKCS11
server.ssl.key-alias=test_key_ocs2

在java.security文件(%JAVA_HOME%/jre/lib/security/)中包含行:

security.provider.11=sun.security.pkcs11.SunPKCS11 C:/cert/pkcs11.cfg

在pkcs11.cfg文件中包含:

name=Thales
library = C:\nCipher\nfast\toolkits\pkcs11\cknfast-64.dll
slot=761406613

但运行我的应用程序后,我得到错误:

INFO 16788 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
ERROR 16788 --- [           main] org.apache.tomcat.util.net.SSLUtilBase   : Failed to load keystore type [PKCS11] with path [file:/C:/ProjectsGit/ssl-server/NONE] due to [Initialization failed]

java.security.ProviderException: Initialization failed
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:376) ~[sunpkcs11.jar:1.8.0_161]
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:103) ~[sunpkcs11.jar:1.8.0_161]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_152]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_152]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_152]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_152]
    at sun.security.jca.ProviderConfig$2.run(ProviderConfig.java:224) ~[na:1.8.0_152]
    at sun.security.jca.ProviderConfig$2.run(ProviderConfig.java:206) ~[na:1.8.0_152]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_152]
    at sun.security.jca.ProviderConfig.doLoadProvider(ProviderConfig.java:206) ~[na:1.8.0_152]
    at sun.security.jca.ProviderConfig.getProvider(ProviderConfig.java:187) ~[na:1.8.0_152]
    at sun.security.jca.ProviderList.getProvider(ProviderList.java:233) ~[na:1.8.0_152]
    at sun.security.jca.ProviderList.getIndex(ProviderList.java:263) ~[na:1.8.0_152]
    at sun.security.jca.ProviderList.getProviderConfig(ProviderList.java:247) ~[na:1.8.0_152]
    at sun.security.jca.ProviderList.getProvider(ProviderList.java:253) ~[na:1.8.0_152]
    at sun.security.jca.GetInstance.getService(GetInstance.java:81) ~[na:1.8.0_152]
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:206) ~[na:1.8.0_152]
    at java.security.Security.getImpl(Security.java:698) ~[na:1.8.0_152]
    at java.security.KeyStore.getInstance(KeyStore.java:896) ~[na:1.8.0_152]
    at org.apache.tomcat.util.net.SSLUtilBase.getStore(SSLUtilBase.java:187) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.SSLHostConfigCertificate.getCertificateKeystore(SSLHostConfigCertificate.java:206) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.SSLUtilBase.getKeyManagers(SSLUtilBase.java:283) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.SSLUtilBase.createSSLContext(SSLUtilBase.java:247) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:97) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:71) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:218) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1124) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1210) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:586) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:1005) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:227) [tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:278) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:197) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:297) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) [spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at com.example.sslserver.SslServerApplication.main(SslServerApplication.java:10) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_FUNCTION_FAILED
    at sun.security.pkcs11.wrapper.PKCS11.C_Initialize(Native Method) ~[sunpkcs11.jar:1.8.0_161]
    at sun.security.pkcs11.wrapper.PKCS11$SynchronizedPKCS11.C_Initialize(PKCS11.java:1545) ~[sunpkcs11.jar:1.8.0_161]
    at sun.security.pkcs11.wrapper.PKCS11.getInstance(PKCS11.java:157) ~[sunpkcs11.jar:1.8.0_161]
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:330) ~[sunpkcs11.jar:1.8.0_161]
    ... 48 common frames omitted

INFO 16788 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
INFO 16788 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
ERROR 16788 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:215) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:297) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at com.example.sslserver.SslServerApplication.main(SslServerApplication.java:10) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: java.lang.IllegalArgumentException: standardService.connector.startFailed
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:231) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:278) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:197) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    ... 15 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:1008) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:227) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    ... 17 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to load keystore type [PKCS11] with path [file:/C:/ProjectsGit/ssl-server/NONE] due to [Initialization failed]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:99) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:71) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:218) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1124) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1210) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:586) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:1005) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    ... 19 common frames omitted
Caused by: java.io.IOException: Failed to load keystore type [PKCS11] with path [file:/C:/ProjectsGit/ssl-server/NONE] due to [Initialization failed]
    at org.apache.tomcat.util.net.SSLUtilBase.getStore(SSLUtilBase.java:229) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.SSLHostConfigCertificate.getCertificateKeystore(SSLHostConfigCertificate.java:206) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.SSLUtilBase.getKeyManagers(SSLUtilBase.java:283) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.SSLUtilBase.createSSLContext(SSLUtilBase.java:247) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:97) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
    ... 25 common frames omitted

INFO 16788 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 1

请告诉我我做错了什么?
也许有其他方法可以使用PKCS 11提供者为服务请求建立TLS连接(定义KeyManager和TrustManager,使用SSLSocketFactory等),但我不是很了解,任何示例都非常有用。

c90pui9n

c90pui9n1#

它正在尝试在以下位置查找文件:文件:/C:/项目Git/ssl服务器/无

Caused by: java.io.IOException: Failed to load keystore type [PKCS11] with path [file:/C:/ProjectsGit/ssl-server/NONE]

你有这个地方的任何参考资料吗?

8oomwypt

8oomwypt2#

This issue已在Sping Boot v2.2.7和v3.0.1中修复。

相关问题