javascript
java6 disable ssl2.0_SpringBoot2.0如何启用https协议
SpringBoot2.0之后,啟用https協議的方式與1.*時有點兒不同,貼一下代碼。
我的代碼能夠根據配置參數中的condition.http2https,確定是否啟用https協議,如果啟用https協議時,會將所有http協議的訪問,自動轉到https協議上。
一、啟動程序
package com.wallimn.iteye.sp.asset;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
/**
* SpringBoot2.0啟動程序
* @author wallimn,http://wallimn.iteye.com
*
*/
@SpringBootApplication
public class AssetApplication {
public static void main(String[] args) {
SpringApplication.run(AssetApplication.class, args);
}
//如果沒有使用默認值80
@Value("${http.port:80}")
Integer httpPort;
//正常啟用的https端口 如443
@Value("${server.port}")
Integer httpsPort;
// springboot2 寫法
@Bean
@ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
@ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)
public Connector httpConnector() {
System.out.println("啟用http轉https協議,http端口:"+this.httpPort+",https端口:"+this.httpsPort);
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector監聽的http的端口號
connector.setPort(httpPort);
connector.setSecure(false);
//監聽到http的端口號后轉向到的https的端口號
connector.setRedirectPort(httpsPort);
return connector;
}}
二、配置文件
1.使用http協議時的配置
server.port=80
2.使用https及http協議時的配置
server.port=443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=your-password
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=your-cert-alias
condition.http2https=true
http.port=80
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java6 disable ssl2.0_SpringBoot2.0如何启用https协议的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ga tsp matlab,遗传算法(G
- 下一篇: java中的servlet是线程安全的嘛