spring-boot - 如何让我的 Spring Boot 微服务使用 HTTPS 运行?

标签 spring-boot ssl tomcat https configuration

我有一个用 Spring Boot 实现的微服务(最初是从 2.0.6 现在是 2.1.8),它使用端口 8080 运行良好。 现在我必须切换到 TLS,让 (REST) 服务作为使用 HTTPS 的 webhook 工作,但我总是得到

2020-01-02 17:14:38.929 DEBUG [-,,,] 19072 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : Application failed to start due to an exception

org.springframework.boot.web.embedded.tomcat.ConnectorStartFailedException: Connector configured to listen on port 8444 failed to start
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.checkConnectorHasStarted(TomcatWebServer.java:228)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.checkThatConnectorsHaveStarted(TomcatWebServer.java:220)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:200)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:297)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
    at de.mediciliving.cloud.crm.CrmServiceApplication.main(CrmServiceApplication.java:21)

2020-01-02 17:14:38.930 ERROR [-,,,] 19072 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 8444 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 8444, or configure this application to listen on another port.

我关注了HTTPS using Self-Signed Certificate in Spring Boot使用以下方法创建 keystore :

keytool -genkeypair -alias MyAlias -keystore keystore.p12 -storetype PKCS12 -keyalg RSA -storepass xxxxxx -validity 730 -keysize 2048

在我的 application.properties我这样定义:

server.ssl.enabled=true
server.port=${APPLICATION_PORT:8443}
# The path to the keystore containing the certificate
server.ssl.key-store=classpath:keystore/keystore.p12
# The password used to generate the certificate
server.ssl.key-store-password=xxxxxx
#server.ssl.key-password=xxxxxx
# The format used for the keystore.
#server.ssl.key-store-type=JKS
#server.ssl.key-store-type=PKCS12

server.ssl.key-alias=MyAlias

我没有其他服务在该端口上运行监听,我什至重新启动了 IntelliJ 以确保没有运行/监听服务。

我已经在网上搜索了几天,但仍然无法使用 HTTPS 运行我的服务。错误消息告诉我没有任何帮助。由于没有其他正在运行的服务,它一定是配置,但我不知道究竟是什么导致了这个错误。

在我的 POM 中:

<spring.boot.version>2.1.8.RELEASE</spring.boot.version>
<tomcat.version>9.0.27</tomcat.version>

<dependency>
    <groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

...I found something to exclude binary resource files from filtering, so I considered it in the POM as well...
<excludes>
    <exclude>**/resources/**/*.p12</exclude>
</excludes>

我尝试了几个端口,如 443、8443、8082 等,还使用了不同的证书格式(PKCS12 和 JKS),但均未成功。

当我做 keytool.exe -keystore keystore.p12 -storepass xxxxxx -list -storetype pkcs12

我得到:

Keystore type: PKCS12
Keystore provider: SUN

Your keystore contains 1 entry

MyAlias, 02.01.2020, PrivateKeyEntry,
Certificate fingerprint (SHA-256): 50:F3:52:68:4D:13:D9:C7:72:8E:9F:E9:60:40:DB:88:4D:1F:E8:75:2B:0A:08:C5:E2:F5:FA:D0:D7:0B:73:EB

我的服务中有这个配置,也许对你有帮助:

@Configuration
@EnableResourceServer
@EnableWebSecurity
@EnableOAuth2Sso
public class SecurityConfig extends /*WebSecurityConfigurerAdapter*/ ResourceServerConfigurerAdapter {

    private static final String[] WHITELIST = {
            "/swagger-resources/**",
            "/swagger-ui.html",
            "/v2/api-docs",
            "/webjars/**",
            "/error*",
            "/health"
    };

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(WHITELIST).permitAll();
//        http.authorizeRequests().anyRequest().fullyAuthenticated();
        http.authorizeRequests().anyRequest().permitAll();

        // disables Cross-Site Request Forgery protection and CORS protection
        http.csrf().disable();
        http.cors();
    }

    @Bean
    RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }

    @Bean
    public FilterRegistrationBean simpleCorsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }
}

最佳答案

Considering that you have added the JKS to your Java cacerts in your system.Below are the steps using JKS for enabling https. 

Step 1 : (Changes to application.properties)
server.port: 443
server.ssl.key-store: jks_file_path
server.ssl.key-store-password: ********
server.ssl.keyStoreType: JKS


Step 2 : Go to your run /debug configuration and use below in VM options.

-Djavax.net.ssl.trustStore= cacert_file_path
-Djavax.net.ssl.trustStorePassword=changeit
-Djavax.net.debug=ssl:handshake
-Dspring.profiles.active=dev (optional if you have profiling enabled)

    enter code here

Step 3 : Start application. 

Hope it will work.

关于spring-boot - 如何让我的 Spring Boot 微服务使用 HTTPS 运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566705/

相关文章:

java - java中JSONObject内部的JSONobject

java - 预期 SQL 错误时出现 UnsupportedOperationException

php - PHP 开发人员需要了解哪些有关 https/安全套接字层连接的知识?

java - 如何创建用于 Apache Tomcat 的 X509 自签名证书

PHP 数组 POST 数据的 Java 等价物

java - 创建名称为 ‘client’ 的 bean 时出错

spring - Spring Boot中的ElasticSearch凭证

docker - Dockerize Wildfly和SSH

php - 如何创建用于在 SSL 下调用 WCF Web 服务的 PHP SOAP 客户端?

java - 在 Ubuntu 18.04 上安装 Tomcat 9.0.16