spring 特定 api 的引导拦截器不应为所有 api 调用
spring boot interceptor for specific api, should not be invoked for all the api's
2 api 暴露在 spring 引导控制器 class 中。我要求只拦截 1 api 并且不应该拦截其他 api。有人可以协助如何做到这一点吗?
下面是代码
public class HeaderValidationInterceptor extends HandlerInterceptorAdapter{
private static final Logger logger = Logger.getLogger(HeaderValidationInterceptor.class);
//before the actual handler will be executed
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {
validateHeaderParam(request);
request.setAttribute("startTime", startTime);
return true;
}
}
我还有一个配置class来添加拦截器如下
Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
HeaderValidationInterceptor headerValidationInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(headerValidationInterceptor)
}
}
控制器class
@RestController
public class MyController {
@Autowired
private ICityService cityService;
@GetMapping(value = "/cities")
public List<City> getCities() {
List<City> cities = cityService.findAll();
return cities;
}
@GetMapping(value = "/cities/{cityId}")
public City getCityById(@PathVariable("cityId") String cityId) {
City city = cityService.findCityById(cityId);
return cities;
}
}
在拦截器中,您可以检查要拦截的端点的请求 URI。
您可以使用正则表达式来匹配 URI。关注 /cities/{cityId}
端点。
if (request.getRequestURI().matches("(\/cities\/[a-zA-Z0-9]+\/?)")) {
validateHeaderParam(request);
request.setAttribute("startTime", startTime);
}
我不确定你的拦截器想要做什么,但对于你的例子,你也可以在你的控制器中做这件事。像这样,
@GetMapping(value = "/cities/{cityId}")
public City getCityById(@PathVariable("cityId") String cityId, HttpServletRequest request) {
// Here you can use HttpServletRequest and do your validation
// validateHeaderParam(request);
// request.setAttribute("startTime", startTime);
City city = cityService.findCityById(cityId);
return cities;
}
2 api 暴露在 spring 引导控制器 class 中。我要求只拦截 1 api 并且不应该拦截其他 api。有人可以协助如何做到这一点吗?
下面是代码
public class HeaderValidationInterceptor extends HandlerInterceptorAdapter{
private static final Logger logger = Logger.getLogger(HeaderValidationInterceptor.class);
//before the actual handler will be executed
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {
validateHeaderParam(request);
request.setAttribute("startTime", startTime);
return true;
}
}
我还有一个配置class来添加拦截器如下
Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
HeaderValidationInterceptor headerValidationInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(headerValidationInterceptor)
}
}
控制器class
@RestController
public class MyController {
@Autowired
private ICityService cityService;
@GetMapping(value = "/cities")
public List<City> getCities() {
List<City> cities = cityService.findAll();
return cities;
}
@GetMapping(value = "/cities/{cityId}")
public City getCityById(@PathVariable("cityId") String cityId) {
City city = cityService.findCityById(cityId);
return cities;
}
}
在拦截器中,您可以检查要拦截的端点的请求 URI。
您可以使用正则表达式来匹配 URI。关注 /cities/{cityId}
端点。
if (request.getRequestURI().matches("(\/cities\/[a-zA-Z0-9]+\/?)")) {
validateHeaderParam(request);
request.setAttribute("startTime", startTime);
}
我不确定你的拦截器想要做什么,但对于你的例子,你也可以在你的控制器中做这件事。像这样,
@GetMapping(value = "/cities/{cityId}")
public City getCityById(@PathVariable("cityId") String cityId, HttpServletRequest request) {
// Here you can use HttpServletRequest and do your validation
// validateHeaderParam(request);
// request.setAttribute("startTime", startTime);
City city = cityService.findCityById(cityId);
return cities;
}