Spring security 在网关中配置zuul.routes时未调用侦听器

Spring security 在网关中配置zuul.routes时未调用侦听器,spring-security,spring-boot,spring-cloud-netflix,Spring Security,Spring Boot,Spring Cloud Netflix,当我在bootstrap.properties中配置zuul路由时,Garway应用程序中定义的TestHandlerInterceptor不会为所有匹配/registrations的请求调用,但会为所有其他请求调用它TestHandlerInterceptor.preHandle正在成功处理 bootstrap.properties TestHandlerInterceptor.java public class TestHandlerInterceptor implements Handle

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

bootstrap.properties

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

 }

}
@EnableZuulProxy
@SpringBoot应用程序
@EnableDiscoveryClient
公共类网关应用程序{
公共静态void main(字符串[]args){
run(GatewayApplication.class,args);
}
@豆子
公共WebMVCConfigureAdapter适配器(){
返回新的WebMVCConfigureAdapter(){
@凌驾
公共无效附加接收器(侦听器注册表){
addInterceptor(新的TestHandlerInterceptor());
超级接收器(注册表);
}
};
}
}
@RestController
类编排{
@负载平衡
@自动连线
私有RestTemplate RestTemplate;
@请求映射(“/api/test”)
公共@ResponseBody
集合getRegistrations(){
ResponseEntity resourceResponseEntity=此.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>

org.springframework.cloud
SpringCloudStarter父级
Angel.SR6
谢谢
Ravi

尝试修改webmvcconfiguratorbean,如下所示

@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
看看这个。


添加addPathPatterns(“/**”)未给出预期结果。。可能是我做错了什么更新了答案也许你需要改变路径模式。您是否能够通过zuul访问注册服务。已经有zuul.routes.registration service.path=**注册**zuul.routes.registration service.service=引导中的注册服务。我可以通过zuul访问该服务,但我的拦截器没有被调用。目前,我已经在ZuulFilter.run()中重复了身份验证(使用api密钥)代码。现在代码存在于我试图避免的两个过滤器和拦截器中
zuul.routes.registration-service.path=/registrations/**
zuul.routes.registration-service.service=registration-service
@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);
    }
}