在网关中配置 zuul.routes 时未调用拦截器 [英] Interceptor not getting called when zuul.routes configured in gateway

查看:30
本文介绍了在网关中配置 zuul.routes 时未调用拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 bootstrap.properties 中配置 zuul 路由时,我在 gareway 应用程序中定义的 TestHandlerInterceptor 没有被所有匹配 /registrations 的请求调用,但它被所有请求调用其他请求 TestHandlerInterceptor.preHandle 正在成功处理

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

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 {

}
 }

网关应用程序.java

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);

 }

}

TestHandlerInterceptor.preHandle 为 localhost:1122/api/test 执行,但它没有被 localhost:1122/registrations 调用

The TestHandlerInterceptor.preHandle executes for localhost:1122/api/test but its not getting called for localhost:1122/registrations

我正在尝试在我的拦截器中添加一个 AuthenticationService ,它将在请求任何子资源之前执行所有无状态身份验证(使用 api 密钥).

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.

我尝试了 ZuulFilter 实现并且 MyZuulFilter.run() 是针对所有 localhost:1122/registrations 请求的调用,但不是针对 localhost:1122/api/test

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>

谢谢拉维

推荐答案

看看这个.HandlerInterceptorAdapter 和 Zuul 过滤器

@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);
    }
}

这篇关于在网关中配置 zuul.routes 时未调用拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