1

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

引导程序属性

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

 }

}

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

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

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

如何配置拦截器,使其先于其他任何东西执行

pom.xml

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>Angel.SR6</version>
</parent>

谢谢拉维

4

2 回答 2

2

看这个。 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);
    }
}
于 2016-12-22T09:30:17.670 回答
0

尝试如下修改 WebMvcconfigurator bean

@Bean
 public WebMvcConfigurerAdapter adapter() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new TestHandlerInterceptor( ()).addPathPatterns("/**")
        }
    };
 }
}

还将引导程序属性更改为:-

zuul.routes.registration-service.path=/registrations/**
zuul.routes.registration-service.service=registration-service
于 2016-06-01T06:17:46.743 回答