Spring Boot with embedded Tomcat behind Apache proxy - spring

We have a Spring Boot (Spring MVC) app with embedded Tomcat on a dedicated appserver behind an Apache SSL proxy.
The SSL port on the proxy server is 4433, forwarding to port 8080 on the appserver.
So the URL to the proxy server is forwarding like:
https://proxyserver:4433/appname >>forward>> http://appserver:8080/
When running WITHOUT proxy, the first thing that happens is that
Spring Security redirects the request, like:
http://appserver:8080/ >>redirect>> http://appserver:8080/login
to display the login form, by extending WebSecurityConfigurerAdapter with
...
httpSecurity.formLogin().loginPage("/login") ...
...
It works fine without the proxy, but WITH proxy the redirect needs to be changed,
so Spring should instead redirect to the corresponding proxy URL, like:
http://appserver:8080/ >>redirect>> https://proxyserver:4433/appname/login
but no success yet.
I am trying to apply this solution:
59.8 Use Tomcat behind a front-end proxy server
We have configured mod_proxy in Apache, and verified that it sends the expected headers:
X-Forwarded-For: xxx.xxx.xxx.xxx
X-Forwarded-Host: proxyserver
X-Forwarded-Port: 4433
X-Forwarded-Proto: https
The application is started with parameters:
export ARG1='-Dserver.tomcat.protocol-header=x-forwarded-proto'
export ARG2='-Dserver.tomcat.remote-ip-header=x-forwarded-for'
java $ARG1 $ARG2 -jar webapp.jar
Still the redirect does not work.
It will keep redirecting locally, to http://appserver:8080/login which is not available to the clients.
Is there anything else we need to do to make this scenario work?
UPDATE
Also, I am concerned about the "/appname" part in the proxy URL. On the appserver the application is rooted at "/". How should Spring be instructed that "/appname" should be included in all URLs sent back to the clients, when going thru the proxy?

I had the same problem the other day. After some debugging of Spring Boot 1.3 I found the following solution.
1. You have to setup the headers on your Apache proxy:
<VirtualHost *:443>
ServerName www.myapp.org
ProxyPass / http://127.0.0.1:8080/
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
ProxyPreserveHost On
... (SSL directives omitted for readability)
</VirtualHost>
2. You have to tell your Spring Boot app to use these headers. So put the following line in your application.properties (or any other place where Spring Boots understands properties):
server.use-forward-headers=true
If you do these two things correctly, every redirect your application sends will not go to http://127.0.0.1:8080/[path] but automatically to https://www.myapp.com/[path]
Update 1. The documentation about this topic is here. You should read it at least to be aware of the property server.tomcat.internal-proxies which defines the range of IP-addresses for proxy servers that can be trusted.
Update 2021 The documentation is moved to here. The Spring Boot configuration is a litte different now.

Your proxy looks fine, and so does the backend app, up to a point, but it doesn't seem to be seeing the RemoteIpValve modified request. The default behaviour of the RemoteIpValve includes a pattern match for the proxy IP address (as a security check) and it only modifies requests that it thinks are from a valid proxy. The pattern defaults in Spring Boot to a well-known set of internal IP addresses like 10.*.*.* and 192.168.*.*, so if your proxy isn't on one of those you need to explicitly configure it, e.g.
server.tomcat.internal-proxies=172\\.17\\.\\d{1,3}\\.\\d{1,3}|127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}
(using properties file format, which means you have to double escape the backslashes).
You can see the what is happening in the RemoteIpValve if you set
logging.level.org.apache.catalina.valves.RemoteIpValve=DEBUG
or set a breakpoint in it.

A typical solution to this problem is to let the proxy handle any required rewrite. For example, in Apache you can use the rewrite_module and/or headers_module to correct headers. As another example, Nginx handles this and other similar cases automatically for you after configuring upstream servers.
In response to comments:
What are the remote_ip_header and protocol_header spring boot configuration values?
Let's forget Spring Boot for a moment. Tomcat, the embedded servlet container, features a valve known as the RemoteIpValve. This valve is a port of the Apache remotip_module. The primary purpose of this valve is to treat the "useragent which initiated the request as the originating useragent" for "the purposes of authorization and logging". In order for this valve to be used it needs to be configured.
Please find more information about this valve here.
Spring Boot conveniently supports configuring this valve via application.properties through the server.tomcat.remote_ip_header and server.tomcat.protocol_header properties.

I had exactly the same case using haproxy as load balancer with the below configuration, which worled for me. The only thing is the client IP is in request.getRemoteAddr() and not in "X-Forwarded-For" header
frontend www
bind *:80
bind *:443 ssl crt crt_path
redirect scheme https if !{ ssl_fc }
mode http
default_backend servers
backend servers
mode http
balance roundrobin
option forwardfor
server S1 host1:port1 check
server S2 host2:port2 check
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
In application.properties:
server.use-forward-headers=true

