配置zuul.routes当我配置bootstrap.properties,我TestHandlerInterceptor在gareway应用程序定义是没有得到调用匹配/注册的所有请求zuul路线,但它被调用不获取调用所有其它请求TestHandlerInterceptor.preHandle是越来越成功处理拦截当网关
bootstrap.properties
zuul.routes.registration-service.path=**registrations**
zuul.routes.registration-service.service=registration-service
TestHandlerInterce ptor.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执行本地主机:1122/API /测试,但它没有得到所谓的本地主机:1122 /注册
我试图在我的拦截器中添加一个AuthenticationService,它将在请求任何子资源之前执行所有无状态身份验证(使用api密钥)。
我试图ZuulFilter实现和MyZuulFilter.run()是呼吁所有本地主机:1122 /注册请求,但不是本地主机:1122/API /测试
如何配置拦截的方式,它被别的
的pom.xml
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Angel.SR6</version>
</parent>
感谢 拉维
添加addPathPatterns(“/ **”)没有给预期的结果..可能是我做错了什么 – Ravi
更新的答案,也许你需要改变的路径模式。你能否通过zuul登录注册服务? –
已经有zuul.routes.registration-service.path = **注册** zuul.routes.registration-service.service =引导程序中的注册服务。我可以通过zuul打这个服务,但是我的拦截器没有被调用。现在我已经在ZuulFilter.run()中重复了我的身份验证(使用api键)代码。现在代码存在两个地方Filter和Interceptor,我试图避免 – Ravi