ClassCastException While Create an org.apache.poi.poifs.cryp



我试图使用Apache POI将保护添加到MS Excel文件中,当我进行一些测试时它工作正常,但是当我在Weblogic Server 12中证明运行时的功能时,它失败了。这是我的代码:


public static void parseFileToSecureFile(String fullFileName, String password) throws MyException{
    POIFSFileSystem fs = new POIFSFileSystem();
    EncryptionInfo info =
        new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes256, HashAlgorithm.sha512, -1, -1, null);
    Encryptor enc = info.getEncryptor();
    enc.confirmPassword(password);
    OPCPackage opc = null;
    OutputStream os = null;
    FileOutputStream fos = null;
    try {
        opc = OPCPackage.open(new File(fullFileName), PackageAccess.READ_WRITE);
        os = enc.getDataStream(fs);
        opc.save(os);
        fos = new FileOutputStream(fullFileName);
        fs.writeFilesystem(fos);
    } catch (Exception e) {
        logger.error("error: "+e.getMessage(), e);
        throw new MyException("An Error");
    }finally {
         fs.close();
         fos.close();
    }
}
public static void main(String[] args){
    System.out.println("Inicia");
    try {
        parseFileToSecureFile("/my_path/my_file.xlsx", "11");
    } catch (MyException e) {
        e.printStackTrace();
    }
    System.out.println("Finaliza");
}

正如我告诉您的,如果我从我的"main 方法"按原样运行我的代码,它可以工作,但是如果我在运行时从 Web 应用程序调用该方法,我有下一个异常:


org.apache.poi.EncryptedDocumentException: java.lang.ClassCastException: org.apache.poi.poifs.crypt.agile.AgileEncryptionInfoBuilder cannot be cast to org.apache.poi.poifs.crypt.EncryptionInfoBuilder
    at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:186)
    at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:153)
    at com.tets.test.test.FileCipher.parserFileToSecureFile(FileCipher.java:39)
    at com.test.test.test.backing.MyAdmin.doSomething(Something.java:387)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.apache.myfaces.trinidadinternal.taglib.util.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:53)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.broadcastToMethodBinding(UIXComponentBase.java:1988)
    at org.apache.myfaces.trinidad.component.UIXEditableValue.broadcast(UIXEditableValue.java:257)
    at org.apache.myfaces.trinidad.component.UIXComponent.broadcastInContext(UIXComponent.java:364)
    at org.apache.myfaces.trinidad.component.WrapperEvent.broadcastWrappedEvent(WrapperEvent.java:82)
    at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent$1.run(ContextSwitchingComponent.java:168)
    at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent._processPhase(ContextSwitchingComponent.java:510)
    at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent.broadcast(ContextSwitchingComponent.java:171)
    at org.apache.myfaces.trinidad.component.UIXComponent.broadcastInContext(UIXComponent.java:364)
    at org.apache.myfaces.trinidad.component.WrapperEvent.broadcastWrappedEvent(WrapperEvent.java:82)
    at oracle.adf.view.rich.component.fragment.UIXInclude.broadcast(UIXInclude.java:111)
    at org.apache.myfaces.trinidad.component.UIXComponent.broadcastInContext(UIXComponent.java:364)
    at org.apache.myfaces.trinidad.component.WrapperEvent.broadcastWrappedEvent(WrapperEvent.java:82)
    at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent$1.run(ContextSwitchingComponent.java:168)
    at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent._processPhase(ContextSwitchingComponent.java:510)
    at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent.broadcast(ContextSwitchingComponent.java:171)
    at oracle.adf.view.rich.component.fragment.UIXInclude.broadcast(UIXInclude.java:115)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:755)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executeApplyValidateOrUpdate(LifecycleImpl.java:642)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:370)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:227)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:650)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:207)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter(RegistrationFilter.java:105)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:529)
    at oracle.adfinternal.view.faces.activedata.AdsFilter.doFilter(AdsFilter.java:60)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:529)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:354)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:232)
    at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at com.sky.osm.view.FilterSession.doFilter(FilterSession.java:76)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:141)
    at java.security.AccessController.doPrivileged(Native Method)
    at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:315)
    at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:650)
    at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:124)
    at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:232)
    at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:94)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:248)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:32)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3683)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3649)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
    at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
    at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2433)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2281)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2259)
    at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1686)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1646)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:270)
    at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
    at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
    at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
    at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
    at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:640)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:406)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)

感谢您的帮助

如果其他人有同样的问题,这是我必须做的更改:

  • 将我的 POI 版本从 v3.15 更新到 v4.0.1
  • 添加共享压缩 v1.18

此外,如果您必须读取加密文件,您将需要:

  • XMLbean-3.0.2
  • 共享资源集合4-4.1

Weblogic Server 有自己的 xmlbeans 版本,您将遇到依赖冲突,因此,您需要将容器描述符标记中的下一个标记添加到 weblogic.xml 文件中:

<prefer-web-inf-classes>false</prefer-web-inf-classes>

您还需要将下一个标签添加到 weblogic-application.xml 文件中:

<prefer-application-packages>
        <package-name>org.apache.xmlbeans.*</package-name>
        <package-name>repackage.*</package-name>
        <package-name>schemaorg_apache_xmlbeans.system.*</package-name>
 </prefer-application-packages>

相关内容

  • 没有找到相关文章

最新更新