Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从需要密钥库的Springboot应用程序向服务器自动发送Post请求_Spring_Rest_Certificate - Fatal编程技术网

从需要密钥库的Springboot应用程序向服务器自动发送Post请求

从需要密钥库的Springboot应用程序向服务器自动发送Post请求,spring,rest,certificate,Spring,Rest,Certificate,每次特定文件夹上发生事件时,我开发的应用程序都必须发送post请求。 我已经构建了我想发布的JSON 我的问题如下:请求必须使用P12证书。为此,我使用了: keytool -importkeystore -srcstoretype PKCS12 -srckeystore MYCERTIFICATE.p12 -destkeystore keystore.jks -deststoretype pkcs12 我设置了一个yaml文件,其中包含有关密钥库的信息: server: ssl:

每次特定文件夹上发生事件时,我开发的应用程序都必须发送post请求。 我已经构建了我想发布的JSON

我的问题如下:请求必须使用P12证书。为此,我使用了:

keytool -importkeystore -srcstoretype PKCS12 -srckeystore MYCERTIFICATE.p12 -destkeystore keystore.jks -deststoretype pkcs12
我设置了一个yaml文件,其中包含有关密钥库的信息:

server:
  ssl:
    key-store-type: PKCS12
    key-store: 'classpath:keystore.jks'
    key-store-password: password
    key-alias: macsf_c
最后,我创建了一个将请求发送到远程服务器的方法:

public DocumentDto sendRequest(DocumentDto documentDto) throws IOException {

    // set the URL to send the request
    URL url = new URL(properties.getProperty("signature.api.url.full"));
    // opening the connection
    HttpURLConnection con = (HttpURLConnection)url.openConnection();

    // set protocol, accept & content type format
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json; utf-8");
    con.setRequestProperty("Accept", "application/json");
    con.setDoOutput(true);

    System.out.println(con.toString());

    // create the JSON String
    ObjectMapper mapper = new ObjectMapper();
    // for test purpose : generate the JSON file
    mapper.writeValue(new File("C:\\Users\\Dvera\\Documents\\testSignature\\document.json"), documentDto);
    // convert an oject to a json string
    String jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(documentDto);

    try(OutputStream os = con.getOutputStream()) {
        byte[] input = jsonInString.getBytes("utf-8");
        os.write(input, 0, input.length);
    }

    // read the response
    try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
        StringBuilder response = new StringBuilder();
        String responseLine = null;
        while ((responseLine = br.readLine()) != null) {
            response.append(responseLine.trim());
        }
        System.out.println(response.toString());
    }

    System.out.println(jsonInString);
    return null;
}

实际上,我不是在处理返回类型。我的问题是我有一个403错误。我确实认为我必须将密钥库添加到我的请求中,我现在陷入了困境

server.ssl
用于将ssl配置为与嵌入式tomcat的ssl一起使用,而不是用于从应用程序发出请求。我删除了它,但它不会更改我的问题,因为它不会更改,因为现在您在没有ssl的情况下运行应用程序(即纯http而不是https)。我建议使用
restemplate
并使用apachehttp库配置要使用的SSL证书。