Spring security: using relative path - java

I have an application with name test.war.
Because of Apache installed on my server I have to use another port number for Tomcat applications so after deployment this application available at domain.com:8080/test/.
I decided to create a subdomain in order to remove that ugly 8080 from url, so I setted up the server like described here. So now test.domain.com reffers to domain.com:8080/test/.
Everything seems fine except of one problem - because my application is not ROOT.war and I am using spring:url function every url in the application is translated to /test/bla-bla. So I removed that function. But still have a problem with spring security because it still translates an urls relative to app name i.e. /test/bla-bla.
How can I solve that problem?
Thank you
UPD: I don't want to deploy it as a ROOT application because I have two or three such applications and I wanted to create a subdomain for each one of them

Spring Security doesn't "translate" URLs. In fact this isn't specific to Spring Security. You'll run into similar issues with any application which does redirects. If you want to hide the context paths of applications which are behind a proxy, then you need to rewrite the URLs at the proxy.
I'd recommend you read the Tomcat Generic Proxy Howto and the section on URL rewriting in particular, as it specifically addresses this issue.

Related

How to set context path in Spring Boot running in AppEngine Standard

Is there a way to set a context path in a Spring Boot application running in GCP AppEngine Standard?
I want to add an implicit prefix to all my resources. So if the #RestControllers are, say, /a and /b, I want that to access them, their URL is always /my/prefix/a and /my/prefix/b. In normal, embedded mode in Spring Boot, this works by setting server.servlet.context-path. However, this setting is not respected by AppEngine. In it, /my/prefix/a returns a 404 and I have to use /a instead
How do I add a common prefix to all my resources in AppEngine Standard? This seems basic for AppEngine's services/modules support to avoid duplicating the prefix in all controllers, but I can't find how to do it
so per my conversation with the Google Cloud Support team - I got confirmation that the App Engine Standard does not support / honor the port & context paths set in spring-boot.
This is the response I got from them:
"With regards to your concern about App Engine not honoring the manually set port number and context path, this is because it should be setup in yaml file.
For the port to be set as 7777, it should be done by port forwarding. However, port forwarding is only available in App Engine Flex. For more details about this, you may visit this page [1].
For the context-path, you may use the tag and include in appengine-web.xml file. This sets the path in your application that contains the static files. You may check this document [2] for more information.
You could also include the tag in your appengine-web.xml as it is also useful for serving static content such as images, CSS stylesheets or JavaScript files. You may use this reference [3].
With this solution or workaround, this also address that spring-boot application could be deployed in App Engine.
If you have further concerns, just let me know.
Thank you and have a nice day!
[1] https://cloud.google.com/appengine/docs/flexible/custom-runtimes/configuring-your-app-with-app-yaml#port_forwarding
[2] https://cloud.google.com/appengine/docs/standard/java/config/appref#public_root
[3] https://cloud.google.com/appengine/docs/standard/java/config/appref#static_files
"
However, setting the to some value did not still pick it up as the context path. So had to abandon GAE unfortunately. Wasted so much time on this - hope this is useful for others that are struggling with this.

How to apply filter to the request to resource which are directly under webapps

I want to apply filter in below URL's.
http://10.78.97.29/robots.txt
http://10.78.97.29/sitemap.xml
https://10.78.97.29/
Since they does not contain any specific URL pattern , I am not getting any idea. And in which web.xml I have to add that filter mapping. I am using Tomcat as a webcontainer.
Thanks in advance.
Tomcat by default uses the web application named ROOT to serve the application at the root folder of the server. You can either handle the requests there or just have the files contained in the web application.
You might want to read about tomcat naming for more details, explaining this behaviour.
Alternatively, you can also have an Apache httpd in front of your tomcat and handle those files there.

how to set java web application's context root when working with reverse proxy

My old way using mod_jk in apache and configure virtual host in tomcat
In the JSP file, I refer to CSS as below
/<%=request.getContextPath()%>/css/styles.css
while the home link is set to
/<%=request.getContextPath()%>/
so this worked fine when I use mod_jk in apache to work with tomcat using ajp;
When I try to configure reverse proxy as below
ProxyPass / http://localhost:800/mywebapp
ProxyPassReverse / http://localhost:800/mywebapp
the home page can be retrieved fine but the css request becomes
http://mydomain.com/mywebapp/mywebapp/css/style.css
so the css file can not be retrieved correctly;
I think one possible way is to always use relative path like ./style.css or ../style.css
a. since header/footer are shared, and the home page is in a different level with detail page, it's inconvenient to use relative path because they're at a different level
b. still, I think the home link will have to be /<%=request.getContextPath()%>/
so I wonder what's the way to set contextroot fine in java web and also work fine with reverse proxy?
thx a lot
As I know your application server (Tomcat) isn't able to be aware of a reverse proxy presence. Generally speaking, it can be contacted through any number of reverse proxies or directly by browsers. A network configuration is usually used to limit this, not HTTP or Java.
So, you must accurately rely on relative URLs to make your application work well.
When I have to deal with reverse proxy presence (almost always due to SSO architectures), I embed a "junction" configuration string item (the portion of the URL used in the proxy to map the application) and use it in the only places where I need to build an absolute URL.

Map different url to same web application in Tomcat

I am not clear on the following:
If we have a web application named: SomeWebApp under Tomcat's webapp directory, the url to access it is:
http://localhost:8080/SomeWebApp
My question is, is it possible to configure Tomcat so that other URLs would point to that web application?
E.g.
http://localhost:8080/ADifferentApp will also point to the SomeWebApp?
From the web.xml I think is not possible since it is about the url patterns when you are inside the SomeWebApp scope.
So what is the proper way to do it? If it is possible that is.
The approach I found to work best is to install Apache2 on the server and proxy all requests. Tomcat is surprisingly difficult to configure in other ways than intended. In my experience, Tomcat doesn't provide this functionality declaratively.
I'd rather recommend Nginx than Apache as proxy. I'm recently working on a project that incorporates tomcat and nginx works as proxy.
Once you've got nginx you can acctualy map as many url's to access the same web application as you want.
Yes,its possible to map different context path to single application edit conf/server.xml file
> **> <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp"
> > path="/SomeWebApp" />
> > <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp" path="/ADifferentApp "/>**
Access application with 2 URL's

ASP.NET/IIS equivalent of Java/WAS context-root

In Java on WebSphere Application Server if I want my servlets, etc., to start with a certain root path, I use the context-root property in the EAR deployment descriptor (application.xml). For example, my servlet is named GetData, but I want the URL to be www.mysite.com/secure/restricted/GetData, so I set the context-root to secure/restricted.
How do I do that in ASP.NET on IIS? Is the some kind of configuration setting for the application?
One option would be Url Rewritting - http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
On the other hand, if you have your dynamic asp.net site separated from the rest of your content, you can add the folder secure/ and configure in there through the IIS Manager to point restricted to your asp.net site (regardless of where you have it stored).
That said, I don't know whether the WAS context-root give you something extra, for links to other info outside the asp.net site.
In .Net Core 2.0, there is an applicationUrl property in launchSettings.json:
By default, it will be "http://localhost:port/GetData" or "http://localhost:port"
Just change it to your final URL:
"http://localhost:port/secure/restricted/GetData"

Categories