How can I get the HttpServletRequest and HttpServletResponse object in Spring AOP

I want to get the response object in spring AOP before the advice. If the session is invalid, I want to redirect to the login page, but could not get the HttpServletResponse object in the Before advice method.

I tried as follows.

@Autowired private HttpServletResponse response; public void setResponse(HttpServletResponse response) { this.response = response; } 

Stacktrace:

 caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.servlet.http.HttpServletResponse com.****.****.aspect.LogProvider.response; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.http.HttpServletResponse] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) ... 33 more 

Any help would be appreciated.

+6
source share
3 answers

Basically, we redirect from a jsp page, that is, from the user interface level, we process such an operation (redirection). Therefore, I hope that you will use some soothing services in your application. And for most other services we ask for an asynchronous request. If it is a combination of asynchronous and quiet services; and I'm sure that you will use this in your application. If your session is invalid and you are trying to access any operation on a "session", then it will drop you into an "IllegalStateException". For this type of scenario, please follow the centralized JAX-RS Exception Handling engine below : javax.ws.rs.ext.ExceptionMapper . Follow these steps: step-1: Create a custom exception thrown as MyApplicationException:

 public class MyApplicationException extends RuntimeException { public MyApplicationException() {super();} // implement other methods of RuntimeException as per your requirement } 

step-2: Create a custom type ExceptionMapper

 public class MyApplicationExceptionHandler implements ExceptionMapper<MyApplicationException> { @Override public Response toResponse(MyApplicationException exception) { return Response.status(Status.FORBIDDEN).entity(exception.getMessage()).build(); // set any Status code of 4XX as this is client side error not server side } } 

step-3: In all your ajax request in the UI code check this Status Code and redirect to the login page.

So that you can do it with a more subtle implementation. Guaranteed ...

+1
source

You can get the answer by the method:

 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse(); 
+1
source

To get the response object, you can use this code:

 ServletWebRequest servletWebRequest=new ServletWebRequest(request); HttpServletResponse response=servletWebRequest.getResponse(); 

To get the request object:

 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRโ€Œequest(); 

If you get a null response, then I see that the answer is not yet formed when the control returns. Then the only way forward is to go with interceptors .

-2
source

Source: https://habr.com/ru/post/986491/


All Articles