Skip to content

Commit f7d62e9

Browse files
authored
Merge pull request #40 from devondragon:issue-36-Enhance-the-devtools-live-reload-HTTP-vs-HTTPS-port-change-to-be-configuration-driven
Enhance the devtools live reload HTTP vs HTTPS port change to be configuration driven
2 parents 1933d1b + 994a1c9 commit f7d62e9

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,33 @@ The framework provides support for the following features:
3030

3131
## How To Get Started
3232

33+
### Configuring Your Local Environment
34+
There is an example configuration file in /src/main/resources called application-local.yml-example. By default this project's gradle bootRun command runs Spring using the "local" profile. So you can just copy that file to application-local.yml and replace the values (keys, URLs, etc..) with your values. If you are using a different profile to run (such as default) you will just need to ensure the same configs are in place in your active configuration file(s).
35+
36+
Missing or incorrect configuration values will make this framework not work correctly.
37+
3338
### Database
3439
This framework uses a database as a user store. By buildling on top of Spring JPA it is easy to use which ever datastore you like. The example configuration in application.yml is for a [MariaDB](https://mariadb.com) 10.5 database. You will need to create a user and a database and configure the database name, username, and password.
3540

3641
You can do this using docker with a command like this:
3742

43+
```
3844
docker run -p 127.0.0.1:3306:3306 --name springuserframework -e MARIADB_ROOT_PASSWORD=springuserroot -e MARIADB_DATABASE=springuser -e MARIADB_USER=springuser -e MARIADB_PASSWORD=springuser -d mariadb:latest
45+
```
3946

4047
Or on Apple Silicon:
4148

49+
```
4250
docker run -p 127.0.0.1:3306:3306 --name springuserframework -e MARIADB_ROOT_PASSWORD=springuserroot -e MARIADB_DATABASE=springuser -e MARIADB_USER=springuser -e MARIADB_PASSWORD=springuser -d arm64v8/mariadb:latest
43-
51+
```
4452

4553
### Mail Sending (SMTP)
4654
The framework sends emails for verficiation links, forgot password flow, etc... so you need to configure the outbound SMTP server and authentication information.
4755

4856
### SSO OAuth2 with Google and Facebook
4957
The framework supports SSO OAuth2 with Google and Facebook. To enable this you need to configure the client id and secret for each provider.
5058

51-
For local development you will need a public hostname and HTTPS enabled. You can use ngrok to create a public hostname and tunnel to your local machine. You can then use the ngrok hostname in your Google and Facebook developer console configuration.
52-
53-
There is an example configuration file in /src/main/resources called application-local.yml-example. By default this project's gradle bootRun command runs Spring using the "local" profile. So you can just copy that file to application-local.yml and replace the values (keys, URLs, etc..) with your values. If you are using a different profile to run (such as default) you will just need to ensure the same configs are in place in your active configuration file(s).
54-
55-
Missing or incorrect configuration values will make this framework not work correctly.
59+
For public OAuth you will need a public hostname and HTTPS enabled. You can use ngrok to create a public hostname and tunnel to your local machine. You can then use the ngrok hostname in your Google and Facebook developer console configuration.
5660

5761

5862
### New Relic
@@ -77,17 +81,23 @@ Read the following articles:
7781
- https://www.digitalsanctuary.com/java/how-to-get-springboot-livereload-working-over-https.html
7882

7983
### Live Reload over HTTPS Setup
80-
If you are running your local dev env using HTTPS or referencing it from a ngrok tunnel using HTTPS, you will need to make a few changes to get Live Reload to work. First you need to comment out the LiveReload HTTP line near the bottom of the index.html Thymeleaf template file. And uncomment the HTTPS line just below.
84+
If you are running your local dev env using HTTPS or referencing it from a ngrok tunnel using HTTPS, you will need to make a few changes to get Live Reload to work. First you need to tell the application to use HTTPS by setting the following properties in your application.yml file:
85+
86+
```
87+
spring.devtools.livereload.https=true
88+
```
8189

8290
You then need to install mitmproxy and configure it to intercept the HTTPS traffic. You can do this by running the following command:
8391

92+
```
8493
mitmproxy --mode reverse:http://localhost:35729 -p 35739
94+
```
8595

86-
By default, mitmproxy uses self-signed SSL certificates, so you need to tell your browser to trust them before this will work. You can do this by opening https://localhost:35739/livereload.js in your browser and going through the steps to trust the server and certificate. Alternatively, you can configure mitmproxy to use real certificates and avoid this step. Follow these directions: https://docs.mitmproxy.org/stable/concepts-certificates/
96+
By default, mitmproxy uses self-signed SSL certificates, so you need to tell your browser to trust them before this will work. You can do this by opening https://localhost:35739/livereload.js in your browser and going through the steps to trust the server and certificate.
97+
98+
Alternatively, you can configure mitmproxy to use real certificates and avoid this step. Follow these directions: https://docs.mitmproxy.org/stable/concepts-certificates/
8799

88100
## Notes
89101
Much of this is based on the [Baeldung course on Spring Security](https://www.baeldung.com/learn-spring-security-course). If you want to learn more about Spring Security or would like to add SSO integration or 2FA to your application, that guide is a great place to start.
90102

91-
The codebase provides examples of different ways to serve and consume APIs. For instance, some APIs return a 200 response for all queries with a success flag and use status codes to convey success or failures. Others only use the 200 response for successful requests and use 409 or 500 for various error scenarios. The AJAX client JavaScript in the codebase also showcases different approaches, with some triggering redirects to new pages while others display messaging directly on the current page. These examples aim to demonstrate different implementation options depending on your preferences and requirements.
92-
93103
Please note that there is no warranty or guarantee of functionality, quality, performance, or security made by the author. The code is available freely, but you assume all responsibility and liability for its usage in your application.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.digitalsanctuary.spring.user.util;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ModelAttribute;
6+
7+
/**
8+
* Provides a global advice for controllers to include LiveReload configuration details.
9+
* <p>
10+
* This advice will make the LiveReload port available to all controllers and views in the application. It is used on the layout.html template to
11+
* include the LiveReload script in dev and local environments.
12+
* </p>
13+
*
14+
* @author Devon Hillard
15+
*/
16+
@ControllerAdvice
17+
public class LiveReloadGlobalControllerAdvice {
18+
19+
/**
20+
* Flag to determine if the application is being accessed over HTTPS.
21+
*/
22+
@Value("${spring.devtools.livereload.https:false}")
23+
private boolean isHttps;
24+
25+
/**
26+
* Provides the appropriate LiveReload port based on the application's protocol.
27+
* <p>
28+
* If the application is running over HTTPS, the port will be {@code 35739}. Otherwise, it will be {@code 35729}.
29+
* </p>
30+
*
31+
* @return The appropriate LiveReload port.
32+
*/
33+
@ModelAttribute("liveReloadPort")
34+
public int liveReloadPort() {
35+
return isHttps ? 35739 : 35729;
36+
}
37+
38+
}

src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
"name": "spring.security.oauth2.enabled",
160160
"type": "java.lang.String",
161161
"description": "A flag to enable/disable OAuth2 setup in WebSecurityConfig"
162+
},
163+
{
164+
"name": "spring.devtools.livereload.https",
165+
"type": "java.lang.String",
166+
"description": "A description for 'spring.devtools.livereload.https'"
162167
}
163168
]
164169
}

