Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to support HTTPS in Spring Boot

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to support HTTPS in Spring Boot. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Support for HTTPS

Spring Boot is easy to configure SSL, just through a series of server.ssl.* parameters, as shown below.

Application.properties profile reference configuration:

Server.port=8443server.ssl.protocol=TLSserver.ssl.key-store=classpath:javastack.keystoreserver.ssl.key-store-password=javastackserver.ssl.key-store-type=JKS

How to create a certificate in a local test, please refer to the article "turn on Tomcat https support in one minute" on the Wechat official account of the Java technology stack and copy the generated certificate to the resources directory in the Spring Boot project.

This is just a demonstration of SSL one-way verification, and more SSL parameters are configured as follows.

Server.ssl.ciphers= # Supported SSL ciphers.server.ssl.client-auth= # Whether client authentication is wanted ("want") or needed ("need") Requires a trust store.server.ssl.enabled= # Enable SSL support.server.ssl.enabled-protocols= # Enabled SSL protocols.server.ssl.key-alias= # Alias that identifies the key in the key store.server.ssl.key-password= # Password used to access the key in the key store.server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file). Server.ssl.key-store-password= # Password used to access the key store.server.ssl.key-store-provider= # Provider for the Key store.server.ssl.key-store-type= # Type of the key store.server.ssl.protocol=TLS # SSL protocol to use.server.ssl.trust-store= # Trust store that holds SSL certificates.server.ssl.trust-store-password= # Password used to access the trust store.server.ssl.trust-store-provider= # Provider for the trust store.server.ssl.trust-store-type= # Type of the trust store.

Class corresponding to the parameter: org.springframework.boot.web.server.Ssl

After the above example is configured, HTTPS can be enabled, the default HTTP protocol is no longer supported, and Spring Boot does not support both HTTP and HTTPS as configuration files.

How to support it at the same time?

If you need to support both HTTP and HTTPS, you need to configure the other protocol programmatically. Because it is easier to configure HTTP protocol programmatically, the recommended practice for Spring Boot is to configure HTTPS in the configuration file and HTTP through the program.

The following example is to programmatically support the HTTP protocol.

@ SpringBootApplicationpublic class JavastackApplication {@ Bean public ServletWebServerFactory servletContainer () {TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory (); tomcat.addAdditionalTomcatConnectors (createStandardConnector ()); return tomcat;} private Connector createStandardConnector () {Connector connector = new Connector ("org.apache.coyote.http11.Http11NioProtocol"); connector.setPort (8080) Return connector;} public static void main (String [] args) {SpringApplication.run (JavastackApplication.class, args);}}

After starting Spring Boot, you will see that both protocol logs are supported at the same time.

Tomcat started on port (s): 8443 (https) 8080 (http) with context path'/ 'the above is how to support HTTPS in Spring Boot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report