问题
When I configure zuul routes in bootstrap.properties, my TestHandlerInterceptor defined in gareway application is not getting invoked for all the request matching /registrations but it gets called for all other request TestHandlerInterceptor.preHandle is getting processed successfully
bootstrap.properties
zuul.routes.registration-service.path=**registrations**
zuul.routes.registration-service.service=registration-service
TestHandlerInterceptor.java
public class TestHandlerInterceptor implements HandlerInterceptor {
@Autowired
AuthenticationService authenticationService;
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println(" ********* preHandle ********");
boolean result = true;
if(!authenticationService.isAuthenticated(httpServletRequest)){
result = false;
httpServletResponse.setStatus(401);
}
return result;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
GatewayApplication.java
@EnableZuulProxy
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication{
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public WebMvcConfigurerAdapter adapter() {
return new WebMvcConfigurerAdapter() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TestHandlerInterceptor());
super.addInterceptors(registry);
}
};
}
}
@RestController
class Orchestration {
@LoadBalanced
@Autowired
private RestTemplate restTemplate ;
@RequestMapping("/api/test")
public @ResponseBody
Collection<Registration> getRegistrations(){
ResponseEntity<Resources<Client>> resourceResponseEntity = this.restTemplate.exchange("http://registration-service/registrations", HttpMethod.GET,null, ptr);
}
}
The TestHandlerInterceptor.preHandle executes for localhost:1122/api/test but its not getting called for localhost:1122/registrations
I am trying to add a AuthenticationService in my interceptor which will perform all stateless Authentication(using api keys) before any of the sub resource is requested.
I tried ZuulFilter implementation and the MyZuulFilter.run() is call for all the localhost:1122/registrations request but not for localhost:1122/api/test
How can I configure the interceptor in a way that its gets executed before anything else
pom.xml
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Angel.SR6</version>
</parent>
Thanks Ravi
回答1:
look at this. HandlerInterceptorAdapter and Zuul Filter
@Configuration
@RequiredArgsConstructor
public class ZuulHandlerBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
private MyInterceptor myInterceptor = new MyInterceptor();
@Override
public boolean postProcessAfterInstantiation(final Object bean, final String beanName) throws BeansException {
if (bean instanceof ZuulHandlerMapping) {
ZuulHandlerMapping zuulHandlerMapping = (ZuulHandlerMapping) bean;
Object[] objects = {oauth2Interceptor};
zuulHandlerMapping.setInterceptors(objects);
}
return super.postProcessAfterInstantiation(bean, beanName);
}
}
回答2:
Try modifying WebMvcconfigurator bean as below
@Bean
public WebMvcConfigurerAdapter adapter() {
return new WebMvcConfigurerAdapter() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TestHandlerInterceptor( ()).addPathPatterns("/**")
}
};
}
}
Also change the bootstrap properties to:-
zuul.routes.registration-service.path=/registrations/**
zuul.routes.registration-service.service=registration-service
来源:https://stackoverflow.com/questions/37553487/interceptor-not-getting-called-when-zuul-routes-configured-in-gateway