{"id":2021,"date":"2025-03-24T16:20:29","date_gmt":"2025-03-24T08:20:29","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=2021"},"modified":"2025-03-24T16:20:29","modified_gmt":"2025-03-24T08:20:29","slug":"review-interceptors","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=2021","title":{"rendered":"review interceptors"},"content":{"rendered":"<p>In a Spring Boot project, interceptors are typically implemented using <code>HandlerInterceptor<\/code> or <code>HandlerInterceptorAdapter<\/code> (deprecated since Spring 5.3). These interceptors allow you to intercept HTTP requests before they reach the controller.<\/p>\n<h3><strong>1. Checking How Many Interceptors Are Registered<\/strong><\/h3>\n<p>To see all the interceptors in your Spring Boot application, you can do the following:<\/p>\n<h4><strong>Approach 1: Print Registered Interceptors in <code>WebMvcConfigurer<\/code><\/strong><\/h4>\n<p>If interceptors are registered via <code>WebMvcConfigurer<\/code>, you can list them by overriding <code>addInterceptors<\/code> method:<\/p>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.web.servlet.HandlerInterceptor;\nimport org.springframework.web.servlet.config.annotation.InterceptorRegistry;\nimport org.springframework.web.servlet.config.annotation.WebMvcConfigurer;\n\nimport java.util.List;\n\n@Configuration\npublic class InterceptorConfig implements WebMvcConfigurer {\n\n    @Autowired(required = false) \/\/ To avoid issues if no interceptors are present\n    private List&lt;HandlerInterceptor&gt; interceptors;\n\n    @Override\n    public void addInterceptors(InterceptorRegistry registry) {\n        if (interceptors != null) {\n            System.out.println(&quot;Registered Interceptors:&quot;);\n            for (HandlerInterceptor interceptor : interceptors) {\n                System.out.println(interceptor.getClass().getName());\n            }\n        }\n    }\n}<\/code><\/pre>\n<h4><strong>Approach 2: Debugging with Breakpoints<\/strong><\/h4>\n<p>You can set breakpoints inside:<\/p>\n<ul>\n<li><code>WebMvcConfigurer#addInterceptors<\/code><\/li>\n<li><code>HandlerInterceptor#preHandle<\/code><\/li>\n<li><code>HandlerInterceptor#postHandle<\/code><\/li>\n<li><code>HandlerInterceptor#afterCompletion<\/code><\/li>\n<\/ul>\n<p>Then, run your application and inspect the interceptor chain when requests are processed.<\/p>\n<hr \/>\n<h3><strong>2. Checking Where and How an Interceptor Works<\/strong><\/h3>\n<p>Once you identify the interceptors, you can analyze how they work by looking at their implementations. Here\u2019s how interceptors typically work:<\/p>\n<h4><strong>A Sample Custom Interceptor<\/strong><\/h4>\n<pre><code class=\"language-java\">import javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\nimport org.springframework.stereotype.Component;\nimport org.springframework.web.servlet.HandlerInterceptor;\n\n@Component\npublic class LoggingInterceptor implements HandlerInterceptor {\n\n    @Override\n    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {\n        System.out.println(&quot;Before handler execution: &quot; + request.getRequestURI());\n        return true; \/\/ Continue request processing\n    }\n\n    @Override\n    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,\n                           org.springframework.web.servlet.ModelAndView modelAndView) {\n        System.out.println(&quot;After handler execution: &quot; + request.getRequestURI());\n    }\n\n    @Override\n    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {\n        System.out.println(&quot;Request completed: &quot; + request.getRequestURI());\n    }\n}<\/code><\/pre>\n<h4><strong>Registering the Interceptor<\/strong><\/h4>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.web.servlet.config.annotation.InterceptorRegistry;\nimport org.springframework.web.servlet.config.annotation.WebMvcConfigurer;\n\n@Configuration\npublic class WebConfig implements WebMvcConfigurer {\n\n    @Autowired\n    private LoggingInterceptor loggingInterceptor;\n\n    @Override\n    public void addInterceptors(InterceptorRegistry registry) {\n        registry.addInterceptor(loggingInterceptor).addPathPatterns(&quot;\/**&quot;);\n    }\n}<\/code><\/pre>\n<hr \/>\n<h3><strong>3. Dynamically Inspecting Interceptors<\/strong><\/h3>\n<p>If you want to check interceptors at runtime, you can use:<\/p>\n<pre><code class=\"language-java\">import org.springframework.web.servlet.HandlerExecutionChain;\nimport org.springframework.web.servlet.HandlerMapping;\nimport org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;\nimport org.springframework.web.context.WebApplicationContext;\nimport org.springframework.stereotype.Component;\n\nimport javax.annotation.PostConstruct;\nimport java.util.List;\nimport java.util.Map;\n\n@Component\npublic class InterceptorInspector {\n\n    private final WebApplicationContext context;\n\n    public InterceptorInspector(WebApplicationContext context) {\n        this.context = context;\n    }\n\n    @PostConstruct\n    public void printInterceptors() throws Exception {\n        RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);\n        Map&lt;String, HandlerMapping&gt; handlerMappings = context.getBeansOfType(HandlerMapping.class);\n\n        for (Map.Entry&lt;String, HandlerMapping&gt; entry : handlerMappings.entrySet()) {\n            HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest(&quot;GET&quot;, &quot;\/&quot;));\n            if (chain != null) {\n                System.out.println(&quot;Interceptors for &quot; + entry.getKey() + &quot;:&quot;);\n                for (var interceptor : chain.getInterceptors()) {\n                    System.out.println(interceptor.getClass().getName());\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>This will list all interceptors applied to different routes in your Spring Boot application.<\/p>\n<hr \/>\n<h3><strong>4. Using Spring Boot Actuator (For Monitoring)<\/strong><\/h3>\n<p>If you have <code>spring-boot-starter-actuator<\/code> dependency, you can inspect handler mappings (including interceptors):<\/p>\n<ol>\n<li>\n<p><strong>Add dependency<\/strong><\/p>\n<pre><code class=\"language-xml\"><dependency>\n   <groupId>org.springframework.boot<\/groupId>\n   <artifactId>spring-boot-starter-actuator<\/artifactId>\n<\/dependency><\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Enable Actuator Endpoint in <code>application.properties<\/code><\/strong><\/p>\n<pre><code class=\"language-properties\">management.endpoints.web.exposure.include=*<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Check Mappings via HTTP Request<\/strong><\/p>\n<pre><code>GET http:\/\/localhost:8080\/actuator\/mappings<\/code><\/pre>\n<p>This will return all handler mappings, including the interceptors.<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3><strong>Summary<\/strong><\/h3>\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>How it helps?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Logging in <code>addInterceptors<\/code><\/strong><\/td>\n<td>Prints interceptors when they are registered.<\/td>\n<\/tr>\n<tr>\n<td><strong>Breakpoints in Interceptor methods<\/strong><\/td>\n<td>Debugs execution flow.<\/td>\n<\/tr>\n<tr>\n<td><strong>Runtime Inspection (<code>HandlerExecutionChain<\/code>)<\/strong><\/td>\n<td>Lists interceptors for specific routes.<\/td>\n<\/tr>\n<tr>\n<td><strong>Spring Boot Actuator (<code>\/actuator\/mappings<\/code>)<\/strong><\/td>\n<td>Provides all request mappings and interceptors.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Would you like more details on any specific aspect? \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a Spring Boot project, interceptors are typically implemented using HandlerInterceptor or HandlerInterceptorAdapter (deprecated since Spring 5.3). These interceptors allow you to intercept HTTP requests before they reach the controller. 1. Checking How Many Interceptors Are Registered To see all the interceptors in your Spring Boot application, you can do the following: Approach 1: Print [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=2021\" title=\"Permanent Link to: review interceptors\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2021","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2021"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2021"}],"version-history":[{"count":1,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2021\/revisions"}],"predecessor-version":[{"id":2022,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2021\/revisions\/2022"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}