1. 程式人生 > >spring boot接口 支持https

spring boot接口 支持https

sets boot span -a int roc div alias oid

1.擁有證書,可自己生成測試用javatool生成

keytool -keystore [keyname].jks -genkey -alias tomcat -keyalg RSA

接下來輸入相關信息即可

2.把證書添加到項目中/src/main/resources/目錄下

3.增加配置

server.port= 8443
server.ssl.key-store= classpath:mykeys.jks
server.ssl.key-store-password= yourpassword
server.ssl.key-password= yourpassword

4.配置用戶訪問http自動跳轉到https(http與https均可訪問)

@Configuration
public class HttpsConfiguration {

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint 
= new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat; } @Bean public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(8080);// 表示用8080端口來供http訪問 connector.setSecure(false); connector.setRedirectPort(8443);// 自動重定向到8443端口 return connector; } }

spring boot接口 支持https