我想在Spring Boot Rest API项目上实现过滤器,但要注意一些如何不调用过滤器的方法。我为此添加了以下实现。
public class AutorizationFilter implements Filter{
Logger log = LoggerFactory.getLogger(AutorizationFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("**** Start ****");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
log.info("**** doFilter ****");
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("**** end ****");
}}
init 和 destroy 方法有效,但 doFilter 方法无法调用。
@Configuration
public class RestApiConfig {
@Bean
public FilterRegistrationBean<AutorizationFilter> filterRegistrationBean(){
FilterRegistrationBean<AutorizationFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new AutorizationFilter());
registrationBean.addUrlPatterns("/**");
return registrationBean;
}}
我的控制器如下:
@RestController
@RequestMapping("/home")
public class HomeController {
@RequestMapping(value="/hello",method=RequestMethod.GET)
public ResponseEntity<Object> hello() {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("data","called home controller");
return new ResponseEntity<>(resultMap,HttpStatus.OK);
}
}
任何人都可以帮助我,为什么 dofilter 方法不起作用。