springboot忽略证书_SpringBoot 配置加密证书

本文介绍如何在SpringBoot应用中配置HTTPS加密证书,并实现HTTP到HTTPS的自动跳转。同时,展示了如何在特定场景下忽略证书验证,确保服务正常运行。
摘要由CSDN通过智能技术生成

packageorg.xftm.app;importorg.apache.catalina.Context;importorg.apache.catalina.connector.Connector;importorg.apache.tomcat.util.descriptor.web.SecurityCollection;importorg.apache.tomcat.util.descriptor.web.SecurityConstraint;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.Banner;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.embedded.EmbeddedServletContainerFactory;importorg.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;importorg.springframework.boot.web.client.RestTemplateBuilder;importorg.springframework.context.annotation.Bean;importorg.springframework.web.client.RestTemplate;importspringfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication

@EnableSwagger2//Swaggerapi集成,入不需要可直接删除public classApplication {

@Value("${server.httpPort}")privateString httpPort;//在yml文件中自定义的http端口

@Value("${server.httpsPort}")privateString httpsPort; //在yml文件中自定义https端口

@Autowired

RestTemplateBuilder restTemplateBuilider;public static voidmain(String[] args) {

SpringApplication application= new SpringApplication(Application.class);

application.setBannerMode(Banner.Mode.CONSOLE);//笔者自定义的banner 此处可按照Springboot默认启动方式启动

application.run(args);

}

@BeanpublicEmbeddedServletContainerFactory servletContainer() {

TomcatEmbeddedServletContainerFactory tomcat= newTomcatEmbeddedServletContainerFactory() {

@Overrideprotected voidpostProcessContext(Context context) {

SecurityConstraint constraint= newSecurityConstraint();

constraint.setUserConstraint("CONFIDENTIAL");

SecurityCollection collection= newSecurityCollection();

collection.addPattern("/*");

constraint.addCollection(collection);

context.addConstraint(constraint);

}

};

tomcat.addAdditionalTomcatConnectors(httpConnector());returntomcat;

}

@BeanpublicConnector httpConnector() {

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

connector.setScheme("http");

System.out.println("httpport="+httpPort);

System.out.println("httpsPort="+httpsPort);//Connector监听的http的端口号

connector.setPort(httpPort ==null?8080:Integer.valueOf(httpPort));

connector.setSecure(false);//监听到http的端口号后转向到的https的端口号

connector.setRedirectPort(httpsPort ==null?443:Integer.valueOf(httpPort));returnconnector;

}

@BeanpublicRestTemplate restTemplate() {//使用build()方法进行获取

returnrestTemplateBuilider.build();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值