Spring Boot服务注入到doFiler

时间:2018-10-23 11:08:43

标签: java spring-boot

我正在尝试向Filer-

注入身份验证服务
@Autowired
AuthRequestService authService;

并以doFiler方法使用它-

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    System.out.println("SAPServiceFilter: "+ req.getRequestURI());
    //TODO - create auth sender
    authService.isAuthnticate((HttpServletRequest)request); //null
    chain.doFilter(request, response);
}

我的@SpringBootApplication类的子包中的过滤器类,并带有@service-

注释
@Service
public class AuthRequestService {

    @PostConstruct
    public void init() {
        System.out.println("AuthRequestService @PostConstruct");
    }

    public boolean isAuthnticate(HttpServletRequest request) {
        System.out.println("isAuthnticate");
        return true;
    }
}

使用-列出我所有的豆子时,该类也会出现

for (String name : applicationContext.getBeanDefinitionNames()) {
    System.out.println(name);
}

在调试authService为null时,仍然在FilterRegistrationBean中注册了过滤器的最后一件事-

FilterRegistrationBean<SAPServiceFilter> filterRegBean = new FilterRegistrationBean<>();
filterRegBean.setFilter(new SAPServiceFilter());

0 个答案:

没有答案