当我在 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>
谢谢拉维