如何在Spring Boot 1.2.x中使用SSL连接器配置Jetty?
以下配置适用于Spring boot 1.1.6,但是'SslSocketConnector无法解析为版本1.2.1的类型'错误。
@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>
答案 0 :(得分:8)
您尝试使用的类特定于Jetty 8,但Spring Boot 1.2.x默认使用Jetty 9。在Spring Boot 1.2中,您现在可以在application.properties
中以声明方式配置SSL。这在spring-boot-sample-jetty-ssl
中有说明。
您可以使用EmbeddedServletContainerCustomizer
中声明的以下属性替换application.properties
实施:
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);
}
});
}
};
}