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只是一個看起來更符合邏輯的名字。 –