配置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