如何在 Spring AOP 中获取 HttpServletRequest 和 HttpServletResponse 对象
How can I get HttpServletRequest and HttpServletResponse object in Spring AOP
我想在 spring AOP 通知之前获取响应对象。如果会话无效我想重定向到登录页面,但无法在 Before advice 方法中获取 HttpServletResponse 对象。
尝试了以下方法
@Autowired
private HttpServletResponse response;
public void setResponse(HttpServletResponse response) {
this.response = response;
}
堆栈跟踪:
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
任何帮助将不胜感激。
要获取响应对象,您可以使用此代码:
ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse();
获取请求对象:
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
如果您收到 null
响应,那么我可以看到在返回控件时尚未形成响应。那么唯一的方法就是使用 interceptors
.
基本上我们从 jsp 页面进行重定向,即从 UI 层我们处理这种操作(重定向)。所以,我希望您将在您的应用程序中使用一些 restful 服务。对于大多数 restful 服务,我们使用异步请求。如果是异步和restful服务的组合;我相信您会在您的应用程序中使用它。如果您的会话无效并且您尝试访问对 'session' 执行任何操作,那么它将使您进入 'IllegalStateException'。对于此类场景,请遵循 JAX-RS 提供的以下集中 'Exception Handling' 机制:javax.ws.rs.ext.ExceptionMapper。
请按照以下步骤操作:
step-1: 创建一个用户定义的未经检查的异常,如 MyApplicationException:
public class MyApplicationException extends RuntimeException {
public MyApplicationException() {super();}
// implement other methods of RuntimeException as per your requirement
}
step-2:创建自定义类型的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
}
}
第三步: In all your ajax request in the UI code check this Status Code and redirect to the login page.
就是这样,您已经完成了更精细的实施。保证...
您可以通过以下方法获得回复:
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
/**
* @return the HttpServletResponse handled by the current thread
*/
public static Optional<HttpServletResponse> getThreadLocalResponse() {
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
.filter(ra -> ra instanceof ServletRequestAttributes)
.map(ServletRequestAttributes.class::cast)
.map(ServletRequestAttributes::getResponse);
}
我想在 spring AOP 通知之前获取响应对象。如果会话无效我想重定向到登录页面,但无法在 Before advice 方法中获取 HttpServletResponse 对象。
尝试了以下方法
@Autowired
private HttpServletResponse response;
public void setResponse(HttpServletResponse response) {
this.response = response;
}
堆栈跟踪:
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
任何帮助将不胜感激。
要获取响应对象,您可以使用此代码:
ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse();
获取请求对象:
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
如果您收到 null
响应,那么我可以看到在返回控件时尚未形成响应。那么唯一的方法就是使用 interceptors
.
基本上我们从 jsp 页面进行重定向,即从 UI 层我们处理这种操作(重定向)。所以,我希望您将在您的应用程序中使用一些 restful 服务。对于大多数 restful 服务,我们使用异步请求。如果是异步和restful服务的组合;我相信您会在您的应用程序中使用它。如果您的会话无效并且您尝试访问对 'session' 执行任何操作,那么它将使您进入 'IllegalStateException'。对于此类场景,请遵循 JAX-RS 提供的以下集中 'Exception Handling' 机制:javax.ws.rs.ext.ExceptionMapper。 请按照以下步骤操作: step-1: 创建一个用户定义的未经检查的异常,如 MyApplicationException:
public class MyApplicationException extends RuntimeException {
public MyApplicationException() {super();}
// implement other methods of RuntimeException as per your requirement
}
step-2:创建自定义类型的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
}
}
第三步: In all your ajax request in the UI code check this Status Code and redirect to the login page.
就是这样,您已经完成了更精细的实施。保证...
您可以通过以下方法获得回复:
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
/**
* @return the HttpServletResponse handled by the current thread
*/
public static Optional<HttpServletResponse> getThreadLocalResponse() {
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
.filter(ra -> ra instanceof ServletRequestAttributes)
.map(ServletRequestAttributes.class::cast)
.map(ServletRequestAttributes::getResponse);
}