Hey there, Spring

If you’re a Spring dev like me, you’ve probably found yourself needing to set up HTTPS for your Spring Boot app at some point—whether it’s for a client’s compliance check, handling sensitive user data, or just locking down a production deployment. As someone who’s been part of a Spring vendor team for years, I’ve messed up this setup more times than I can count (remember when I used a self-signed cert that broke every mobile test? Oops), so today I’m breaking down exactly how to do it right—no fluff, no jargon that makes your eyes glaze over.
First, let’s cut to the chase: why bother with HTTPS anyway? Sure, it’s 2024, but I still see devs using plain HTTP in prod because “it’s easier.” Here’s the thing: HTTPS encrypts every bit of data between your server and your users, so no one can snoop on passwords, credit card info, or even their browsing patterns. Browsers also flag unencrypted sites as “Not Secure” these days, which is a huge red flag for your users. Plus, Google uses HTTPS as a minor SEO factor—so it’s a win for security, trust, and even your site’s visibility. For our Spring vendors, it’s non-negotiable when we’re building tools for clients that handle customer data. No exceptions.
Now, let’s get into the how. The good news is Spring Boot makes this way simpler than it used to be in old Spring frameworks. We’re going to use three key parts here: a keystore (that’s where your SSL cert lives), a couple of properties in your application config, and (if you need it) a redirect from HTTP to HTTPS so users don’t accidentally hit the unencrypted version. Let’s walk through each step, no fancy commands you’ll forget later.
Step 1: Get an SSL Keystore. Wait, what’s a keystore? Think of it as a digital safe that holds your SSL certificate and your private key. Spring Boot needs this to prove your app is who it says it is. There are two main types here: self-signed (great for local testing, but don’t use this in prod) and a trusted CA-signed cert (for real users).
For local testing, you can generate a keystore super easily right from your terminal. Most devs use keytool, which comes with the JDK—no extra downloads needed. Here’s the exact command I use every time: keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650. Let me break that down: -alias tomcat just names your cert (I stick with tomcat, no reason to overcomplicate), -storetype PKCS12 is the standard keystore format Spring recognizes, -validity 3650 means it lasts 10 years so you don’t have to regenerate it every few months. When you run this, it’ll ask for a password—make it something you’ll remember, like myapp123 (but not that exact one, obviously) and fill out the name, organization, etc. Don’t stress too much about the details here; for local test it just needs to be valid.
For production, skip the self-signed cert. You’ll need to get a cert from a trusted Certificate Authority—like Let’s Encrypt (free, perfect for small projects) or DigiCert (for enterprise clients). Once you have your cert files, you’ll convert them into a PKCS12 keystore that Spring can use. Most CAs have guides for this, and if you’re a Spring vendor, we usually work with clients to make sure their prod certs are compatible with Spring’s setup. No one wants a client’s production outage because of a bad cert, right?
Step 2: Configure Spring Boot to Use HTTPS. This is the part where 90% of people get it wrong—they forget to point Spring to their keystore or enter the wrong password. Open your application.properties or application.yml file (I use .properties because it’s more straightforward for quick configs, but both work). Add these four lines:
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=your-keystore-password
server.ssl.key-alias=tomcat (or whatever alias you used when generating the keystore)
Wait, why classpath:? Because if you put your keystore.p12 file in your project’s src/main/resources folder, Spring will look there automatically. If you keep it somewhere else, you can use a full file path like file:/opt/keystores/keystore.p12—just make sure the path is correct, or you’ll get a “keystore not found” error that’ll take you 30 minutes to debug (speaking from experience).
Once you add those lines, run your app with mvn spring-boot:run or your IDE’s run button, and check the console. You should see a line like “Tomcat started on port 8443 (https)”—that’s your sign it worked. Now, if you go to https://localhost:8443 in your browser, it should load (for local self-signed cert, you’ll get a “not secure” warning—just tell your browser to proceed, that’s normal for testing). For prod, use port 443, the default HTTPS port, so users don’t have to add :8443 to the URL.
Step 3: Redirect HTTP to HTTPS (Optional, but Useful). If you want to make sure no one ever hits your unencrypted port 8080, add a simple @Bean to your main Spring Boot application class. Here’s the code:
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(createHttpConnector());
return tomcat;
}
private Connector createHttpConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
This tells Tomcat to listen on port 8080 (HTTP) and automatically redirect any request there to port 8443 (HTTPS). Super handy for users who accidentally type your domain without https://. Pro tip: If you’re using a reverse proxy like Nginx in front of your Spring app, you can handle the redirect there instead, which is even better for performance—but this works great for basic setups.
Wait, let’s talk about common mistakes I see Spring devs make all the time. First, mismatched ports: if your HTTPS is on 8443 but your redirect is set to 443, it’ll break. Double-check your port numbers. Second, wrong keystore path: I’ve had projects where I put the keystore in src/main/java instead of src/main/resources, and Spring couldn’t find it. Rule of thumb: if using classpath, put it in resources. Third, using a self-signed cert in prod: that’s a big no-no. Browsers will flag it, and some APIs won’t work with self-signed certs. For prod, always get a CA-signed cert. Fourth, forgetting to update your firewall: make sure port 8443 (or 443) is open, and port 8080 is closed or only accessible locally. No one wants their app exposed on the internet on port 8080.
Now, as a Spring vendor, I’ll be honest with you: we get a lot of inquiries from clients who messed up their HTTPS setup and need help fixing it. It’s one of those things that looks easy until you’re staring at a “SSL handshake failed” error at 2 a.m. If you’re building Spring-based tools for your team or your clients, getting HTTPS right from the start saves you hours of headaches later. We’ve helped dozens of customers fix misconfigured HTTPS setups, and we know exactly how to streamline the process so it works every time.
If you’re looking to integrate secure, production-ready HTTPS into your Spring Boot apps—whether for your own platform or to offer as part of your client solutions—reach out to our team to discuss how we can tailor a solution that fits your needs. We work with teams of all sizes, from startups to enterprise companies, to make Spring deployment and security as smooth as possible.
Lastly, a quick note on testing: after you set up HTTPS, test it with tools like SSL Labs’ Server Test (it’s free, no account needed) to make sure your cert is valid and your setup is secure. That’ll catch any weak spots, like old encryption protocols or expired certs, before you launch.

Don’t let HTTPS be a afterthought. It’s the first line of defense for your app and your users. For anyone who’s ever spent hours debugging a broken HTTPS setup in Spring, trust me: following these simple steps will save you way more time than it takes to set up.
Locking Quick Release Pin References:
Spring Boot Documentation, HTTPS
Let’s Encrypt: Getting Started with HTTPS
Keytool Command Line Documentation
SSL Labs Server Test Guide
Huizhou Zeyao Hardware Products Co., Ltd.
Huizhou Zeyao Hardware Products Co., Ltd. is one of the most professional spring manufacturers and suppliers in China, specialized in providing high quality products and service. We warmly welcome you to wholesale customized spring made in China here from our factory.
Address: 1/F, Building A, Huifeng West Third Road No. 108, Chenjiang St., Zhongkai High-Tech District,Huizhou, Guangdong, China
E-mail: sales@hky-hardware.com
WebSite: https://www.zeyaohardware.com/