玖叶教程网

前端编程开发入门

springboot2配置https和http同时访问

一、配置ssl(.keystore):

application.yml中配置证书

server:

port: 8881 //端口

ssl:

enabled: true

key-store: classpath:crt/codewang.keystore //证书路径

key-alias: codewang //证书别名

key-password: codewang //别名密码

key-store-type: jks

二、配置ssl(.p12):

application.yml中配置证书

server:

port: 8881 //端口

ssl:

enabled: true

key-store: classpath:crt/server.p12

key-store-password: 123456

key-store-type: PKCS12

三、重写Tomcat配置:

@Bean

public Connector connector() {

Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

connector.setScheme("http");

connector.setPort(8888);// http访问的端口

connector.setSecure(false);

connector.setRedirectPort(8881); //https访问的端口,在配置文件中配置的server.port

return connector;

}

@Bean

public TomcatServletWebServerFactory tomcatServletWebServerFactory() {

TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {

@Override

protected void postProcessContext(Context context) {

SecurityConstraint securityConstraint = new SecurityConstraint();

securityConstraint.setUserConstraint("CONFIDENTIAL");

SecurityCollection collection = new SecurityCollection();

collection.addPattern("/*");

collection.addMethod("*"); //所有方法都重定向

securityConstraint.addCollection(collection);

context.addConstraint(securityConstraint);

}

};

tomcat.addAdditionalTomcatConnectors(connector());

return tomcat;

}

四、springboot2.0以下版本:

只需将2.0中的TomcatServletWebServerFactory改为TomcatEmbeddedServletContainerFactory即可

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言