以编程方式添加过滤器和Init参数

时间:2014-11-16 21:00:21

标签: spring spring-mvc servlet-filters yahoo-finance

我需要将web.xml的内容复制到WebAppInitializer.class(Java配置类)。我从web.xml复制了YahooFilter类(参见代码),但我不确定如何实用地添加init-params。

我已粘贴下面的Java Configuration类的web.xml和片段。有人可以看看并提供一些反馈吗?

<web-app> 
     <display-name>sample</display-Aname> 
     <filter> 
         <filter-name>YOSFilter</filter-name> 
         <filter-class>com.yahoo.yos.YahooFilter</filter-class> 


         <!--  
         optional param - 
         underlying oauth client class 
         possible values: 
             net.oauth.client.URLConnectionClient (default) 
             net.oauth.client.httpclient3.HttpClient3 
             net.oauth.client.httpclient4.HttpClient4 
         --> 
         <init-param> 
             <param-name>oauthConnectionClass</param-name> 
             <param-value>net.oauth.client.httpclient4.HttpClient4</param-value> 
         </init-param> 
         <!--  
         optional param - 
         redirect end-user if an access token is not found, set to false if you  
         are only making two-legged oauth calls e.g. oauth calls without an  
         access token to retrieve public information 
         defauts to true 
         --> 
         <init-param> 
             <param-name>redirect</param-name> 
             <param-value>true</param-value> 
         </init-param> 
     </filter> 


     <!-- 
     The URL where the filter is mapped to will redirect the user to Yahoo for 
     authorization if an OAuth authorization token has not been obtained for the 
     user.  Should correspond to your callback url 
     --> 


     <filter-mapping> 
         <filter-name>YOSFilter</filter-name> 
         <url-pattern>/login.jsp</url-pattern> 
     </filter-mapping> 
 </web-app> 

Java Config Class

    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
     ...

            @Override
        protected void registerDispatcherServlet(ServletContext servletContext) {
                    super.registerDispatcherServlet(servletContext);
            servletContext.addListener(new HttpSessionEventPublisher());

           // servletContext.addListener(new RequestContextListener());      
        }

             @Override
            protected Filter[] getServletFilters() {
                     DelegatingFilterProxy delegatingFilterProxy =  new DelegatingFilterProxy();
                    delegatingFilterProxy.setTargetBeanName("springSecurityFilterChain");
                    // FilterConfig filterConfig = delegatingFilterProxy.getFilterConfig();

                     YahooFilter yosFilter = new YahooFilter();


                    return  new Filter[] {delegatingFilterProxy,yosFilter};
            }
    }

1 个答案:

答案 0 :(得分:3)

尝试覆盖onStartup()方法并以ServletContext编程注册您的过滤器,如下所示:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    FilterRegistration yahooFilter = servletContext.addFilter("yahooFilter", new YahooFilter());
    yahooFilter.setInitParameter("oauthConnectionClass", "net.oauth.client.httpclient4.HttpClient4");
    yahooFilter.setInitParameter("redirect", "true");
}