最近在一个工程中需要实现非对称加解密功能,于是使用Java提供的类org.bouncycastle.jce.provider.BouncyCastleProvider,进一步引入jar包bcprov-jdk16-146.jar,在Mavean工程中直接加入依赖:
org.bouncycastle
bcprov-jdk16
1.46
部署到Jboss7.2.0中,然后启动工程,出现错误:
2017-10-23 21:18:49,273 INFO [stdout] (web-container-thread-1) java.lang.SecurityException: JCE cannot authenticate the provider BC
2017-10-23 21:18:49,273 INFO [stdout] (web-container-thread-1) at javax.crypto.Cipher.getInstance(DashoA13*..)
2017-10-23 21:18:49,273 INFO [stdout] (web-container-thread-1) at com.unionpay.upchat.pub.console.util.SecurityUtil.(SecurityUtil.java:41)
2017-10-23 21:18:49,273 INFO [stdout] (web-container-thread-1) at com.unionpay.upchat.pub.console.restservice.UserResource.login(UserResource.java:111)
2017-10-23 21:18:49,274 INFO [stdout] (web-container-thread-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-10-23 21:18:49,274 INFO [stdout] (web-container-thread-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2017-10-23 21:18:49,274 INFO [stdout] (web-container-thread-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2017-10-23 21:18:49,274 INFO [stdout] (web-container-thread-1) at java.lang.reflect.Method.invoke(Method.java:597)
2017-10-23 21:18:49,275 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
2017-10-23 21:18:49,275 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
2017-10-23 21:18:49,275 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
2017-10-23 21:18:49,275 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
2017-10-23 21:18:49,275 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542)
2017-10-23 21:18:49,276 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524)
2017-10-23 21:18:49,276 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126)
2017-10-23 21:18:49,276 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
2017-10-23 21:18:49,276 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
2017-10-23 21:18:49,276 INFO [stdout] (web-container-thread-1) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
2017-10-23 21:18:49,283 INFO [stdout] (web-container-thread-1) Caused by: java.util.jar.JarException: Cannot parse vfs:/content/upchat-public-console-web.war/WEB-INF/lib/bcprov-jdk16-1.46.jar
2017-10-23 21:18:49,283 INFO [stdout] (web-container-thread-1) at javax.crypto.SunJCE_c.a(DashoA13*..)
2017-10-23 21:18:49,284 INFO [stdout] (web-container-thread-1) at javax.crypto.SunJCE_b.b(DashoA13*..)
2017-10-23 21:18:49,284 INFO [stdout] (web-container-thread-1) at javax.crypto.SunJCE_b.a(DashoA13*..)
2017-10-23 21:18:49,284 INFO [stdout] (web-container-thread-1) ... 46 more
2017-10-23 21:18:49,285 ERROR [stderr] (web-container-thread-1) java.lang.SecurityException: JCE cannot authenticate the provider BC
2017-10-23 21:18:49,285 ERROR [stderr] (web-container-thread-1) at javax.crypto.Cipher.getInstance(DashoA13*..)
2017-10-23 21:18:49,286 ERROR [stderr] (web-container-thread-1) at com.unionpay.upchat.pub.console.util.SecurityUtil.(SecurityUtil.java:41)
2017-10-23 21:18:49,286 ERROR [stderr] (web-container-thread-1) at com.unionpay.upchat.pub.console.restservice.UserResource.login(UserResource.java:111)
2017-10-23 21:18:49,286 ERROR [stderr] (web-container-thread-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-10-23 21:18:49,286 ERROR [stderr] (web-container-thread-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2017-10-23 21:18:49,287 ERROR [stderr] (web-container-thread-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2017-10-23 21:18:49,287 ERROR [stderr] (web-container-thread-1) at java.lang.reflect.Method.invoke(Method.java:597)
2017-10-23 21:18:49,287 ERROR [stderr] (web-container-thread-1) at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
2017-10-23 21:18:49,288 ERROR [stderr] (web-container-thread-1) at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
按照网上提供的方法:
<scope>providedscope>
security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider to java.security
发现并不能解决问题,仍然报错:
2017-10-23 18:53:51,924 INFO [org.jboss.weld.ClassLoading] (MSC service thread 1-4) catching: org.jboss.weld.resources.spi.ResourceLoadingException: Error loading class com.unionpay.upchat.pub.console.util.SecurityUtil
Caused by: java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at java.lang.Class.getDeclaredFields0(Native Method) [rt.jar:1.6.0_45]
at java.lang.Class.privateGetDeclaredFields(Class.java:2300) [rt.jar:1.6.0_45]
at java.lang.Class.getDeclaredFields(Class.java:1745) [rt.jar:1.6.0_45]
at org.jboss.weld.util.reflection.SecureReflections$4.work(SecureReflections.java:105) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.util.reflection.SecureReflections$4.work(SecureReflections.java:102) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.util.reflection.SecureReflectionAccess.runAndWrap(SecureReflectionAccess.java:63) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.util.reflection.SecureReflections.getDeclaredFields(SecureReflections.java:102) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.introspector.jlr.WeldClassImpl.(WeldClassImpl.java:155) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.introspector.jlr.WeldClassImpl.of(WeldClassImpl.java:121) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.resources.ClassTransformer$TransformTypeToWeldClass.apply(ClassTransformer.java:59) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at org.jboss.weld.resources.ClassTransformer$TransformTypeToWeldClass.apply(ClassTransformer.java:50) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
at com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:358) [guava-18.0.jar:]
at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184) [guava-18.0.jar:]
at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153) [guava-18.0.jar:]
at com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69) [guava-18.0.jar:]
at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:396)
at org.jboss.weld.resources.ClassTransformer.loadClass(ClassTransformer.java:163) [weld-core-1.1.10.Final.jar:2012-10-12 10:00]
... 11 more
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider from [Module "deployment.upchat-public-console-web.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:196) [jboss-modules.jar:1.2.2.Final]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:444) [jboss-modules.jar:1.2.2.Final]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:432) [jboss-modules.jar:1.2.2.Final]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:399) [jboss-modules.jar:1.2.2.Final]
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:374) [jboss-modules.jar:1.2.2.Final]
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:119) [jboss-modules.jar:1.2.2.Final]
经过一番研究,可以按照如下步骤解决上述存在的问题:
(1)project的pom.xml增加:
org.bouncycastle
bcprov-jdk16
1.46
provided
(2)在$JBOSS_HOME/modules/system/layers/base/org/bouncycastle/main下,添加bcprov-jdk16-146.jar、module.xml
其中module.xml内容如下:
<global-modules>
<module name="org.bouncycastle" slot="main"/>
</global-modules>