Adding Filter and Initialization Param Progmatically

I need to copy the content of web.xml to the WebAppInitializer.class (Java configuration class). I copied the YahooFilter class from web.xml (see code), but I'm not sure how to pragmatically add init-params.

I have pasted web.xml and Java config class snippet below. Can someone take a look and give some feedback?

<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};
            }
    }

      

+3


source to share


1 answer


Try overriding the method onStartup()

and registering your filter programmatically with the ServletContext

following:



@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");
}

      

+3


source







All Articles