How to map tenant request to the root page of application in spring boot using spring webflux webfilter

155 Views Asked by At

I am using spring webflux and webfilter in my spring boot application and i'm bundling angular and spring boot into a single jar file and i am able to access angular at "/".

My use case is i want to support multi-tenant and when i access my application using "localhost:8080/tenant/tenantname/" it is considered as new tenant with name "tenantname" and i need to open the home page of angular which is at "/" by keeping the uri in the search bar as "localhost:8080/tenant/tenantname/".

So to achieve it i have implemented a webfilter and modifying the path to "/" if the uri has "tenant", but when we send the modified uri to chain.filter(), the uri in the search box is getting to "localhost:8080/" i dont want it happen i want the uri in search box should be same "localhost:8080/tenant/tenantname/".

@Component
public class TenantFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        String uri = exchange.getRequest().getURI().getPath();
        ServerWebExchange updatedExchange = exchange;
        if(uri.contains("tenant")){
             updatedExchange = exchange.mutate().request(request->{
                request.path("/");
            }).build();
        }
        return chain.filter(updatedExchange);
    }
}

In my old POC i have use servlet filter it was working fine there.

Servlet filter example:

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String requestURI = httpRequest.getRequestURI();
        if(requestURI.contains("/tenant")){
            request.getRequestDispatcher(URLDecoder.decode(uri, "UTF-8")).forward(request, response);
        }
        chain.doFilter(request, response);
    }

in servlet filter i have use request.getRequestDispatcher().forward(request,response). is there anything same in spring webflux webfilter or do i need to change anything in my webFlux WebFilter.

1

There are 1 best solutions below

1
On

Related to: Spring webflux: redirect http to https

A better solution than changing the direction of the request internally.

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        URI uri = exchange.getRequest().getURI();

        if (uri.getPath().contains("tenant")) {
            try {
                URI mutatedUri = new URI(
                    uri.getScheme(),
                    uri.getUserInfo(),
                    uri.getHost(),
                    uri.getPort(),
                    "/",
                    uri.getQuery(),
                    uri.getFragment()
                );
                ServerHttpResponse response = exchange.getResponse();
                response.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
                response.getHeaders().setLocation(mutatedUri);
                return Mono.empty();
            } catch (URISyntaxException e) {
                return Mono.error(new IllegalStateException(e.getMessage(), e));
            }
        }

        return chain.filter(exchange);
    }