Last active
July 21, 2020 08:57
-
-
Save m-x-k/03a87252d2458010e1d69f37e35af6f1 to your computer and use it in GitHub Desktop.
Spring Boot Interceptor with annotation support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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) {
}
// $$$$$$$$$$$$$$$$ Type 2 bean for interceptor $$$$$$$$$$$$$$$$$$$$$$$$$$$
@bean
public PageTaskAuthInterceptor myInterceptor() {
return new PageTaskAuthInterceptor();
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
}