Spring + Tomcat multi tenancy based on Request URL - spring

We have a single application which is used by multiple customers. It's every time the same app, but with a different database.
Our current setup consists of:
Apache 2 Web Server
Tomcat 8
Spring 4.1
The Web Server routes specific URLs to Tomcat, i.e.
/customer1 -> tomcat/customer1
/customer2 -> tomcat/customer2
Now we'd like to keep those entries within Apache Config, but somehow configure Tomcat and Spring. Basically Tomcat should take every request (maybe defined by regex - not necessary) and route it to one application.
Currently one application runs at /webapps/customer1 and another instance at /webapps/customer2.
In the future the application should somehow run at /webapps/*, so within spring I can have a look at the request URL and choose db_customer1 for requests at /customer1/... and the same for customer2.
Is this possible to achieve?

If I understood you correctly, you want single application that uses 2 (or more) databases, depending on the customer. Customer is determined via URL.
If that is the case, you should take a look at AbstractRoutingDatasource and create your own implementation of that class. It allows you to use different datasources in a single application. You should then create a Filter or maybe Interceptor that would intercept the URL and then, based on the URL, route the routing datasource to the appropriate underlying datasource.
Take a look at this example linked below: it has a customer routing datasource that uses different database for different logged in user:
https://spring.io/blog/2007/01/23/dynamic-datasource-routing/

Related

centralization of spring boot management endpoint url

Is it possible in spring-boot application to create single management port/url of many application.
eg. in single instance we can do like below given property.
management.server.port=89090
We can implement it by creating management server of spirng-boot application for more details click on https://www.baeldung.com/spring-boot-admin

spring-saml multi-tenant - 1 webapp with multi SPs

I've deployed 1 webapplication (WAR) to Tomcat behind context root /myApp
The application is exposed to the public by means of 2 public HTTPS URLs:
Eg. https://a.this.that and https//b.this.that
Both these public requests arrive in that same application (/myApp).
Now I need this webapplication to support 2 SAML service providers.
Both will use the same IDP.
How should I configure Spring SAML in order for this application to support two SPs, one for the first URL, and one for the second ?
What ACS location URLs should I put in my service provider XML ?
Do I need multiple SP XML files and/or do I need to define extended service provider metadata to get this working ?
Do I need a spring saml multi-tenant setup to get this working and if so what is the relationship between the entityId and the URL ? Or can all this be achieved in a different way without a multi-tenant setup ?
Could somebody provide some sample xml metadata, spring saml config that demonstrates how the above could be achieved ?
Kr,
EDH
You'll need to generate SP metadata for each of the instances separately, and add it to the metadata manager. The URLs will be a.this.that in the first one, and b.this.that in the other. You also need to set entity alias to differentiate the two instances. See the manual for more details.

Sharing of a view among microservies

I'm splitting up a monolith web service into several microservices using spring boot. To reduce duplicated code I extracted shared parts in a maven module that is used in the different microservices.
The monolith application had a healthcheck page that showed various information about the state of the service and some debbuging infos. It is implemented with Spring MVC and jsp.
I'd like to use this view in each of the microservices. Whats the best way to do this without duplicating the view/controller?
I was thinking of adding a web module to the shared maven project that contains the controller, view, spring mvc settings,...
But I'm not sure if it is good to have two web modules in one microservice.
Have you considered using spring boot actuator to retrieve health (and more) application information?
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
You could then have another microservice that retrieves that information from each of your services, or just simply check it on then hitting the different endpoints (/health, /env, etc.).
UPDATE:
If you have you custom health logic you can even implement your own actuator endpoint for it. Furthermore, you can create your own library to reuse it in all your microservices:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-customizing-endpoints-programmatically
46.4 Adding custom endpoints
If you add a #Bean of type Endpoint then it will automatically be exposed over JMX and HTTP (if there is an
server available). An HTTP endpoints can be customized further by
creating a bean of type MvcEndpoint. Your MvcEndpoint is not a
#Controller but it can use #RequestMapping (and #Managed*) to expose
resources.
[Tip]
If you are doing this as a library feature consider adding a
configuration class annotated with #ManagementContextConfiguration to
/META-INF/spring.factories under the key
org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration.
If you do that then the endpoint will move to a child context with all
the other MVC endpoints if your users ask for a separate management
port or address. A configuration declared this way can be a
WebConfigurerAdapter if it wants to add static resources (for
instance) to the management endpoints.

Spring Boot REST JSP

I have a working Spring BOOT application that has a custom security provider and REST API controllers. I would also like to add a GUI interface to the application for access from a browser through jsps, html, and a login page which uses my existing custom security provider I used with the REST APIs. Maybe using Spring MVC since that is needed for the REST API support. I could not find a single example of doing this on the web. Also, I do NOT want to use any web XML based configuration files - as I am currently only using Java config for the implementation of the REST APIs. I am also currently using SSL for REST API access through SSL in a Jetty embedded web container. Please help if you can? Thanks in advanced.
Paul there is a rather large amount of information on view technologies that are compatible with Spring BOOT. You need to decide what you want to use and do relevant research for it.
As a guiding hand here check this page out for just one of the many types:
http://kielczewski.eu/2014/04/spring-boot-mvc-application/
You may follow this procedure :
lets assume that u have an endpoint for which you need both REST and view controllers,your REST endpoint exposes your data in JSON as RESTController and your view Endpoint returns the view name as Simple old controller.
lets say your base url is at localhost:8080 and your endpoint of interest is /students
you could have both in same application but at different endpoints like this :
REST : localhost:8080/api/v1/students -- exposes json
VIEW : localhost:8080/students -- returns a view
hope this make clear ..

Spring MVC and Web Application separated

I have been Googling a lot lately, but I find myself coming up short on answers.
I have a complete Spring MVC application secured by Spring Security and with services exposing logic to controllers (controllers -> service -> repository -> POJO's).
Currently all my controllers, except the login controller, serve nothing but JSON/XML and I want it to stay this way. I do not want to render different views on each controller.
However, what I want to be able to, is to separate the web application from the backend because in time I want to be able to integration with my service using more platforms than just a web browser.
Currently I have the regular Spring MVC application:
Configuration
Domain(POJO's)
Repository
Service
Controller
Login is done using a Thymeleaf rendered view and Spring Security which does nothing more than filtering all urls under the application root. After this, it is just a bunch of static files being served as resources:
Spring Controllers send a "{ontrollerName}/layout" to serve the AngularJS HTML partial used for all data under that given Spring Controller.
What I want, is a way to separate everything in the /webapp directory from the rest of my project. I have looked into a few different solutions here such as using Apache Proxy where my Apache Http server hosts the client code which communicate with the backend using Ajax to rest controllers, and Tomcat hosting the backend. However, I hear that proxying like this is not safe. I could however be dead wrong here.
So, the questions I hope to receive answers to are:
1. Is it fine to just write a client that uses Apache http server's Proxy to provide access to Ajax on the server?
If not; How should I proceed? Is it any point in trying to extract the client side from the /webapp directory or is this just some stupid idea I created because it seemed cool to be able to deploy them without having to relay on each others?
Is there any best practices in regards to how I structure a project with separate modules? Think Gradle build scripts for multi modules.
Should I think in different terms and use an approach not based on Spring MVC at all? If so, please advice me.
I hope this was clear enough to make sense for you guys.
The solution was to use the simpler approach of a restful API that did authentication over OAuth 2.
Basically the two best alternatives are a hybrid solution (part restful API and part server side rendering of pages) or a full blown restful API where you implement all functionality in a static web client. This way there is no need for multi-module projects and packing things together in one package.

Resources