Spring boot handle request in different controller differentiate by incoming domain - java

Existing #RequestMapping annotation can only delegate request path to different controllers & methods but not checking request domain itself.
Is it possible to set spring boot to handle request from "www.domainA.com" & "www.domainB.com" to respond differently by using the different controller?
Thanks.

Browsers send the Domain in the HTTP Header "Host".
#RequestMapping.headers can be used to match only requests for a specific domain.

Related

Ignore context-path for specific endpoint in Spring boot

I have setup the property server.servlet.context-path=/xyz in the application.properties file so all my endpoint have a prefix of /xyz. Now, I want to create an endpoint that should not have /xyz as the prefix(as an exceptional scenario). How can I achieve that?
Technically it is not possible because spring boot has only one DispatcherServlet which is a front controller, if you want two different paths then you can use #RequestMapping annotation on two different controllers`
Still of you want two different context-paths then you should have two DispatcherServlet's

how spring handles Multiple login request as default scope is singleton?

How, Spring controllers handle multiple request ? As default scope of bean is singleton and when the application will deploy in server multiple user will try to access the landing page. As the default scope is singleton then how spring MVC handles request ?
From your application server, each thread will be created for serving your each http requests. Controller instance will be only one per server as being singleton.All the requests will share the same controller instance.

Spring Boot - webservice client define multiple endpoints/profiles

How do I define multiple endpoints/configurations for my webservice client?
At the moment I've defined the webservice endpoint within application.properties
uri=https://foo.bar/endpoint
username=foo
password=bar
But I need to add several endpoints with different properties (username, password as an example), similar to spring-boot profiles but I have to change them request scoped.
Is there a mechanism to autowire my service request-scoped and use different profiles?
Is there another mechanism?

Spring security filters interfering with threaded request handling?

The title for this question was difficult as we're having trouble even diagnosing the exact source of our issue. Allow me to begin by framing the structure of the application and then follow up with the issue we're experiencing.
The Setup
We have a decently large, multi-module Spring 4.x application using Spring Security. Spring security advocates the use of filters that check attributes on a request to determine if it should be rejected or handled by the request handler. We make use of these and our authentication works perfectly for single requests. However, our authentication is based on a number of data models from our internal services. To access these data models in the filter, we inject "service beans" (quotes because they're actually #Components but comprise our service layer) into the filter. One of our filters, the EarlyLoadingFilter retrieves several such models based on request parameters and then places those models in the request's attributes before continuing with the chain.
Subsequent filters in the chain retrieves these models from the request's attributes and use them to determine whether the user is authorized.
All of the "service beans" that are injected in this way are marked with the following annotations:
#Component
#Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "request")
The Problem
We're experiencing what appears to be some thread-safety issues when saturating our server with requests. During normal usage, it performs exactly as we expect it to. However, under load, when a high volume of requests are handled in a short period of time, we find that a given request may receive a response based on the preceding request's data. That is, it seems like request attributes are being retained between requests.
Now, I realize that servlet filters are singletons, but the only instance variables we rely on in any of our filters are the autowired beans for the services.
Are we perhaps overlooking some critical configuration of Spring or Spring Security? Is there a way to leverage Spring's HandlerInterceptors to achieve the same result with our authentication without mucking about in servlet filters? Are we completely off the mark with thinking this is thread or filter related?
Thanks in advance for any help you might be able to offer, this issue has become infuriating!

Spring Cloud: How to define default fallback for Hystrix in Zuul gateway?

I am using Spring Cloud Brixton.M3 and Spring Boot 1.3.0.RELEASE. I am sort of new in this (especially in Spring Cloud). I have created Registry Server (i.e. Eureka instance), Config server and Gateway.
As per my requirement I am intercepting each request hitting the gateway in one of my Filter to extract required information from Header and based upon that I am throwing exception or forwarding / verifying that request using Feign Client. Some time hystrix throw HystrixRuntimeException when it couldn't reach out to respective services or because of any other issues.
So What I want is:
Provide default fallback method for every forwarding request, so that I can read and process it accordingly.
Global Exception handling other than #ControllerAdvice since I am not providing any custom #HystrixCommand and Controller to call services (AOP based solution ?).
Is it possible to intercept every failed request and retry them for certain number of times ? Internally it might be happening but can I override this functionality and handle each failed request either because of TimedOutException or because of HttpConnectionPool exception ?
Update
Is it a good practice to provide own routing in Zuul gateway ? using #RestController and #HystrixCommand together ? (I think its a bad idea, because over the period of time we will end up with lots of controllers and hence actual use of intelligent routing wouldn't work as expected)
Currently there is an open issue for fallbacks with feign. There is also an open issue for fallbacks with zuul.

Categories