DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Enable HTTP/HTTPS on Spring Boot [Snippet]

How to Enable HTTP/HTTPS on Spring Boot [Snippet]

Learn how to enable HTTP and HTTPS on your Spring Boot applications with this tutorial on using the SSL layer for application configuration.

By 
Ganesh Sahu user avatar
Ganesh Sahu
·
Updated Aug. 07, 18 · Code Snippet
Likes (7)
Comment
Save
Tweet
Share
65.3K Views

Join the DZone community and get the full member experience.

Join For Free

You can configure Spring Boot services to be accessed over the SSL layer. And, the configuration can be done in the application configuration YML or property file. If there is ever a need to support the services so that they can be accessed in both the HTTP and HTTPS layer, you will have to plug in an additional connector.

By default, it allows only one connector to use the properties. The key to supporting both connectors would require some customization.

Let's take a look at the following interface:

 org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer.

This is the strategy interface for customizing auto-configured embedded servlet containers.
Any beans of this type will get a callback with the container factory before the container itself is started, so you can set the port, address, error pages, etc.

  • Create a bean that returns an instance of EmbeddedServletContainerCustomizer, overriding the customize method. Inside the customize method, we need to add additional support for the connectors. 

@Configuration
public class EmbeddedTomcatConfig {

    @Value("${http.port}")
    private int httpPort;

    @Bean
    public EmbeddedServletContainerCustomizer customizeTomcatConnector() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    TomcatEmbeddedServletContainerFactory containerFactory =
                            (TomcatEmbeddedServletContainerFactory) container;

                    Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                    connector.setPort(httpPort);
                    containerFactory.addAdditionalTomcatConnectors(connector);
                }
            }
        };
    }

}


  • Next, we need to establish the application.properties:

http.port=8081
server.port=8082
server.ssl.key-password=****yourjkspassword***
server.ssl.key-store=classpath:applicationssl.jks
server.ssl.key-store-type=JKS


Now, your application should support both HTTP (8081) and HTTPS (8082).

The JKS should be placed in the application classpath, for example, the /src/resources  folder.

You can create self-signed certificates using the key-tool for testing purposes.

Please refer to how to convert SSL certificates to JKS here. 

I have shared the configuration for a Tomcat-embedded container. This code can always be tweaked for Jetty. 

Happy coding!

Spring Framework Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!