在我的web.xml中,我创建了以下过滤器:
<filter>
<filter-name>tokenAuthenticationFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>tokenAuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我创建了以下组件:
@Component("tokenAuthenticationFilter")
public class TokenAuthenticationFilter implements Filter
{
final static Logger logger = Logger.getLogger(TokenAuthenticationFilter.class.getCanonicalName());
@Autowired
private IAMUserDAO iamUserDAO;
@Override
public void init(FilterConfig arg0) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
//Code comes here
}
@Override
public void destroy() {
}
不幸的是,这个过滤器永远不会被访问,我不知道为什么。请帮忙
答案 0 :(得分:0)
我最初的需求就是使用DelegatingFilterProxy为现有应用程序添加一个过滤器,因为我的过滤器是使用@Autowired属性进行注释的。我最初尝试按these instructions修改web.xml;但是这对我没用。
然后我创建了这个类,一切都按计划运行:
public class AuthenticationBWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Filter[] getServletFilters () {
DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
filterProxy.setTargetBeanName("tokenAuthenticationFilter");
return new Filter[]{filterProxy, new AppLog4jFilter()};
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { SpringRootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected void registerContextLoaderListener(ServletContext servletContext) {
super.registerContextLoaderListener(servletContext);
}
}
希望这会帮助一些失去灵魂的人带来同样的问题。