在网关

时间:2016-05-31 18:51:31

标签: spring-security spring-boot spring-cloud-netflix

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

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

 }

}

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>

由于 拉维

2 个答案:

答案 0 :(得分:1)

看看这个。 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);
    }
}

答案 1 :(得分: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
相关问题