Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Last active July 21, 2020 08:57
Show Gist options
  • Save m-x-k/03a87252d2458010e1d69f37e35af6f1 to your computer and use it in GitHub Desktop.
Save m-x-k/03a87252d2458010e1d69f37e35af6f1 to your computer and use it in GitHub Desktop.
Spring Boot Interceptor with annotation support
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import static java.lang.String.format;
public class MyInterceptor extends HandlerInterceptorAdapter {
@Autowired
private MyRepository myRepository;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// Simple check to see if database object exists for requesting urls otherwise
// throw runtime exception to be picked up by handler for error response
Map pathVariables = getUrlPathVariables(request);
String myId = (String) pathVariables.get("myId"); // e.g. /rest/path/${myParam}
if (myId != null && myRepository.findById(myId) == null) {
throw new RuntimeException("My Object not found");
}
return true;
}
private Map getUrlPathVariables(HttpServletRequest request) {
return (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor());
}
@Bean
public MyInterceptor myInterceptor() {
return new myInterceptor();
}
}
@jpssasadara
Copy link

package com.epic.pos.posApp.Interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**

  • Created by oa on 6/3/2019.
    */
    @configuration
    public class WebMVCConfig extends WebMvcConfigurerAdapter {
    @OverRide
    public void addInterceptors(InterceptorRegistry registry) {

     /**@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Note @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      * There are two type of object creation 1) java Bean
      *                                       2) using new xxx();
      * but all dependency injection (@Autowired) and @Service/@Repository will work with java Bean, if we use these
      * annotations inside our Interceptor we should create its' object as java bean (Type 2) is hte example for that.
      * otherwise we can create interceptors object using new xxx() as (Type 1)
     @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
    
     // ########### Type 1 ############ Testing Interceptors ########################################################
     //##############################################################################################################
     registry.addInterceptor(new LogInterceptor())
             .addPathPatterns("/login/GetUserbyUsername/*")
             .addPathPatterns("/Testing")
             .excludePathPatterns("/secure-code/public"); //by default applies to
     registry.addInterceptor(new AuthInterceptor())
             .addPathPatterns("/auth");
     //##############################################################################################################
     //##############################################################################################################
    
     // ########### Type 2 ############ Real Interceptors ###########################################################
     //##############################################################################################################
     registry.addInterceptor(myInterceptor())
             .addPathPatterns("/ViewUserRole/SEARCH/*");
     //##############################################################################################################
     //##############################################################################################################
    

    }
    // $$$$$$$$$$$$$$$$ Type 2 bean for interceptor $$$$$$$$$$$$$$$$$$$$$$$$$$$
    @bean
    public PageTaskAuthInterceptor myInterceptor() {
    return new PageTaskAuthInterceptor();
    }
    //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment