gpt4 book ai didi

ssl - Spring Boot 1.2.1 中带有 SSL 的 Jetty

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:38 32 4
gpt4 key购买 nike

我应该如何在 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(父POM中的Spring Boot版本是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>

最佳答案

您尝试使用的类特定于 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);
}
});
}
};
}

关于ssl - Spring Boot 1.2.1 中带有 SSL 的 Jetty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28320112/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com