springboot项目https访问配置

编程

生成https访问的ssl证书
在jdkin目录下,使用keytool.exe生成ssl签名证书
打开命令窗口,输入:keytool -genkey -keyalg RSA  -alias ssl_alias -keystore C:/Users/Liunh/.ssh/ssl.keystore
指定的签名证书名字叫ssl.keystore,别名叫ssl_alias,如下图

application.properties https访问方式配置

server.ssl.enabled=true

server.ssl.key-store=classpath:ssl.keystore

server.ssl.key-store-password=0123789

server.ssl.key-store-type=JKS

server.ssl.key-alias=ssl_alias

使用代码配置将http访问自动跳转为https方式访问

@Configuration

public class TomcatConfiguration {

@Value("${server.port:8080}")

private int port;

@Value("${server.servlet.context-path:/*}")

private String path;

@Bean

public ServletWebServerFactory servletContainer() {

TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(){

protected void postProcessContext(Context context) {

SecurityConstraint securityConstraint = new SecurityConstraint();

securityConstraint.setUserConstraint("CONFIDENTIAL");

SecurityCollection collection = new SecurityCollection();

collection.addPattern(path);

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(80);

connector.setSecure(false);

connector.setRedirectPort(443);

return connector;

}

}

源码:https://gitee.com/lion123/springboot-tomcat-demo

 

以上是 springboot项目https访问配置 的全部内容, 来源链接: utcz.com/z/514241.html

回到顶部