Skip to content

Commit 276dd93

Browse files
committed
Ensure interceptors apply to all HandlerMappings. Fixes #9622 and Fixes #9724
1 parent 1ee250b commit 276dd93

File tree

4 files changed

+22
-30
lines changed

4 files changed

+22
-30
lines changed

grails-plugin-interceptors/src/main/groovy/grails/artefact/Interceptor.groovy

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ import org.grails.web.util.GrailsApplicationAttributes
3535
import org.grails.web.util.WebUtils
3636
import org.springframework.core.Ordered
3737
import org.springframework.web.servlet.ModelAndView
38-
import org.springframework.web.servlet.View
39-
import org.springframework.web.servlet.ViewResolver
4038

39+
import javax.annotation.PostConstruct
4140
import javax.servlet.http.HttpServletRequest
4241
import javax.servlet.http.HttpServletResponse
4342
import java.util.concurrent.ConcurrentLinkedQueue
@@ -80,13 +79,6 @@ trait Interceptor implements ResponseRenderer, ResponseRedirector, RequestForwar
8079
return (Boolean)existing
8180
}
8281

83-
if(matchers.isEmpty()) {
84-
// default to map just the controller by convention
85-
def matcher = new UrlMappingMatcher(this)
86-
matcher.matches(controller:Pattern.compile(GrailsNameUtils.getLogicalPropertyName(getClass().simpleName, "Interceptor")))
87-
matchers << matcher
88-
}
89-
9082
def req = request
9183
def uri = req.requestURI
9284

@@ -268,4 +260,17 @@ trait Interceptor implements ResponseRenderer, ResponseRedirector, RequestForwar
268260
}
269261
}
270262

263+
/**
264+
* Registers the default match strategy if non has been registered
265+
*/
266+
@PostConstruct
267+
void defaultMatcher() {
268+
if(matchers.isEmpty()) {
269+
// default to map just the controller by convention
270+
def matcher = new UrlMappingMatcher(this)
271+
matcher.matches(controller:Pattern.compile(GrailsNameUtils.getLogicalPropertyName(getClass().simpleName, InterceptorArtefactHandler.TYPE)))
272+
matchers << matcher
273+
}
274+
275+
}
271276
}

grails-plugin-interceptors/src/main/groovy/org/grails/plugins/web/interceptors/InterceptorsGrailsPlugin.groovy

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import grails.plugins.Plugin
2222
import grails.util.GrailsUtil
2323
import groovy.transform.CompileDynamic
2424
import groovy.transform.CompileStatic
25+
import org.springframework.web.servlet.HandlerInterceptor
26+
import org.springframework.web.servlet.handler.MappedInterceptor
2527

2628

2729
/**
@@ -45,7 +47,7 @@ class InterceptorsGrailsPlugin extends Plugin {
4547
GrailsClass[] interceptors = grailsApplication.getArtefacts(InterceptorArtefactHandler.TYPE)
4648
if(interceptors.length == 0) return
4749

48-
grailsInterceptorHandlerInterceptorAdapter(GrailsInterceptorHandlerInterceptorAdapter)
50+
grailsInterceptorMappedInterceptor(MappedInterceptor, ['/**'] as String[], bean(GrailsInterceptorHandlerInterceptorAdapter))
4951

5052
def enableJsessionId = config.getProperty(Settings.GRAILS_VIEWS_ENABLE_JSESSIONID, Boolean, false)
5153
for(GrailsClass i in interceptors) {
@@ -61,8 +63,8 @@ class InterceptorsGrailsPlugin extends Plugin {
6163

6264
@Override
6365
void doWithApplicationContext() {
64-
if(applicationContext.containsBeanDefinition("grailsInterceptorHandlerInterceptorAdapter")) {
65-
interceptorAdapter = applicationContext.getBean("grailsInterceptorHandlerInterceptorAdapter", GrailsInterceptorHandlerInterceptorAdapter)
66+
if(applicationContext.containsBeanDefinition("grailsInterceptorMappedInterceptor")) {
67+
interceptorAdapter = (GrailsInterceptorHandlerInterceptorAdapter)applicationContext.getBean("grailsInterceptorMappedInterceptor", MappedInterceptor).getInterceptor()
6668
}
6769
}
6870

@@ -76,7 +78,7 @@ class InterceptorsGrailsPlugin extends Plugin {
7678
def interceptorClass = (Class) source
7779
def grailsClass = grailsApplication.addArtefact(InterceptorArtefactHandler.TYPE, interceptorClass)
7880

79-
def interceptorAdapter = this.interceptorAdapter ?: applicationContext.getBean(GrailsInterceptorHandlerInterceptorAdapter)
81+
def interceptorAdapter = this.interceptorAdapter ?: (GrailsInterceptorHandlerInterceptorAdapter)applicationContext.getBean("grailsInterceptorMappedInterceptor", MappedInterceptor).getInterceptor()
8082
defineInterceptorBean(grailsClass, interceptorClass, enableJsessionId)
8183
interceptorAdapter.setInterceptors(
8284
applicationContext.getBeansOfType(Interceptor).values() as Interceptor[]

grails-plugin-interceptors/src/test/groovy/grails/artefact/InterceptorSpec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class InterceptorSpec extends Specification {
3939
void "Test the default interceptor mappings"() {
4040
given:"A test interceptor"
4141
def i = new TestInterceptor()
42+
i.defaultMatcher()
4243
def webRequest = GrailsWebMockUtil.bindMockWebRequest()
4344
HttpServletRequest request = webRequest.request
4445
when:"The current request is for a controller called test"

grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping {
6161
setOrder(-5)
6262
}
6363

64-
@Autowired
65-
void setHandlerInterceptors(HandlerInterceptor[] handlerInterceptors) {
66-
setInterceptors(handlerInterceptors)
67-
}
68-
6964
@Autowired(required = false)
7065
void setWebRequestInterceptors(WebRequestInterceptor[] webRequestInterceptors) {
7166
webRequestHandlerInterceptors = webRequestInterceptors.collect( { WebRequestInterceptor wri ->
@@ -80,23 +75,12 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping {
8075

8176
@Override
8277
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
83-
HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
84-
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
78+
def chain = super.getHandlerExecutionChain(handler, request)
8579

8680
// WebRequestInterceptor need to come first, as these include things like Hibernate OSIV
8781
if(webRequestHandlerInterceptors) {
8882
chain.addInterceptors webRequestHandlerInterceptors
8983
}
90-
91-
chain.addInterceptors getAdaptedInterceptors()
92-
93-
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request)
94-
for (MappedInterceptor mappedInterceptor in getMappedInterceptors()) {
95-
if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
96-
chain.addInterceptor(mappedInterceptor.interceptor)
97-
}
98-
}
99-
10084
chain.addInterceptor(new ErrorHandlingHandler())
10185
return chain
10286
}

0 commit comments

Comments
 (0)