Setting a base url for urls generated by Spring - spring

We've got a restful api running using spring which generates HATEOAS links. When I run the service locally, they are generated fine, but on our server, which hosts tomcat behind apache which proxies the app to /api/ (locally it's just run on /), the urls are still generated like the app is run on /.
Is there any way to tell spring to use a base url when generating these?

I've figured out my problem. Spring wasn't at fault. When building links, spring uses the X-Forwarded-Host, X-Forwarded-Ssl, etc. and, since 4.1.3, X-Forwarded-Prefix to correctly build urls that will be forwarded by a proxy. This last one however, is not automatically set by apache when proxying, so installing mod_headers and adding RequestHeader set X-Forwarded-Prefix "api"to my LocationMatch directive solved everything.

Related

How to disable force HTTPS in tomcat based on header value

I've got a Spring boot application hosted on internal tomcat. We've enabled the below properties to force a redirect to HTTPS as a response.
server.use-forward-headers=true
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto
Now, I've a requirement to open this application for internal use as well and in this case, I should not force HTTPS in response.
I understand that I can use property
server.tomcat.internal-proxies=192\\.168\\.\\d{1,3}\\.\\d{1,3}
But I do not want to do this based on IP, rather want to use a specific value in the header.
Example: if header "X-Forwarded-Host" contains a value like "test.com.sg" --> then do NOT force HTTPS.
May I know how can I achieve this?

Proxying Vaadin Spring Boot application and URL issue

I've asked the same question at Vaadin's forum, but noone responds there, so maybe someone here can help.
I've developed a Vaadin Spring Boot app that works perfectly when I run it as is, but when I tried to run it behind Apache Proxy (so that requests to http://website/vaadin-app/ are forwarded to http://website:8080), I've got a problem: UI is rendered successfully, but the data can't be fetched from the server. The error is: The requested URL /vaadinServlet/UIDL/ was not found on this server.
It looks like Vaadin doesn't make relative requests, but rather absolute ones, so it tries to load http://website/vaadinServlet/UIDL/ instead of http://website/vaadin-app/vaadinServlet/UIDL/
What kind of configuration should I add to address this issue?

How to deploy html from tomcat server with Springboot

So, the answer is probably super easy, but I just can't seem to figure it out.
I have set up a REST webservice, according to this tutorial: https://spring.io/guides/gs/rest-service/ and have set up the Requestmapping and everything using postman, and that end is working like I intended to.
I am setting up my site locally, but when I try to send a get request with jquery, I am receiving the following error: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
So I have understood that the problem is that I am trying to run my html from file:// and that the solution is to run it from localhost on the tomcat server and found this answer in another topic: Deploying just HTML, CSS webpage to Tomcat
However, I cannot find such a webapps folder, and I am assuming it is because my tomcat server is deployed using maven and springboot. So how do I deploy my html/js on the tomcat server when it is deployed this way?
I am working on a mac and with IntelliJ.
In your spring boot application you can put your index.html file in src/main/resources/static directory and it will be served by the application.
Also you may try to configure CORS in spring boot, see this answer for links.
If you are deploying normal web applications like jee apps you do it by placing your war file in webapps folder. The web apps folder is inside your tomcat
But for for intellij-idea go through this, it should work:
Where is my app placed when deploying to Tomcat?

How to disable tomcat 8 websocket server endpoint autodiscovery

I need to do some processing on the endpoint classes before they can be deployed and then deploy them manually. However it seems simply having a class annotated with #ServerEndpoint in my war is enough to deploy the endpoint in Tomcat and when I try to manually deploy later obviously I can't because the URL has been deployed already. Is there any way to disable the autodiscovery of endpoints?
Looking at the source for the version I'm using - 8.0.28, there's no dedicated option. The code deploying the endpoints is in org.apache.tomcat.websocket.server.WsSci. The quickest 'shurest' hack is to put my endpoints into the javax.websocket package. I elected to use their ServerApplicationConfig hook instead which serves my purposes if with some minor issues.

how to enable https if application running in tomcat and resources are comming from apache?

We have created spring application, this is running in tomcat and resources(css, images and js) are coming from apache. We are trying to enable ssl but we are not able to get resources from apache. In console getting exception like resources could not be loaded. Can any one please help me.
You should probably use Apache as a reverse proxy to tomcat. This way, everything will go through Apache. Resources will be served directly, and requests to the appication will be proxied to your tomcat server:
http://httpd.apache.org/docs/current/mod/mod_proxy.html

Resources