Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 审核/记录所有传入web请求到@RequestMapping注释方法_Java_Spring_Spring Mvc - Fatal编程技术网

Java 审核/记录所有传入web请求到@RequestMapping注释方法

Java 审核/记录所有传入web请求到@RequestMapping注释方法,java,spring,spring-mvc,Java,Spring,Spring Mvc,来源 我有用@Controller注释的控制器类,它们有用@RequestMapping注释的方法我的任务是审核控制器类接收到的所有web请求到@RequestMapping方法,我使用UI上的datatables发送和接收控制器的响应。审计框架已经到位 该项目是在Java配置中配置的 我不知道如何着手完成这项工作 // Configure Interceptor public class WebConfig extends WebMvcConfigurerAdapter { @Ov

来源

我有用@Controller注释的控制器类,它们有用@RequestMapping注释的方法我的任务是审核控制器类接收到的所有web请求到@RequestMapping方法,我使用UI上的datatables发送和接收控制器的响应。审计框架已经到位

该项目是在Java配置中配置的

我不知道如何着手完成这项工作

// Configure Interceptor

public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new MyInterceptor());
}

public @Bean
RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
    handlerMapping.setAlwaysUseFullPath(true);
    handlerMapping.setUseSuffixPatternMatch(false);
    return handlerMapping;
  }
}

//Add Handler
@Component
public class MyInterceptor extends HandlerInterceptorAdapter {
@Inject RequestMappingHandlerMapping requestMappingHandlerMapping;
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
// do stuff - ( Does my Audit Code go here? eg: Logger.info("xyz request"))
return true;
 }
}
我在想这样的办法行得通

对此有何建议,以及
如果使用侦听器或其他拦截器更容易,那么使用您可以完全访问的拦截器将很有帮助,它提供了对方法参数、方法返回值、方法注释等的方便访问

下面的示例截获并记录映射的请求

class WebMvcConfig extends WebMvcConfigurerAdapter {

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(new HandlerInterceptorAdapter() {
         Logger logger = LoggerFactory.getLogger(WebMvcConfig.class);

         @Override
         public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            if (handler instanceof HandlerMethod) {
               HandlerMethod handlerMethod = (HandlerMethod) handler;
               Method method = handlerMethod.getMethod();
               logger.info("{} - {} - method '{}' on controller '{}'",
                     request.getMethod(), request.getRequestURI(), method.getName(),
                     handlerMethod.getBean().getClass()
               );
            }
            return true;
         }
      });
   }
}
它返回true以继续执行链(并将请求转发给其他拦截器或控制器方法本身)

日志输出示例如下所示:


GET-/greeting-controller“class hello.GreetingController”上的方法“greeting”

@Betlista你能给我举个例子吗?效果很好。我可以进一步筛选要记录的映射请求吗?使用通配符或任何其他方式?或者通过避免一些控制器类?看看javadoc。您可以添加注册的拦截器应该应用到的路径模式。如果这还不够,您可以在定制的
HandlerInterceptor
实现中实现一个符合您需要的自定义include/exclude过滤器。由于您可以访问
HandlerMethod
,因此您可以访问几乎所有的内容—甚至可以引入您自己的方法—或类级注释来标记方法/控制器。