Ordered WebMvcConfigurer interceptor registrations

Issue: SPR-15620
This commit is contained in:
Eko Kurniawan Khannedy
2017-06-03 12:09:28 +07:00
committed by Rossen Stoyanchev
parent 5c1d8c7c59
commit ac68cc35c2
3 changed files with 48 additions and 7 deletions

View File

@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
@@ -33,7 +34,7 @@ import org.springframework.web.servlet.handler.MappedInterceptor;
* @author Keith Donald
* @since 3.1
*/
public class InterceptorRegistration {
public class InterceptorRegistration implements Ordered {
private final HandlerInterceptor interceptor;
@@ -43,6 +44,7 @@ public class InterceptorRegistration {
private PathMatcher pathMatcher;
private int order = 0;
/**
* Creates an {@link InterceptorRegistration} instance.
@@ -99,4 +101,17 @@ public class InterceptorRegistration {
return mappedInterceptor;
}
/**
* An order position to be used, default is 0.
*/
public InterceptorRegistration order(int order){
this.order = order;
return this;
}
@Override
public int getOrder() {
return order;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -18,7 +18,9 @@ package org.springframework.web.servlet.config.annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.core.OrderComparator;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
@@ -64,11 +66,10 @@ public class InterceptorRegistry {
* Return all registered interceptors.
*/
protected List<Object> getInterceptors() {
List<Object> interceptors = new ArrayList<>(this.registrations.size());
for (InterceptorRegistration registration : this.registrations) {
interceptors.add(registration.getInterceptor());
}
return interceptors ;
return this.registrations.stream()
.sorted(OrderComparator.INSTANCE)
.map(InterceptorRegistration::getInterceptor)
.collect(Collectors.toList());
}
}