【问题标题】:How can I get HttpServletRequest and HttpServletResponse object in Spring AOP如何在 Spring AOP 中获取 HttpServletRequest 和 HttpServletResponse 对象
【发布时间】:2015-07-13 16:59:19
【问题描述】:

我想在通知之前在 spring AOP 中获取响应对象。如果会话无效,我想重定向到登录页面,但无法在 Before 通知方法中获取 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

任何帮助将不胜感激。

【问题讨论】:

  • 不是自动装配,你试过这个吗? HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
  • 感谢您的回复。我会试试这个
  • 我需要 HttpServletResponse 对象。
  • 抱歉,误读了您的问题。
  • 使用更适合此任务的FilterHandlerInterceptor。或者使用 Spring Security,它可以为您处理更多事情。

标签: spring spring-mvc servlets spring-aop


【解决方案1】:

要获取响应对象,您可以使用以下代码:

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

获取请求对象:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getR‌equest();

如果您收到null 响应,那么我可以看到在返回控件时响应尚未形成。那么接下来的唯一方法就是使用interceptors

【讨论】:

  • 获取响应返回 null!
【解决方案2】:

基本上我们会从 jsp 页面重定向,即从 UI 层我们处理这种操作(重定向)。所以,我希望你会在你的应用程序中使用一些宁静的服务。对于大多数宁静的服务,我们选择异步请求。如果是异步服务和restful服务的结合;我相信你会在你的应用程序中使用它。如果您的会话无效并且您尝试访问对“会话”执行任何操作,那么它将使您进入“IllegalStateException”。对于此类场景,请遵循 JAX-RS 提供的以下集中式“异常处理”机制:javax.ws.rs.ext.ExceptionMapper。 请按照以下步骤操作: 步骤 1: 创建一个用户定义的未经检查的异常,例如 MyApplicationException:

public class MyApplicationException extends RuntimeException {
  public MyApplicationException() {super();}

  // implement other methods of RuntimeException as per your requirement
}

第 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.

就是这样,您完成了一个更精细的实现。保证...

【讨论】:

    【解决方案3】:

    您可以通过以下方法获得响应:

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

    【讨论】:

    • The method getResponse() is undefined for the type ServletRequestAttributes
    • 如何获取 HttpServletResponse 对象?
    【解决方案4】:
    /**
     * @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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2013-08-24
      相关资源
      最近更新 更多