用Spring Boot在https上启动Web应用
当你想要在本地开发环境中确认https的运行情况时,需要进行如下设置。
LibraryVersionJava1.8.0_25Spring-Boot1.2.0
创建密钥库
keytool -genkey -alias tomcat -keyalg RSA
生成一个.keystore文件。
在application.yml中添加SSL启动配置
server:
port: 8443
ssl:
key-store: "path/to/.keystore"
key-store-password: yourpass
key-password: yourpass
建筑
gradle build
启动
java -jar build/libs/sample-0.0.1-SNAPSHOT.jar

请提供更多上下文。 “参考” 在中文中有多个意思,可以是指参考资料、参考人或事物、参考意见等等。
以下是用中文本地化的表述。只需要一个选项:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-ssl
过去的Spring Boot版本备注
春季启动版的版本在过去的版本(1.0系列?)中似乎需要这样的设置。
在Application.java中添加SSL启动配置。
package jp.sample;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ComponentScan
@EnableAutoConfiguration
@RestController
public class Application {
@RequestMapping("/")
String hello() {
return "Hello";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
@Profile("production")
EmbeddedServletContainerCustomizer containerCustomizer(
@Value("${keystore.file}") Resource keystoreFile,
@Value("${keystore.pass}") String keystorePass) throws Exception {
String absoluteKeystoreFile = keystoreFile.getFile().getAbsolutePath();
return (ConfigurableEmbeddedServletContainer container) -> {
TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
tomcat.addConnectorCustomizers(
(connector) -> {
connector.setPort(8443);
connector.setSecure(true);
connector.setScheme("https");
Http11NioProtocol proto = (Http11NioProtocol) connector.getProtocolHandler();
proto.setSSLEnabled(true);
proto.setKeystoreFile(absoluteKeystoreFile);
proto.setKeystorePass(keystorePass);
// proto.setKeystoreType("PKCS12");
proto.setKeystoreType("JKS");
proto.setKeyAlias("tomcat");
}
);
};
}
}
建筑
gradle build
开机
java -Dspring.profiles.active=production -Dkeystore.file=file:/path/to/.keystore -Dkeystore.pass=yourKeystorePass -jar build/libs/sample-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.2.0.BUILD-SNAPSHOT)
・・中略・・
2014-12-12 13:36:57.550 INFO 27289 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-12-12 13:36:57.880 INFO 27289 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8443/https
2014-12-12 13:36:57.882 INFO 27289 --- [ main] jp.sample.Application : Started Application in 5.552 seconds (JVM running for 6.139)