Async request complete issue in Spring RequestMappingHandlerAdapter - spring

Why does RequestMappingHandlerAdapter call webRequest.requestCompleted(); even though the async request has not completed? Is that intentional? or is it a bug?

Related

Spring boot requestcontextholder with completableFuture.supplyAsync

I am developing an Api using spring boot. I need to call a dependent service asynchronously.
I have an interceptor which uses RequestContextHolder from spring to get the httpservletrequest. But as the calls to dependent service is happening ashnchronously, a new thread is created for executing the dependent call and requestContextHolder.getAttributes returns null for the requests started in new thread. Any suggestions to overcome this?
Code :
CompletableFuture.supplyAsyn(()-> dependentService.getSomething());
Interceptor:
HttpServletRequest request = (ServletRequest) (RequestContextHolder.getAttributes()).getRequest();
Here RequestContextHolder.getAttributes() returns null for the requests happening in new threads.Anyway to hold the requestcontext ?

Spring Boot HandlerInterceptor not triggered when a request is authenticated

As described in the title, the interceptor is only triggered if the request does not contain the required auth token. With the token, the interceptor does not get triggerd...
How can this be prevented so that the interceptor gets triggerd on all requests?
You need to set the order property to the lowest.
Maybe #Order(Ordered.LOWEST_PRECEDENCE) might do the job.
If not, check this out: https://kreepcode.com/spring-boot-define-execution-order-multiple-interceptors/

How can I test a secured endpoint with Awaitility in Spring boot?

I'm using spring boot and I want to assert an asynchronous side effect by calling a secured endpoint with MockMvc.
I have been using Awaitility, but apparently the mocked security context is lost when executing in a different thread.
I couldn't find a way of passing the context, I tried with SecurityContextHolder.setContext() but it didn't work, I guess spring's MockMvc stores the context in a different way.
#Test
#WithMockUser(authorities = "admin", username = "user")
void shouldRunSideEffectAsync() throws Exception {
mockMvc.perform(post("/foo")).andExpect(status().isAccepted());
await()
.atMost(TIMEOUT)
.untilAsserted(() -> mockMvc.perform(get("/foo")).andExpect(status().isOk()));
}
The GET would return 404 for a while and then 200 when the async task is completed. However this will always return 403 as the MockUser info is lost.
How can I solve this?
You almost got it. Security for MockMvc is implemented by TestSecurityContextHolderPostProcessor, which uses the TestSecurityContextHolder to set/get the security context. That is just a wrapper around the SecurityContextHolder.
So you can use TestSecurityContextHolder.setContext() in the awaitility thread and it should work.

Spring async controller method with DeferredResult and logback-access for full request/response logging

Spring async controller method with DeferredResult and logback-access for full request/response logging. The problem is that for synchronous methods for example that return simple String request body is written into the log file but for methods that return Asynchronously logback-access fails to print response body. BTW, in last case headers get printed.
Logback-access is installed on container(Tomcat 8) level.
Let me know if I can provide more details

How to read request in an Interceptor in Spring

I can't use getReader() because in some other class getReader() is already used and the logic can't be changed.
How can I read it from interceptor?
I need to persist request, response and status from interceptor. So for this, I need to access request and response.
Trying to call service from preHandle()

Resources