I have create sample jHipster app. Now I want to add self signed SSL certificate and test in local to have a access to https. How to achieve this?
In IIS Manager, do the following to create a self-signed certificate: In the Connections pane, select your server in the tree view and double-click Server Certificates. In the Actions pane, click Create Self-Signed Certificate. Enter a user-friendly name for the new certificate and click OK.
To make keystore information available, you must create a keystore, then export and import a self-signed certificate. Create a keystore using the keytool command in the Netcool Configuration Manager Java installation. You must provide domain identity information, for example, your name, company, and country.
Open Chrome settings, scroll to the bottom, and click Show advanced settings... Click the Trusted Root Certification Authorities tab, then click the Import... button. This opens the Certificate Import Wizard.
These instructions are applicable for all Spring Boot applications, on which JHipster is based. I have tested this on a newly generated JHipster 2.7 project.
You need to complete these steps when starting from scratch:
First you need to generate your self-signed certificate in your project directory, this can be done with keytool
, which is utility script provided by Java:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650 Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: What is the name of your organizational unit? [Unknown]: What is the name of your organization? [Unknown]: What is the name of your City or Locality? [Unknown]: What is the name of your State or Province? [Unknown]: What is the two-letter country code for this unit? [Unknown]: Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct? [no]: yes
I have chosen password mypassword
so this is the one I will use in the next step. When you have done this, you will see a keystore.p12
in your current directory.
application.properties
or application.yml
as mentioned in the Spring Boot documentation Now you need to add the HTTPS connector properties for Tomcat. You can find the property (yml) files in src/main/resources/
and you need to update the application.yml
(or if it is only for development in application-dev.yml
with the following properties:
server: ssl: key-store: keystore.p12 key-store-password: mypassword keyStoreType: PKCS12 keyAlias: tomcat
Now you can package your application with Maven (or Gradle if you chose that for your JHipster application) using mvn clean package
and run the application using mvn spring-boot:run. You can now access your application on https://localhost:8080
For simplicity I did not change the port, but ideally you should change it as well in the properties files, but I left it out since they are already defined in application-dev.yml
and application-prod.yml
so you would have to change it in there or remove it and put it in the general application.yml
You can only enable one protocol through the application.properties
, so when you do this like above only HTTPS will work. If you want HTTP to work too, and redirect to HTTPS you have to add a @Configuration
class like below
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @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(initiateHttpConnector()); return tomcat; } private Connector initiateHttpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(8080); connector.setSecure(false); connector.setRedirectPort(8443); return connector; }
This response is basically a copy of my blog post on the same subject: http://www.drissamri.be/blog/java/enable-https-in-spring-boot/
To extend the Driss Amri brilliant answer on how to re-enable BrowserSync
.
If you choose not to support http, or if http is redirected to https, BrowserSync
will not work. To make it work again, few changes are necessary in:
gulp/config.js, apiPort
and uri
to:
apiPort: 8443, uri: 'https://localhost:',
gulp/serve.js: add options.rejectUnauthorized = false;
into proxyRoutes
so that node does not complain about self signed certificate:
proxyRoutes.map(function (r) { var options = url.parse(baseUri + r); options.route = r; options.preserveHost = true; options.rejectUnauthorized = false; return proxy(options); }));
optionally let BrowserSync
serve content over https too. I recommend it with Spring Social
to save some trouble. Just add https: true
into browserSync
call in gulp/serve.js:
browserSync({ open: true, port: config.port, server: { baseDir: config.app, middleware: proxies }, https: true });
Now BrowserSync will serve content with self signed certificate shipped with it. It is possible to reuse the one created for Spring Boot
, more on BrowserSync homepage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With