From 84c11a5cc742c024733a9279007f735e329e2665 Mon Sep 17 00:00:00 2001 From: Marten Deinum Date: Wed, 4 Jun 2014 14:25:39 +0200 Subject: [PATCH] Allow interceptors with excludePathPattern only Issue: SPR-11130 --- .../annotation/InterceptorRegistration.java | 4 +-- .../annotation/InterceptorRegistryTests.java | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java index 2a5386987d..25453cda41 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -82,7 +82,7 @@ public class InterceptorRegistration { * {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}. */ protected Object getInterceptor() { - if (this.includePatterns.isEmpty()) { + if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) { return this.interceptor; } MappedInterceptor mappedInterceptor = new MappedInterceptor( diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java index ec188c1ea4..0176930f84 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java @@ -138,21 +138,35 @@ public class InterceptorRegistryTests { verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor2); } + /** + * Test for SPR-11130 + */ + @Test + public void addInterceptorWithExcludePathPatternOnly() { + registry.addInterceptor(interceptor1).excludePathPatterns("/path1/secret"); + registry.addInterceptor(interceptor2).addPathPatterns("/path2"); + + assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1")); + assertEquals(Arrays.asList(interceptor1, interceptor2), getInterceptorsForPath("/path2")); + assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret")); + } + + private List getInterceptorsForPath(String lookupPath) { PathMatcher pathMatcher = new AntPathMatcher(); List result = new ArrayList(); - for (Object i : registry.getInterceptors()) { - if (i instanceof MappedInterceptor) { - MappedInterceptor mappedInterceptor = (MappedInterceptor) i; + for (Object interceptor : registry.getInterceptors()) { + if (interceptor instanceof MappedInterceptor) { + MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor; if (mappedInterceptor.matches(lookupPath, pathMatcher)) { result.add(mappedInterceptor.getInterceptor()); } } - else if (i instanceof HandlerInterceptor){ - result.add((HandlerInterceptor) i); + else if (interceptor instanceof HandlerInterceptor) { + result.add((HandlerInterceptor) interceptor); } else { - fail("Unexpected interceptor type: " + i.getClass().getName()); + fail("Unexpected interceptor type: " + interceptor.getClass().getName()); } } return result;