Skip to content

Commit 84c11a5

Browse files
mdeinumrstoyanchev
authored andcommitted
Allow interceptors with excludePathPattern only
Issue: SPR-11130
1 parent 52f1be7 commit 84c11a5

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -82,7 +82,7 @@ public InterceptorRegistration pathMatcher(PathMatcher pathMatcher) {
8282
* {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}.
8383
*/
8484
protected Object getInterceptor() {
85-
if (this.includePatterns.isEmpty()) {
85+
if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
8686
return this.interceptor;
8787
}
8888
MappedInterceptor mappedInterceptor = new MappedInterceptor(

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,35 @@ public void addWebRequestInterceptorsWithUrlPatterns() throws Exception {
138138
verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor2);
139139
}
140140

141+
/**
142+
* Test for SPR-11130
143+
*/
144+
@Test
145+
public void addInterceptorWithExcludePathPatternOnly() {
146+
registry.addInterceptor(interceptor1).excludePathPatterns("/path1/secret");
147+
registry.addInterceptor(interceptor2).addPathPatterns("/path2");
148+
149+
assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1"));
150+
assertEquals(Arrays.asList(interceptor1, interceptor2), getInterceptorsForPath("/path2"));
151+
assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret"));
152+
}
153+
154+
141155
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
142156
PathMatcher pathMatcher = new AntPathMatcher();
143157
List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
144-
for (Object i : registry.getInterceptors()) {
145-
if (i instanceof MappedInterceptor) {
146-
MappedInterceptor mappedInterceptor = (MappedInterceptor) i;
158+
for (Object interceptor : registry.getInterceptors()) {
159+
if (interceptor instanceof MappedInterceptor) {
160+
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
147161
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
148162
result.add(mappedInterceptor.getInterceptor());
149163
}
150164
}
151-
else if (i instanceof HandlerInterceptor){
152-
result.add((HandlerInterceptor) i);
165+
else if (interceptor instanceof HandlerInterceptor) {
166+
result.add((HandlerInterceptor) interceptor);
153167
}
154168
else {
155-
fail("Unexpected interceptor type: " + i.getClass().getName());
169+
fail("Unexpected interceptor type: " + interceptor.getClass().getName());
156170
}
157171
}
158172
return result;

0 commit comments

Comments
 (0)