在 Spring Boot 1.2.1 中使用 SSL 的 Jetty

Jetty with SSL in Spring Boot 1.2.1

我应该如何在 Spring Boot 1.2.x 中使用 SSL 连接器配置 Jetty?

以下配置适用于 Spring boot 1.1.6,但对于版本 1.2.1 会出现 'SslSocketConnector cannot be resolved to a type' 错误。

@Configuration
@EnableAutoConfiguration
public class OptosoftOAuthSecurityApplication implements
        EmbeddedServletContainerCustomizer {

    public static void main(String[] args) {
        SpringApplication.run(OptosoftOAuthSecurityApplication.class, args);
    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if (container instanceof JettyEmbeddedServletContainerFactory) {
            customizeJetty((JettyEmbeddedServletContainerFactory) container);
        }
    }

    public void customizeJetty(
            JettyEmbeddedServletContainerFactory containerFactory) {
        containerFactory.addServerCustomizers(jettyServerCustomizer());
    }

    @Bean
    public JettyServerCustomizer jettyServerCustomizer() {
        return new JettyServerCustomizer() {

            @Override
            public void customize(Server server) {
                SslContextFactory sslContextFactory = new SslContextFactory();
                sslContextFactory.setKeyStorePassword("jetty6");
                try {
                    sslContextFactory.setKeyStorePath(ResourceUtils.getFile(
                            "classpath:jetty-ssl.keystore").getAbsolutePath());
                } catch (FileNotFoundException ex) {
                    throw new IllegalStateException("Could not load keystore",
                            ex);
                }

                // THIS CLASS cannot be resolved !!!!!!  
                SslSocketConnector sslConnector = new SslSocketConnector(
                        sslContextFactory);
                sslConnector.setPort(9443);
                sslConnector.setMaxIdleTime(60000);
                server.addConnector(sslConnector);
            }
        };
    }

    @Bean
    @ConditionalOnMissingBean(RequestContextListener.class)
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
}

我的pom.xml(Spring父POM中的引导版本是1.2.1):-

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>
    </dependencies>

您尝试使用的 class 特定于 Jetty 8,但 Spring Boot 1.2.x 默认使用 Jetty 9。在 Spring Boot 1.2 中,您现在可以在 application.properties 中以声明方式配置 SSL。 spring-boot-sample-jetty-ssl.

中对此进行了说明

您可以使用 application.properties 中声明的以下属性替换 EmbeddedServletContainerCustomizer 实现:

server.port = 9443
server.ssl.key-store = classpath:jetty-ssl.keystore
server.ssl.key-store-password = jetty6

Spring Boot 仅支持单个连接器的声明式配置。如果您希望可以通过 HTTP 和 HTTPS 访问服务器,那么您必须以编程方式配置其中之一。我建议以编程方式配置 HTTP 连接器,因为涉及的代码较少:

@Bean
public EmbeddedServletContainerCustomizer servletContainerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {

        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof JettyEmbeddedServletContainerFactory) {
                configureJetty((JettyEmbeddedServletContainerFactory) container);
            }
        }

        private void configureJetty(JettyEmbeddedServletContainerFactory jettyFactory) {
            jettyFactory.addServerCustomizers(new JettyServerCustomizer() {

                @Override
                public void customize(Server server) {
                    ServerConnector serverConnector = new ServerConnector(server);
                    serverConnector.setPort(8080);
                    server.addConnector(serverConnector);
                }
            });
        }
    };
}