Skip to content

Commit dba9c90

Browse files
committed
Ordered WebMvcConfigurer interceptor registrations
Closes gh-22434
1 parent 58b17bf commit dba9c90

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222

23+
import org.springframework.core.Ordered;
2324
import org.springframework.util.Assert;
2425
import org.springframework.util.PathMatcher;
2526
import org.springframework.util.StringUtils;
@@ -43,6 +44,8 @@ public class InterceptorRegistration {
4344

4445
private PathMatcher pathMatcher;
4546

47+
private int order = 0;
48+
4649

4750
/**
4851
* Create an {@link InterceptorRegistration} instance.
@@ -80,6 +83,15 @@ public InterceptorRegistration pathMatcher(PathMatcher pathMatcher) {
8083
return this;
8184
}
8285

86+
/**
87+
* Specify an order position to be used. Default is 0.
88+
* @since 4.23
89+
*/
90+
public InterceptorRegistration order(int order){
91+
this.order = order;
92+
return this;
93+
}
94+
8395
/**
8496
* Build the underlying interceptor. If URL patterns are provided, the returned
8597
* type is {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}.
@@ -98,4 +110,13 @@ protected Object getInterceptor() {
98110
return mappedInterceptor;
99111
}
100112

113+
Ordered toOrdered() {
114+
return new Ordered() {
115+
@Override
116+
public int getOrder() {
117+
return order;
118+
}
119+
};
120+
}
121+
101122
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,8 +17,11 @@
1717
package org.springframework.web.servlet.config.annotation;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.Comparator;
2022
import java.util.List;
2123

24+
import org.springframework.core.OrderComparator;
2225
import org.springframework.web.context.request.WebRequestInterceptor;
2326
import org.springframework.web.servlet.HandlerInterceptor;
2427
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
@@ -64,11 +67,25 @@ public InterceptorRegistration addWebRequestInterceptor(WebRequestInterceptor in
6467
* Return all registered interceptors.
6568
*/
6669
protected List<Object> getInterceptors() {
67-
List<Object> interceptors = new ArrayList<Object>(this.registrations.size());
70+
Collections.sort(this.registrations, INTERCEPTOR_ORDER_COMPARATOR);
71+
List<Object> result = new ArrayList<Object>(this.registrations.size());
6872
for (InterceptorRegistration registration : this.registrations) {
69-
interceptors.add(registration.getInterceptor());
73+
result.add(registration.getInterceptor());
7074
}
71-
return interceptors ;
75+
return result;
7276
}
7377

78+
79+
private static final Comparator<Object> INTERCEPTOR_ORDER_COMPARATOR =
80+
OrderComparator.INSTANCE.withSourceProvider(new OrderComparator.OrderSourceProvider() {
81+
@Override
82+
public Object getOrderSource(final Object object) {
83+
if (object instanceof InterceptorRegistration) {
84+
return ((InterceptorRegistration) object).toOrdered();
85+
}
86+
return null;
87+
}
88+
});
89+
90+
7491
}

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.junit.Test;
2626
import org.mockito.Mockito;
2727

28+
import org.springframework.core.Ordered;
2829
import org.springframework.mock.web.test.MockHttpServletRequest;
2930
import org.springframework.mock.web.test.MockHttpServletResponse;
3031
import org.springframework.ui.ModelMap;
@@ -150,6 +151,30 @@ public void addInterceptorWithExcludePathPatternOnly() {
150151
assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret"));
151152
}
152153

154+
@Test
155+
public void orderedInterceptors() {
156+
this.registry.addInterceptor(this.interceptor1).order(Ordered.LOWEST_PRECEDENCE);
157+
this.registry.addInterceptor(this.interceptor2).order(Ordered.HIGHEST_PRECEDENCE);
158+
159+
List<Object> interceptors = this.registry.getInterceptors();
160+
assertEquals(2, interceptors.size());
161+
162+
assertSame(this.interceptor2, interceptors.get(0));
163+
assertSame(this.interceptor1, interceptors.get(1));
164+
}
165+
166+
@Test
167+
public void nonOrderedInterceptors() {
168+
this.registry.addInterceptor(this.interceptor1).order(0);
169+
this.registry.addInterceptor(this.interceptor2).order(0);
170+
171+
List<Object> interceptors = this.registry.getInterceptors();
172+
assertEquals(2, interceptors.size());
173+
174+
assertSame(this.interceptor1, interceptors.get(0));
175+
assertSame(this.interceptor2, interceptors.get(1));
176+
}
177+
153178

154179
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
155180
PathMatcher pathMatcher = new AntPathMatcher();
@@ -177,6 +202,7 @@ private void verifyWebInterceptor(HandlerInterceptor interceptor, TestWebRequest
177202
assertTrue(webInterceptor.preHandleInvoked);
178203
}
179204

205+
180206
private static class TestWebRequestInterceptor implements WebRequestInterceptor {
181207

182208
private boolean preHandleInvoked = false;

0 commit comments

Comments
 (0)