There are several properties that you can configure, related to this.
application.yaml example:
server:
forward-headers-strategy: native
tomcat:
use-relative-redirects: true
protocol-header: x-forwarded-proto
remote-ip-header: x-forwarded-for
Setting server.forward-headers-strategy: native is the replacement of the deprecated server.use-forward-headers:true

Have you tried setting
server.context-path=/appname
In Spring Boot?

Try setting the Rewrite rule like:
https://proxyserver:4433/appname >>forward>> http://appserver:8080/appname
And then set your application context to "appname"
server.context-path=/appname
So locally you can run by http://appserver:8080/appname and via Reverse Proxy you access via https://proxyserver:4433/appname
Since I am using JBOSS, changes in standalone.xm of jboss:
<http-listener name="default" socket-binding="http" redirect-socket="https" proxy-address-forwarding="true" enable-http2="true"/>
Tomcat would have similar config, to inform Tomcat (proxy-address-forwarding="true") to respect the proxy forwarding address.

server.use-forward-headers=true did not work for me, experienced a weird issue where X-Forwarded-For header is not populated to HttpServletRequest consistently.
Ended up using ForwardedHeaderFilter: https://stackoverflow.com/a/51500554/986942.
On top of that, make sure the load balancer (proxy) provide the following headers properly:
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;

Related

Spring Authorization Server Redirects to HTTP when behind proxy

I have a standalone spring authorization server at https://auth.foo.com with certificate configured in the spring boot application directly (no Proxy).
I have a client spring boot app using http 8080 behind a K8s Nginx Proxy. The certificate terminates on the proxy and then delivers http upstream to the spring boot app. https://client.foo.com
I have enabled the following in the client application.yaml
server:
port: 8080
forward-headers-strategy: native
tomcat:
use-relative-redirects: true
remoteip:
remote-ip-header: x-forwarded-for
protocol-header: x-forwarded-proto
When navigating to the https://client.foo.com app, the relative redirects work fine, all links redirect to https.
However on successful login via https://auth.foo.com, the auth server redirects to https://client.foo.com/login/oauth2/code/foo-client-oidc?code=XXX then the client responds with a 302 but the Location header is insecure http://client.foo.com
Is there a way to force the redirect to use https in Spring Authorisation Server
Thanks in advance

Issue with Spring Security w/ Azure AD & the redirect_uri behind a reverse proxy

