当前位置: 首页 > 知识库问答 >
问题:

Spring引导中的SSL证书

高溪叠
2023-03-14

我有证书文件xx.crtxx.pfx.我也有密码xx.pfx.如何配置这个Spring启动嵌入式tomcat?

    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
    connector.setScheme("https");
    connector.setSecure(true);
    protocol.setSSLEnabled(true);
    protocol.setKeystoreFile(??);
    protocol.setKeyPass(??);
    protocol.setTruststoreFile(??);
    return connector;

共有3个答案

仉运乾
2023-03-14

如果你看Http11NioProtocol,这些方法需要字符串参数...不管怎样,这里有一个相关的问题/答案我如何使用Spring Boot和Tomcat指定我的. keystore文件?

拓拔泉
2023-03-14

最简单的使用方法。带有Spring Boot的pfx文件
请按照以下步骤进行操作
1)从服务提供商处获取pfx文件
2)安装证书。有关如何安装证书,请参阅此链接。

应用性质

server.port= 9091
server.ssl.trust-store= classpath:jks/Test_Certificate.pfx
server.ssl.trust-store-password= XXXXXX
server.ssl.enabled= false
server.ssl.trust-store-type= PKCS12
soap.url=https://localost:8080/test/Calculator.wsdl

TestClientConfiguration。JAVA

import java.io.IOException;
import java.security.KeyStore;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.transport.http.HttpComponentsMessageSender;

@Configuration
public class TestClientConfiguration {

    private final String SOAP_URI = "https://localost:8080/test/Calculator.wsdl";

    @Value("${server.ssl.trust-store}")
    private Resource trustStore;

    @Value("${server.ssl.trust-store-password}")
    private String trustStorePassword;

    @Value("${server.ssl.trust-store-type}")
    private String trustStoreType;

    @Bean
    public TestServiceClient processMessage(Jaxb2Marshaller marshaller) throws Exception {
        TestServiceClient client = new TestServiceClient();
        client.setDefaultUri(SOAP_URI);
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        client.setMessageSender(httpComponentsMessageSender());
        return client;
    }

    @Bean
    Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath("com.test.soap.types");

        return jaxb2Marshaller;
    }

    @Bean
    public HttpComponentsMessageSender httpComponentsMessageSender() throws Exception {
        HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
        httpComponentsMessageSender.setHttpClient(httpClient());
        return httpComponentsMessageSender;
    }

    public HttpClient httpClient() throws Exception {
        KeyStore keyStore = KeyStore.getInstance(trustStoreType);
        keyStore.load(trustStore.getInputStream(), trustStorePassword.toCharArray());

        SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, trustStorePassword.toCharArray())
                .build();

        HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext)
                .addInterceptorFirst(new ContentLengthHeaderRemover()).build();
        return httpClient;
    }

}

TestServiceClient。JAVA

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapMessage;

@Qualifier("testSoapServiceClient")
public class TestServiceClient extends WebServiceGatewaySupport {
    @Value("${soap.url}")
    private String testsoapurl;

    public String processMessage(String mgRequest, Message reqHeader) {

        StreamSource source = new StreamSource(new StringReader(mgRequest));
        StringWriter stringWriter = new StringWriter();
        StreamResult result = new StreamResult(stringWriter);
        getWebServiceTemplate().sendSourceAndReceiveToResult(testsoapurl, source, new WebServiceMessageCallback() {

            @Override
            public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
                try {
                    SoapMessage soapMessage = (SoapMessage) message;

                    //Soap Actio
                    soapMessage.setSoapAction("urn:test/ProcessMessage");

                    // get the header from the SOAP message
                    SoapHeader soapHeader = soapMessage.getSoapHeader();

                    // create the header element
                    ObjectFactory factory = new ObjectFactory();
                    JAXBElement<Message> headerMessage = factory.createMessage(reqHeader);

                    // create a marshaller
                    JAXBContext context = JAXBContext.newInstance(Message.class);
                    Marshaller marshaller = context.createMarshaller();

                    // marshal the headers into the specified result
                    marshaller.marshal(headerMessage, soapHeader.getResult());
                } catch (Exception e) {
                    /////
                }
            }

        }, result);
        return stringWriter.toString();
    }

}
卫博
2023-03-14

解决此问题的最简单方法是使用keytool(Windows上的keytool.exe)将PFX文件转换为JKS:

keytool -importkeystore -srckeystore mypfxfile.pfx -srcstoretype pkcs12 -destkeystore newkeystore.jks -deststoretype JKS

并使用协议。setKeystoreFile()协议。setKeyPass()来加载它。

 类似资料:
  • 问题内容: 如何将Java创建的SSL证书导入Eclipse中的项目? 问题答案: 可能您想在JRE的trustcacerts中导入一个“伪” SSL证书,以避免证书无效的问题。是不是 正如乔恩所说,您可以使用keytool来完成这项工作: 询问时,请使用“ changeit”作为默认密码感谢BrianClozel。确保在服务器或启动配置上使用此运行时。

  • 我正在尝试使用开源用户管理服务,比如KeyClope。构建一个angularJs应用程序,该应用程序将向由KeyClope保护的RESTendpoint发送HTTP请求。我在后端使用spring boot。我能够调用endpoint并得到结果,这应该是不可能的,因为它会阻止来自未经授权来源的请求。 这是我遵循的两个链接 带角和Spring靴的钥匙斗篷 2.Github链接到KeyClope示例 由

  • 假设我编写了两个Java应用程序:和,它们在两个独立的服务器上部署和运行(部署到和部署到

  • 我有所有需要导入apache tomcat的文件,即: 证书申请文件(CSR文件) RSA私钥文件 证书颁发机构发送给我的证书(根证书、中间证书和实体/域) 现在我需要将它们导入我的ApacheTomcat服务器。 我的问题是,鉴于我尚未为我的服务器创建任何密钥存储(CSR和私钥已发送给我,我没有使用keytool等工具创建它们),下一步该怎么做?我需要通过keytool-genkey命令(哪个别

  • 我有一个配置了Spring安全性的Spring启动应用程序。我已将登录请求重定向到我在 python 服务器上运行前端的 。现在,当我尝试将登录名发布到我的 springboot 应用程序时,它不起作用。即使我从邮递员那里尝试,它也说 错误。我怎样才能让它工作。如果我将其作为 html 放在同一个项目中,而不是从 python 服务器或邮递员中,它就可以从 /login 工作。有什么区别。 “消息

  • 我是Java和Spring Boot的初学者,我在Spring Boot上使用< code>Pagination,用这个代码我返回用户列表,如果我想返回页数,我必须这样做? 我知道使用我可以获取页数,但如何返回它?