|
@@ -6,6 +6,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
|
import org.springframework.core.Ordered;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
import org.w3c.dom.Node;
|
|
@@ -20,55 +21,66 @@ import javax.servlet.FilterChain;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
- * Replaces a Spring bean of type "Filter" with a wrapper class which implement the <tt>Ordered</tt>
|
|
|
- * interface. This allows user to add their own filter to the security chain.
|
|
|
+ * Replaces a Spring bean of type "Filter" with a wrapper class which implements the <tt>Ordered</tt>
|
|
|
+ * interface. This allows user to add their own filter to the security chain. If the user's filter
|
|
|
+ * already implements Ordered
|
|
|
*
|
|
|
* @author Luke Taylor
|
|
|
* @version $Id$
|
|
|
*/
|
|
|
public class OrderedFilterBeanDefinitionDecorator implements BeanDefinitionDecorator {
|
|
|
+
|
|
|
public static final String ATT_ORDER = "order";
|
|
|
|
|
|
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) {
|
|
|
Element elt = (Element)node;
|
|
|
String order = elt.getAttribute(ATT_ORDER);
|
|
|
BeanDefinition filter = holder.getBeanDefinition();
|
|
|
-
|
|
|
- Assert.hasText(order, "order attribute is required");
|
|
|
-
|
|
|
BeanDefinition wrapper = new RootBeanDefinition(OrderedFilterDecorator.class);
|
|
|
- wrapper.getConstructorArgumentValues().addGenericArgumentValue(filter);
|
|
|
- wrapper.getPropertyValues().addPropertyValue("order", order);
|
|
|
+ wrapper.getConstructorArgumentValues().addIndexedArgumentValue(0, holder.getBeanName());
|
|
|
+ wrapper.getConstructorArgumentValues().addIndexedArgumentValue(1, filter);
|
|
|
+
|
|
|
+ if (StringUtils.hasText(order)) {
|
|
|
+ wrapper.getPropertyValues().addPropertyValue("order", order);
|
|
|
+ }
|
|
|
|
|
|
return new BeanDefinitionHolder(wrapper, holder.getBeanName());
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-class OrderedFilterDecorator implements Filter, Ordered {
|
|
|
- private int order = LOWEST_PRECEDENCE;
|
|
|
- private Filter delegate;
|
|
|
+ static class OrderedFilterDecorator implements Filter, Ordered {
|
|
|
+ private Integer order = null;
|
|
|
+ private Filter delegate;
|
|
|
+ private String beanName;
|
|
|
|
|
|
- OrderedFilterDecorator(Filter delegate) {
|
|
|
- this.delegate = delegate;
|
|
|
- }
|
|
|
+ OrderedFilterDecorator(String beanName, Filter delegate) {
|
|
|
+ this.delegate = delegate;
|
|
|
+ this.beanName = beanName;
|
|
|
+ }
|
|
|
|
|
|
- public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
|
|
- delegate.doFilter(request, response, chain);
|
|
|
- }
|
|
|
+ public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
|
|
+ delegate.doFilter(request, response, chain);
|
|
|
+ }
|
|
|
|
|
|
- public final void init(FilterConfig filterConfig) throws ServletException {
|
|
|
- delegate.init(filterConfig);
|
|
|
- }
|
|
|
+ public final void init(FilterConfig filterConfig) throws ServletException {
|
|
|
+ delegate.init(filterConfig);
|
|
|
+ }
|
|
|
|
|
|
- public final void destroy() {
|
|
|
- delegate.destroy();
|
|
|
- }
|
|
|
+ public final void destroy() {
|
|
|
+ delegate.destroy();
|
|
|
+ }
|
|
|
|
|
|
- public final int getOrder() {
|
|
|
- return order;
|
|
|
- }
|
|
|
+ public final int getOrder() {
|
|
|
+ if(order == null) {
|
|
|
+ Assert.isInstanceOf(Ordered.class, "Filter '"+ beanName +"' must implement the 'Ordered' interface " +
|
|
|
+ " or you must specify an order in <user-filter>");
|
|
|
+
|
|
|
+ return ((Ordered)delegate).getOrder();
|
|
|
+ }
|
|
|
+ return order.intValue();
|
|
|
+ }
|
|
|
|
|
|
- public final void setOrder(int order) {
|
|
|
- this.order = order;
|
|
|
+ public final void setOrder(int order) {
|
|
|
+ this.order = new Integer(order);
|
|
|
+ }
|
|
|
}
|
|
|
}
|