src/main/resources/application-local.yml-example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ spring:
6969

7070
livereload:
7171
enabled: 'true'
72+
https: 'true'
7273

7374
mvc:
7475
log-request-details: 'true'
@@ -102,3 +103,4 @@ management:
102103
export:
103104
account-id: ACCTID
104105
api-key: KEYYYYY
106+

src/main/resources/templates/layout.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ <h1>Static content for prototyping purposes only</h1>
5151
<br /><br />
5252
<div th:replace="~{fragments/footer :: footer}">Footer</div>
5353

54-
<!-- Enable LiveReload in the dev environment -->
55-
<!-- Uncomment this line if you are running without HTTPS -->
56-
<!-- <script th:if="${@environment.acceptsProfiles('dev','local')}" src="http://localhost:35729/livereload.js"></script> -->
57-
<!-- Uncomment this line if you are running with HTTPS, and refer to my blog post here: https://www.digitalsanctuary.com/java/how-to-get-springboot-livereload-working-over-https.html -->
58-
<script th:if="${@environment.acceptsProfiles('dev','local')}" src="https://localhost:35739/livereload.js"></script>
54+
<!-- Enable LiveReload in the dev and local environments -->
55+
<script th:if="${@environment.acceptsProfiles('dev','local')}" th:src="'https://localhost:' + ${liveReloadPort} + '/livereload.js'"></script>
5956
</body>
6057

6158
</html>

0 commit comments

Comments
 (0)