The subject of this post may sound pretty dull, but here's the scenario which makes it interesting. We're using Spring Social and need to set an ApplicationUrl property on our ProviderSignInController based on our current environment. Each environment requires a different setting that we need to set at run-time.
We could be running our Spring Social application on a localhost:port, a virtual domain with a proxy, on a public facing site, or something else. Because OAuth Providers treat these environments differently, the Spring ApplicationUrl property comes in handy. Let's say we're using Apache Proxy Mod where a port, say :8086, is re-routed to an executable WAR. The OAuth2 Server (Twitter, for example) will return authorization to localhost:8086 instead of to the WAR virtual address. Our ProviderSignInController ApplicationUrl property fixes that.
Back to how to pass the ApplicationUrl as a Spring Boot argument. We'll demonstrate doing this in an Eclipse Run Configuration, in a Gradle bootRun task, and on the command line when executing our WAR file. We'll also see how Spring's CommandLineRunner allows us to view any command arguments on startup.
Our three environments for using Spring Social with NixMash Spring are http://localhost:9000, http://nixmashspring.com (a local virtual site using a WAR) and our public facing site at http://nixmashspring.daveburkevt.com, also a WAR. We can view these as Development, Staging and Production if you like.
Let's jump to where we actually use our applicationUrl property, in the SocialConfig Configuration Class where we use the spring.social.application.url Environment Property we're passing to the Spring Boot app.
Earlier in the Component we'll convert the environment property to a local variable.
Before moving on to how we'll pass the appurl argument to our Spring Boot App, let's look at the application.properties entry. We're using a Spring Shortcut to apply the value of the appurl argument and if missing default to http://localhost:9000.
Here's how we'll set appurl in an Eclipse Run Configuration. Notice the 2 dashes “–“.
We could also add the argument in a build.gradle bootRun task.
When executing our WAR we add the argument with the double-dashes like elsewhere. Here's the complete bash script that kills the existing WAR process and loads a fresh site.
I blogged on the Spring CommandLineRunner before. Here is an excerpt from the custom CommandLineRunner class demonstrating how we'll retrieve the appurl property and display it on startup.
And the output on application startup.