3
我需要将web.xml的内容复制到WebAppInitializer.class(Java配置类)。我已经从web.xml复制了YahooFilter类(请参阅代码),但我不确定如何实用地添加init-params。以编程方式添加过滤器和初始化参数
我粘贴了下面的Java配置类的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配置类
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};
}
}
@Bohuslav_Burghardt是这个答案意味着要一直补充或替代原始海报在他的getServletFilters()代码中做了什么?如果答案是“除了”,那么yahooFilter和yosFilter之间的关系是什么? –
@SteveCohen OP在'getServletFilters'方法中添加过滤器,该方法只产生'Filter'对象数组。我的解决方案涉及在'onStartup'方法中通过servlet上下文注册过滤器,该方法返回['FilterRegistration'](https://docs.oracle.com/javaee/6/api/javax/servlet/FilterRegistration.html)对象,允许设置初始化参数。因此这个解决方案是“替代”原来的解决方案,而不是“另外”。 yahooFilter而不是yosFilter只是一个看起来更符合逻辑的名字。 –