Skip to content

Commit

Permalink
Refactor Interceptors
Browse files Browse the repository at this point in the history
- Filter to swagger
- Add Token Interceptor
  • Loading branch information
edysegura committed May 19, 2020
1 parent 24d94a2 commit 6e10fb8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/main/java/br/com/spring/restful/config/InterceptorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import br.com.spring.restful.interceptor.CounterInterceptor;
import br.com.spring.restful.interceptor.TokenInterceptor;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

@Autowired
private TokenInterceptor tokenInterceptor;
private CounterInterceptor counterInterceptor;

@Autowired
public InterceptorConfig(
TokenInterceptor tokenInterceptor,
CounterInterceptor counterInterceptor
) {
this.tokenInterceptor = tokenInterceptor;
this.counterInterceptor = counterInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(tokenInterceptor);
registry.addInterceptor(counterInterceptor);
// TODO add token interceptor
// TODO exception handler
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public class CounterInterceptor implements HandlerInterceptor {
TriggerCounter triggerCounter;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
triggerCounter.increment(request.getMethod() + ":" + request.getRequestURI());
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

System.out.println("||----------> API Counter");

String api = request.getMethod() + ":" + request.getRequestURI();
triggerCounter.increment(api);

return HandlerInterceptor.super.preHandle(request, response, handler);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package br.com.spring.restful.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class TokenInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

System.out.println("||----------> Token authorization validation");

if (!request.getRequestURI().contains("swagger") && StringUtils.isEmpty(request.getHeader("Authorization"))) {
throw new Exception("Could not check the authorization");
}

return HandlerInterceptor.super.preHandle(request, response, handler);
}

}

0 comments on commit 6e10fb8

Please sign in to comment.