Multi-tenant Service using Spring webflux - spring-boot

I am trying to implement multi-tenant service using Spring web flux.I have read examples of using AbstractRoutingDataSource and ThreadLocal TenantContext to populate the tenantId for mapping datasource cofigurations. However with Web Flux we cannot use ThreadLocal and therefore this solution.
With WebFlux, I can read the tenant configuration which is a part of my User Principle -
ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication)
.map(Authentication::getPrincipal).cast(UserPrincipal.class).flatMap(user -> {
TenantContext.setCurrentTenant(user.getTenantID().toString())
However I am not sure how to not use ThreadLocal. Can I use SubscriberContext for performing the same action?
Any example or help would be much apprecited.
Thanks

Related

Spring boot stateless security with redis cache data

While doing some practice examples to learn spring security, I come up with some following use case. But I did not find best possible approach using spring security. Could some one please help me on this
I have angularJs application, Spring boot application running on different servers . In redis cache I have user info(role, and some other info) with gsid as key. In each rest call I am passing gsid as cookie. Now I want to validate each request in the Spring security Filter by fetching user info from the redis cache before sending to #Restcontroller.
what could be best approach to authenticate and authorize the request using spring boot security.
Use spring-session with redis, it also provides integration with spring-security.

spring security oauth2 manually generate authcode

HI I am implementing oauth2 using spring security. This application will be deployed in a multi-node clustered environment. How spring will synchronize authorization code between multiple nodes? It can be achieved via jdbcAuthorizationCodeServices but I can't use relational DB. Requirement is to use NoSql DB. Is there a way to add custom plug-in custom authorization code generator which will be used by spring to create and consume auth code (Code example will be really helpful)?
Thanks

Spring JPA change datasource depending on request header

I developed an application with spring-data-rest.
I love it and it works like a charme.
What I want to do (to implement one backend for multiple customers) is to change the datasource of my repository depending on an apikey which is sent in a custom request header.
The connection info (url, credentials, database) can be retrieved from an external microservice which manages all the database configurations.
The idea is to retrieve all available database connections on startup and store them in a map with apikey as key and the connection info as value.
I´m not clear about how I can change the datasource of my repo for each incoming request at runtime.
Any ideas?
Depending on your JPA provider, what you are wanting will be multi-tenancy support.
For Hibernate, there is a nice multi-tenancy API available that plugs in with Spring very nicely for configuring what data source to use. MultiTenantConnectionProvider and CurrentTenantIdentifierResolver for some API details.
I finally found a solution using the AbstractRoutingDataSource.
This article saved my day. A really easy to use and to understand solution.
http://fizzylogic.nl/2016/01/24/Make-your-Spring-boot-application-multi-tenant-aware-in-2-steps/

Getting Spring Session information from Spring Actuator

I have a spring cloud based application that stores the spring session in redis. It is using Spring Boot with embedded tomcat.
When using spring actuator, the /metrics endpoint does not return any valuable information. httpsessions.active is set to 0, and httpsessions.max is set to -1.
I am guessing this is because spring replaces the httpsession implementation with its own spring session implementation.
Is there a way to access this information from some endpoint? preferably using JMX but not mandatory.
Thanks.
I eventually used the following solution, by using a generic username. The endpoint would return all sessions, a simple count would give the total.
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/findbyusername.html

Filter to track time spent by an user in each page in a spring application

I am working on a spring application in which I want to track the users time spent on each page. I am planning to do it via Spring filters. Is there a spring filter that intercepts each and every page request?
Or if you know any better ways of doing or an API that already does this which I can plug-in into my application - please suggest.
There is the Spring org.springframework.web.servlet.HandlerInterceptor interface. It is a normal bean with an special interface.
Read the Spring reference chapter 15.4.1 Intercepting requests - the HandlerInterceptor interface
The other (spring independend) way would be implmenting a servelt filter, but there you do not have spring support for example for easy access to other beans.

Resources