Spring's Interceptor works based on URI matching.
However, when developing a Restful API server, there are times when you need to use different HTTP methods for the same uri.
Example:
GET /memos
POST /memos
If you create an Interceptor to apply User authentication only to POST /memos
, you would perform the following
procedures.
- Create an Interceptor.
- Register the Interceptor for
/memos
in theaddInterceptor()
of theWebMvcConfigurer
implementation. - Implement it so that
GET /memos
passes in thepreHandle()
of the Interceptor.
like this:
public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getMethod().equals("GET") && request.getRequestURI().equals("/memos")) {
return true;
}
// authentication logic
return true;
}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/memos");
}
}
I thought this method would increase the complexity in a Restful application. So I created a RestInterceptor library that matches based on URI and HTTP Method rather than URI-based matching.
repositories {
...
maven { url "https://jitpack.io" }
}
dependencies {
...
implementation 'com.github.Dh3356:rest_interceptor:v{version}'
// example: implementation 'com.github.Dh3356:rest_interceptor:v0.1'
}
@Slf4j
@Component
public class RestTestInterceptor extends RestInterceptor {
@Override
protected boolean doInternal(HttpServletRequest request, HttpServletResponse response, Object handler) {
log.info("Hello, world!");
return true;
}
}
@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {
private final RestTestInterceptor testInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// Create RestInterceptorRegistry from InterceptorRegistry
RestInterceptorRegistry restInterceptorRegistry = new RestInterceptorRegistry(registry);
restInterceptorRegistry.addInterceptor(testInterceptor)
.addRestfulPatterns(RestfulPattern.of("/memos", HttpMethod.GET)); // GET /memos
restInterceptorRegistry.build(); // Deprecated Since v1.0
}
}