I have been trawling through the internet in search of a solution to my problem which is the following -
We have a Spring boot application with Spring Security OAuth2 and Azure Active Directory integration to manage our external authentication. When we try to log in, the redirect_uri in the login URL is wrong.
Instead of the redirect_uri showing as it is defined in the application.yaml & Azure AD Portal like "&redirect_uri=https://10.10.10.10/v1/login/oauth2/code/", it's appearing as "&redirect_uri=http://10.10.10.10:8080/v1/login/oauth2/code/". As you can see, it's dropping the https scheme and adding on the port which results in an error logging in due to a redirect_uri mismatch. It's important to note that this works fine locally, however when deployed to an environment behind a reverse proxy, it doesn't work.
What I have tried -
Adding the following properties to application.yaml -
server.forward-headers-strategy=NATIVE
server.forward-headers-strategy=FRAMEWORK
server.tomcat.internal-proxies=.*
server.tomcat.remoteip.protocol-header=x-forwarded-proto
server.tomcat.remoteip.remote-ip-header=x-forwarded-for
Adding this to the nginx config for the path -
resolver 127.0.0.11 valid=30s;
set $upstream_be application;
proxy_pass http://$upstream_be:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Host $host;
None of this has worked unfortunately and the issue still persists. Any ideas anybody?
Relevant pom.xml versions: spring-boot, spring-security=2.4.5, all azure-spring dependencies=3.4.0
This also had worked fine before we upgraded our azure dependencies from 2.3.5 to 3.4.0.
When the proxy applies the standard RFC7239 "Forwarded Headers" like X-Forwarded-Proto and X-Forwarded-Host, then the redirect url should be correctly computed after applying the following two configurations. (I assume you're using Tomcat)
server.forward-headers-strategy=NATIVE
"If the proxy adds the commonly used X-Forwarded-For and
X-Forwarded-Proto headers, setting server.forward-headers-strategy to
NATIVE is enough to support those."
server.tomcat.redirect-context-root=false
If you are using Tomcat and terminating SSL at the proxy,
server.tomcat.redirect-context-root should be set to false. This
allows the X-Forwarded-Proto header to be honored before any redirects
are performed.
How is the redirect URL defined in Spring Boot? The above configuration works if you use a placeholder for the base URL, for example {baseUrl}/login/oauth2/code/{registrationId}. In this way, the {baseUrl} placeholder is dynamically resolved by Spring Security differently depending on whether it's behind a proxy or not.
More info in the official documentation:
Spring Boot - Running Behind a Front-end Proxy Server
Spring Security - Proxy Server Configuration

How to run Spring Boot on port 443

I ran into an issue with one of our Spring Boot applications. We have it running with https on port 8443 and it all works fine. Now we're building an integration with an external payment processor and they require that we have a callback endpoint in our application on port 443.
Some research tells me that deploying a Spring boot app listening to a port number below 1024 is not allowed. The threads i find on this issue usually say "use a port number above 1024" and the poster walks
off happy. I already have that and need to figure out a solution that uses port 443.
Does anyone have any recommendations? Could i solve this by building an Apache proxy for the callback endpoint?
We have web applications using Apache2 and port 443 on the same server, so the Boot application needs to coexist with that.
OK, i managed to solve this issue by myself in a pretty simple and elegant way. In the process, i also solved the issue of Spring boot applications having to be called with a port number in the URL, which has been annoying me.
I found this thread: Spring Boot with embedded Tomcat behind Apache proxy
In it, the solution is pretty much laid out. I had to activate three apache2 mods:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod headers
I added these lines to my Apache2 vhost config file, right under ServerName in the VirtualHost tag:
SSLProxyEngine on
ProxyPass / https://127.0.0.1:8443/
ProxyPassReverse / https://127.0.0.1:8443/
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
ProxyPreserveHost On
8443 is the port number used by the Spring Boot application, as configured in the server.port property.
As a bonus, this also means we no longer need to open the ports used by our Boot applications in our firewall.
There several Web-Server or Reverse-Proxy solutions, which can listen on port 443 and route your requests to Port 8443 of your Spring Boot application server.
Beside Apache2 there are:
nginx : https://www.nginx.com/
haproxy : https://www.haproxy.org/
There are probably many more, but those 3 I used so far.
If your application is running in a cloud - the cloud provider offers typically also services, which can do this job, e.g. AWS ELB.

Spring Boot & ELB - How do I make the load balancer redirect http to https?

I have deployed a Spring Boot application via Elastic Beanstalk. I'm using a load balancer, so this is the flow (as far as I understand):
Internet/Browser request ---HTTPS---> Load Balancer ---HTTP---> Spring Boot App Server
So essentially, the SSL terminates at the load balancer and the app server just deals with plain old HTTP.
But in the case of a HTTP request from the browser, I would like the load balancer to automatically redirect to HTTPS.
There are several questions about this issue:
Spring Boot with Embedded Tomcat behind AWS ELB - HTTPS redirect
How to redirect automatically to https with Spring Boot
Spring Boot redirect HTTP to HTTPS
But none of the answers to these questions make sense to me. Perhaps I'm misunderstanding, but all the answers basically make the Spring Boot app only server HTTPS request (for example when using http.requiresChannel().anyRequest().requiresSecure()).
However, this goes against the flow because I'm perfectly fine with the SSL terminating at the load balancer and the Spring Boot app server just dealing with HTTP. So if I require SSL at the spring boot level, then I'll need to do an end-to-end SSL connection, which isn't really required for my application.
I have also used the following properties, which don't seem to help either:
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto
With the help of this article, I was finally able to figure out how to do this for a Spring Boot app in an ELB environment.
I had to create a conf file in src/main/webapp/.ebextensions/nginx/conf.d. I just called it myconf.conf.
In myconf.conf, I put this code in:
server {
listen 80;
server_name www.my-site.com;
if ($http_x_forwarded_proto != "https") {
rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
}
}
Also, make sure that both HTTP and HTTPS listeners are open for the load balancer.
Additionally, my spring boot app only opens up HTTP since the load balancer already terminates SSL.
AWS Load balancer cannot handle redirection. You may do it via your server or by using cloudfront distributions.

Spring Boot forcing https cause redirection too many times in Google App Engine

I enabled https for all the requests within the App Engine - Flexible Environment.
override fun configure(http: HttpSecurity) {
if (securityProperties.isRequireSsl) http.requiresChannel().anyRequest().requiresSecure()
}
But it is causing too many redirects and fails.
How to force https with Spring Boot on App Engine?
The Google Cloud Load Balancer terminates all https connections, and
then forwards traffic to App Engine instances over http. Read More
Which was causing the redirects as the Spring Application was unaware of this switch.
App Engine proxies the request to the application by adding the X-Forwarded-Proto and X-Forwarded-For headers with which spring identifies the actual request protocol and where the request actually originated from.
To enable such behavior, I added the following configuration in application.yml:
server:
tomcat:
remote_ip_header: X-Forwarded-For
protocol_header: X-Forwarded-Proto

Resources