org.apache.xml.security在Spring Boot下无法运行

时间:2017-08-03 04:58:06

标签: java eclipse spring-boot

我正在尝试使用org.apache.santuario

如果我在Spring Boot主方法中调用它,它将失败:

10:31:40.245 [main] ERROR org.apache.xml.security.Init - Bad: 
java.lang.IllegalArgumentException: InputStream cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:117)
    at org.apache.xml.security.Init.init(Unknown Source)
    at ru.gosuslugi.dom.signature.demo.commands.SignCommand.<init>(SignCommand.java:55)
    at com.ric.web.Gisxn2Application.main(Gisxn2Application.java:18)
java.lang.IllegalArgumentException: InputStream cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:117)

我的主要方法:

package com.ric.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import ru.gosuslugi.dom.signature.demo.commands.Command;
import ru.gosuslugi.dom.signature.demo.commands.SignCommand;

@SpringBootApplication
public class Gisxn2Application {

    public static Command sc;

    public static void main(String[] args) {

        //Создать объект подписывания XML
        try {
            sc = new SignCommand();
            System.out.println("Объект подписывания XML СОЗДАН!");
        } catch (Exception e1) {
            System.out.println("Объект подписывания XML не создан!");
            e1.printStackTrace();
        }

        SpringApplication.run(Gisxn2Application.class, args);


    }
}

但是如果我从非@SpringBootApplication - 类主模块调用它,它可以完美地工作。 我使用下一个Jvm param来处理这个程序:

-Dorg.apache.xml.security.resource.config=resource/tj-msxml.xml

有人可以描述这个问题是什么,为什么会发生?

Upd1: SignCommand:

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import xades4j.algorithms.EnvelopedSignatureTransform;
import xades4j.algorithms.ExclusiveCanonicalXMLWithoutComments;
import xades4j.production.*;
import xades4j.properties.DataObjectDesc;
import xades4j.providers.KeyingDataProvider;
import xades4j.providers.MessageDigestEngineProvider;
import xades4j.providers.impl.DirectKeyingDataProvider;

import java.security.KeyException;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;
import java.security.cert.X509Certificate;

public class SignCommand implements Command {

    private SignParameters parameters;
    private XadesSigner signer;

    //конструктор
    public SignCommand(SignParameters parameters) {
        System.out.println("############ SIGNCOMMAND CONSTRUCTOR START!");
        org.apache.xml.security.Init.init();
        this.parameters = parameters;
    }

    //конструктор
    public SignCommand() throws Exception {
        // init Apache Santuario
        org.apache.xml.security.Init.init();  <--- here error

UPD2: 我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ric.soap</groupId>
    <artifactId>Soap2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <repositories>
        <repository>
            <id>cata1</id>
            <name>cata</name>
            <url>http://repo.boundlessgeo.com/main/</url>
        </repository>

    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- Spring -->
        <spring-framework.version>4.2.5.RELEASE</spring-framework.version>
        <!-- Hibernate / JPA -->
        <hibernate.version>5.1.0.Final</hibernate.version>
        <!-- Test -->
        <junit.version>4.12</junit.version>

        <start-class>com.ric.web.SoapWebApplication</start-class>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>

        <!-- BWC -->
        <dependency>
            <groupId>com.ric.bill</groupId>
            <artifactId>BWC</artifactId>
            <version>0.0.2-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

<!--        <dependency>
            <groupId>checkSoap</groupId>
            <artifactId>chksp</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency> 
 -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-loader</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <!-- Не удалять! Используется для AuthConfigFactory!!! -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>9.0.0.M6</version>
        </dependency>
<!--        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.2</version>
        </dependency> -->

        <!-- https://mvnrepository.com/artifact/javax.security.auth.message/javax.security.auth.message-api -->
        <dependency>
            <groupId>javax.security.auth.message</groupId>
            <artifactId>javax.security.auth.message-api</artifactId>
            <version>1.1</version>
        </dependency>

        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
          <groupId>org.ehcache</groupId>
          <artifactId>ehcache2</artifactId>
          <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-terracotta</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
          <groupId>javax.cache</groupId>
          <artifactId>cache-api</artifactId>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>oracle</artifactId>
            <version>10.2.0.2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.diffplug.durian/durian -->
        <dependency>
            <groupId>com.diffplug.durian</groupId>
            <artifactId>durian</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>com.beust</groupId>
            <artifactId>jcommander</artifactId>
            <version>1.48</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk15on</artifactId>
            <version>1.55</version>
        </dependency>

        <dependency>
            <groupId>org.signserver.xades4j</groupId>
            <artifactId>xades4j</artifactId>
            <version>1.3.2-signserver4</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.2.7</version>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                <source>1.8</source>
                <target>1.8</target>
              </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
              <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-loader</artifactId>
                        </exclude>
                    </excludes>
              </configuration>                

              <executions>
                <execution>
                  <goals>
                    <goal>repackage</goal>
                  </goals>
                </execution>
              </executions>

            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>


</project>

1 个答案:

答案 0 :(得分:0)

我发现了下一个Jvm param:

-Dorg.apache.xml.security.resource.config =资源/ TJ-msxml.xml

每当我运行程序时,

主程序内的更改。

为了找到它,我在主程序中写了下一行:

System.out.println("PROP1="+System.getProperty("org.apache.xml.security.resource.config"));

输出为&#34; PROP1 = resource / tj-msxml.xml-noverify&#34; 这很奇怪,因为,我希望&#34; PROP1 = resource / tj-msxml.xml&#34;

我已使用以下方法纠正了此问题:

System.setProperty("org.apache.xml.security.resource.config", "resource/tj-msxml.xml");

但我对我的解决方案不满意,可能有人纠正我了吗?