我应该如何在 Spring Boot 1.2.x 中使用 SSL 连接器配置 Jetty?
以下配置适用于 Spring boot 1.1.6,但在 1.2.1 版本中出现“SslSocketConnector 无法解析为类型”错误。
@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>