Adding a Filter and Init Params Progmatically

一世执手 提交于 2019-12-05 04:34:10

问题


I need to copy the contents of a web.xml to the WebAppInitializer.class (Java Configuration Class). I have copied the YahooFilter Class from web.xml (see code) but I am not sure how to add the init-params pragmatically.

I have pasted the web.xml and snippet of the Java Configuration class below. Can somebody take a look and provide 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};
            }
    }

回答1:


Try overriding onStartup() method and programatically register your filter with ServletContext like this:

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


来源:https://stackoverflow.com/questions/26961998/adding-a-filter-and-init-params-progmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!