Springboot支持 https和http请求

    技术2023-07-15  114

    一 首先去下载证书

    我自己用阿里云的免费的一年

    我下载的是 tomcat那个,下载完成有两个文件 一个是证书 ,一个是密码,之后将证书放在 resource下面 之后就是 配置 yml了 最后 在 启动类中配置

    /** * @return */ @Bean public ServletWebServerFactory servletContainer() { 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(DEFAULT_PROTOCOL);//没加这句话时,请求get会报错,在网上找了下 加上这句话就好了 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监听的http的端口号 connector.setPort(8088); connector.setSecure(false); //监听到https的端口号 //connector.setRedirectPort(443); return connector; }

    启动:

    测试: https 443端口

    http 8088端口:

    Processed: 0.008, SQL: 9