【问题标题】:Spring Boot + Jetty + TLS1.2 (HTTPS)Spring Boot + 码头 + TLS1.2 (HTTPS)
【发布时间】:2017-12-21 21:44:43
【问题描述】:

我有一个使用 Spring Boot 和 Jetty 在本地运行的应用程序。当我尝试将 TLS1.2 添加到我的项目以支持 HTTPS 时,问题就出现了。我可以完美地使用 http,但现在我希望使用 TLS1.2 在 https 中发布服务。这是我失败并出现错误的时候。

我收到以下错误:

Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jettyEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedJetty.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverProperties': Could not bind properties; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'ssl[keyAlias]' of bean class [org.springframework.boot.autoconfigure.web.ServerProperties]: Cannot access indexed value in property referenced in indexed property path 'ssl[keyAlias]'; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'ssl[keyAlias]' of bean class [org.springframework.boot.autoconfigure.web.ServerProperties]: Bean property 'ssl[keyAlias]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:135)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:130)
at com.viajesurbis.Application.main(Application.java:21)
Caused by: org.springframework.beans.factory.BeanCreationException

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>springboot-jersey</groupId>
<artifactId>springboot-jersey</artifactId>
<version>1.0-SNAPSHOT</version>

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <jersey.version>2.8</jersey.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-inmemory</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>1.5.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.skyscreamer</groupId>
        <artifactId>jsonassert</artifactId>
        <version>1.2.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>

    </plugins>
</build>

这是我开始我的应用程序的主要地方:

        package com.viajesurbis;

import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.viajesurbis.config.JerseyConfig;
import com.viajesurbis.rest.RestResource;

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = { RestResource.class })
public class Application {

    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(Application.class).showBanner(false).run(args);
    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
        return registration;
    }

}

这是我的 application.properties 文件:(该文件位于我项目的 src/main/resources 路径中)

server.port: 8443
server.ssl.key-store: keystore.p12
server.ssl.key-store-password: #REMOVED#
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: jetty

我在路径 src/main/resources 中也有一个名为 keystore.p12 的文件,以防万一我也将 keytrore.p12 放在 pom.xml 所在的项目中。也就是说,我复制了文件,但它仍然不起作用。

如果我删除 application.properties 的内容,那么该应用程序对我来说非常适合,但在 HTTP 中,我希望它在 HTTPS 中工作。

非常感谢您的帮助。 亲切的问候,

【问题讨论】:

    标签: java rest spring-boot https jetty


    【解决方案1】:

    Ssl 配置支持仅添加到 1.1.7 版本:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.1.7.RELEASE</version>
    </dependency>
    

    我刚刚将 spring-boot-starter-parent 版本升级到 1.1.0.RELEASE 并且它摆脱了错误:

    尝试在你的 pom 中使用这个父对象:

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

    【讨论】:

    • 您好 tsolakp,非常感谢您的帮助,但我收到一个错误:线程“主”org.springframework.context.ApplicationContextException 中的异常:无法启动嵌入式容器;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedJetty.class] 中定义名称为 'jettyEmbeddedServletContainerFactory' 创建 bean 时出错:您可以将此添加到我的pom.xml,你能告诉我这个文件的外观吗?
    • 拜托,任何用户都可以帮助我吗?谢谢。
    • @Javi。我不确定你是否还在为这个问题而苦苦挣扎。如果您确实尝试我对您问题的更新答案。
    • 你好@tsolakp,非常感谢你的帮助,但我继续出错,这仍然不起作用。请问,你能用我所有的文件和java类创建一个新项目吗?这样你就会看到我遇到的错误。
    • 我就是这么做的。复制您的 pom 和应用程序代码并构建/运行应用程序,没有任何错误。
    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多