【问题标题】:Spring boot by default HTTPS but test profile with HTTPSpring Boot 默认使用 HTTPS,但使用 HTTP 测试配置文件
【发布时间】:2020-11-10 19:49:43
【问题描述】:

是否可以定义默认的properties 文件来启用https 例如:

server.port: 8443

# SSL Configuration
server.ssl.key-store:/etc/pki/certificate.p12
server.ssl.key-store-password: ${P12PASS}
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: ${KEYALIAS}
server.ssl.trust-store=/etc/pki/trust.jks
server.ssl.trust-store-password=${TRUSTPASS}

但是在8080 端口上使用@Bean 定义@Bean 以某种方式执行@Configuration 而没有任何密钥库信息?

我想使用rest-assured 库准备一些集成测试,但是当测试加载我的默认应用程序上下文时,我收到与未设置变量相关的错误,例如P12PASSTRUSTPASS。我希望集成测试在没有 https 配置的情况下运行应用程序上下文。

有什么建议吗?

【问题讨论】:

标签: java spring spring-boot unit-testing integration-testing


【解决方案1】:

另一种方法是为您的应用创建双端口。一种是用于 Http。另一个是用于https。所以不会影响你的测试。

application.yml

server:
  port: 8080

https:
  port: 8443
  key-store: /etc/pki/certificate.p12
  key-store-password: ${P12PASS}

应用

import java.nio.charset.Charset;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    private final static Logger logger = LoggerFactory.getLogger(Application.class) ; 

    @Value("${https.port}")
    int port;
    
    @Value("${https.key-store}")
    String storeFile;
    
    @Value("${https.key-store-password}")
    String password;
    
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }

    @Bean
    public ServletWebServerFactory  servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory ();
        tomcat.setUriEncoding(Charset.forName("UTF-8"));
        tomcat.addAdditionalTomcatConnectors(createSslConnector());
        return tomcat;
    }

    public Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

        connector.setPort(port);
        connector.setScheme("https");
        connector.setSecure(true);
        protocol.setSSLEnabled(true);
        protocol.setClientAuth("false");
        protocol.setSSLProtocol("TLSv1+TLSv1.1+TLSv1.2");
        protocol.setCiphers("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA,SSL_RSA_WITH_RC4_128_SHA");
        protocol.setKeystoreFile(storeFile);
        protocol.setKeystorePass(password);


        return connector;
    }
    
}

【讨论】:

    猜你喜欢
    • 2017-02-03
    • 2023-04-10
    • 2019-02-20
    • 2019-01-05
    • 2021-07-03
    • 1970-01-01
    • 2020-02-13
    • 2019-01-20
    • 1970-01-01
    相关资源
    最近更新 更多