Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
普通程序员一名
spring-framework
提交
a931ff12
S
spring-framework
项目概览
普通程序员一名
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a931ff12
编写于
3月 19, 2021
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MappedInterceptor falls back on PathMatcher
Closes gh-26690
上级
e4f753e3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
85 addition
and
31 deletion
+85
-31
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MappedInterceptor.java
...pringframework/web/servlet/handler/MappedInterceptor.java
+75
-30
spring-webmvc/src/test/java/org/springframework/web/servlet/handler/MappedInterceptorTests.java
...framework/web/servlet/handler/MappedInterceptorTests.java
+10
-1
未找到文件。
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MappedInterceptor.java
浏览文件 @
a931ff12
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -34,6 +34,7 @@ import org.springframework.web.util.ServletRequestPathUtils;
import
org.springframework.web.util.UrlPathHelper
;
import
org.springframework.web.util.pattern.PathPattern
;
import
org.springframework.web.util.pattern.PathPatternParser
;
import
org.springframework.web.util.pattern.PatternParseException
;
/**
* Wraps a {@link HandlerInterceptor} and uses URL patterns to determine whether
...
...
@@ -64,10 +65,10 @@ public final class MappedInterceptor implements HandlerInterceptor {
@Nullable
private
final
Pat
hPattern
[]
includePatterns
;
private
final
Pat
ternAdapter
[]
includePatterns
;
@Nullable
private
final
Pat
hPattern
[]
excludePatterns
;
private
final
Pat
ternAdapter
[]
excludePatterns
;
private
PathMatcher
pathMatcher
=
defaultPathMatcher
;
...
...
@@ -88,21 +89,11 @@ public final class MappedInterceptor implements HandlerInterceptor {
public
MappedInterceptor
(
@Nullable
String
[]
includePatterns
,
@Nullable
String
[]
excludePatterns
,
HandlerInterceptor
interceptor
,
@Nullable
PathPatternParser
parser
)
{
this
.
includePatterns
=
initPatterns
(
includePatterns
,
parser
);
this
.
excludePatterns
=
initPatterns
(
excludePatterns
,
parser
);
this
.
includePatterns
=
PatternAdapter
.
initPatterns
(
includePatterns
,
parser
);
this
.
excludePatterns
=
PatternAdapter
.
initPatterns
(
excludePatterns
,
parser
);
this
.
interceptor
=
interceptor
;
}
@Nullable
private
static
PathPattern
[]
initPatterns
(
@Nullable
String
[]
patterns
,
@Nullable
PathPatternParser
parser
)
{
if
(
ObjectUtils
.
isEmpty
(
patterns
))
{
return
null
;
}
parser
=
(
parser
!=
null
?
parser
:
PathPatternParser
.
defaultInstance
);
return
Arrays
.
stream
(
patterns
).
map
(
parser:
:
parse
).
toArray
(
PathPattern
[]::
new
);
}
/**
* Variant of
...
...
@@ -151,7 +142,7 @@ public final class MappedInterceptor implements HandlerInterceptor {
@Nullable
public
String
[]
getPathPatterns
()
{
return
(!
ObjectUtils
.
isEmpty
(
this
.
includePatterns
)
?
Arrays
.
stream
(
this
.
includePatterns
).
map
(
Pat
hPattern
:
:
getPatternString
).
toArray
(
String
[]::
new
)
:
Arrays
.
stream
(
this
.
includePatterns
).
map
(
Pat
ternAdapter
:
:
getPatternString
).
toArray
(
String
[]::
new
)
:
null
);
}
...
...
@@ -199,8 +190,8 @@ public final class MappedInterceptor implements HandlerInterceptor {
}
boolean
isPathContainer
=
(
path
instanceof
PathContainer
);
if
(!
ObjectUtils
.
isEmpty
(
this
.
excludePatterns
))
{
for
(
Pat
hPattern
pattern
:
this
.
excludePatterns
)
{
if
(
matchPattern
(
path
,
isPathContainer
,
pattern
))
{
for
(
Pat
ternAdapter
adapter
:
this
.
excludePatterns
)
{
if
(
adapter
.
match
(
path
,
isPathContainer
,
this
.
pathMatcher
))
{
return
false
;
}
}
...
...
@@ -208,20 +199,14 @@ public final class MappedInterceptor implements HandlerInterceptor {
if
(
ObjectUtils
.
isEmpty
(
this
.
includePatterns
))
{
return
true
;
}
for
(
Pat
hPattern
pattern
:
this
.
includePatterns
)
{
if
(
matchPattern
(
path
,
isPathContainer
,
pattern
))
{
for
(
Pat
ternAdapter
adapter
:
this
.
includePatterns
)
{
if
(
adapter
.
match
(
path
,
isPathContainer
,
this
.
pathMatcher
))
{
return
true
;
}
}
return
false
;
}
private
boolean
matchPattern
(
Object
path
,
boolean
isPathContainer
,
PathPattern
pattern
)
{
return
(
isPathContainer
?
pattern
.
matches
((
PathContainer
)
path
)
:
this
.
pathMatcher
.
match
(
pattern
.
getPatternString
(),
(
String
)
path
));
}
/**
* Determine a match for the given lookup path.
* @param lookupPath the current request path
...
...
@@ -233,8 +218,8 @@ public final class MappedInterceptor implements HandlerInterceptor {
public
boolean
matches
(
String
lookupPath
,
PathMatcher
pathMatcher
)
{
pathMatcher
=
(
this
.
pathMatcher
!=
defaultPathMatcher
?
this
.
pathMatcher
:
pathMatcher
);
if
(!
ObjectUtils
.
isEmpty
(
this
.
excludePatterns
))
{
for
(
Pat
hPattern
pattern
:
this
.
excludePatterns
)
{
if
(
pathMatcher
.
match
(
pattern
.
getPatternString
(),
lookupPath
))
{
for
(
Pat
ternAdapter
adapter
:
this
.
excludePatterns
)
{
if
(
pathMatcher
.
match
(
adapter
.
getPatternString
(),
lookupPath
))
{
return
false
;
}
}
...
...
@@ -242,8 +227,8 @@ public final class MappedInterceptor implements HandlerInterceptor {
if
(
ObjectUtils
.
isEmpty
(
this
.
includePatterns
))
{
return
true
;
}
for
(
Pat
hPattern
pattern
:
this
.
includePatterns
)
{
if
(
pathMatcher
.
match
(
pattern
.
getPatternString
(),
lookupPath
))
{
for
(
Pat
ternAdapter
adapter
:
this
.
includePatterns
)
{
if
(
pathMatcher
.
match
(
adapter
.
getPatternString
(),
lookupPath
))
{
return
true
;
}
}
...
...
@@ -274,4 +259,64 @@ public final class MappedInterceptor implements HandlerInterceptor {
this
.
interceptor
.
afterCompletion
(
request
,
response
,
handler
,
ex
);
}
/**
* Contains both the parsed {@link PathPattern} and the raw String pattern,
* and uses the former when the cached path is {@link PathContainer} or the
* latter otherwise. If the pattern cannot be parsed due to unsupported
* syntax, then {@link PathMatcher} is used for all requests.
* @since 5.3.6
*/
private
static
class
PatternAdapter
{
private
final
String
patternString
;
@Nullable
private
final
PathPattern
pathPattern
;
public
PatternAdapter
(
String
pattern
,
@Nullable
PathPatternParser
parser
)
{
this
.
patternString
=
pattern
;
this
.
pathPattern
=
initPathPattern
(
pattern
,
parser
);
}
@Nullable
private
static
PathPattern
initPathPattern
(
String
pattern
,
@Nullable
PathPatternParser
parser
)
{
try
{
return
(
parser
!=
null
?
parser
:
PathPatternParser
.
defaultInstance
).
parse
(
pattern
);
}
catch
(
PatternParseException
ex
)
{
return
null
;
}
}
public
String
getPatternString
()
{
return
this
.
patternString
;
}
public
boolean
match
(
Object
path
,
boolean
isPathContainer
,
PathMatcher
pathMatcher
)
{
if
(
isPathContainer
)
{
PathContainer
pathContainer
=
(
PathContainer
)
path
;
if
(
this
.
pathPattern
!=
null
)
{
return
this
.
pathPattern
.
matches
(
pathContainer
);
}
String
lookupPath
=
pathContainer
.
value
();
path
=
UrlPathHelper
.
defaultInstance
.
removeSemicolonContent
(
lookupPath
);
}
return
pathMatcher
.
match
(
this
.
patternString
,
(
String
)
path
);
}
@Nullable
public
static
PatternAdapter
[]
initPatterns
(
@Nullable
String
[]
patterns
,
@Nullable
PathPatternParser
parser
)
{
if
(
ObjectUtils
.
isEmpty
(
patterns
))
{
return
null
;
}
return
Arrays
.
stream
(
patterns
)
.
map
(
pattern
->
new
PatternAdapter
(
pattern
,
parser
))
.
toArray
(
PatternAdapter
[]::
new
);
}
}
}
spring-webmvc/src/test/java/org/springframework/web/servlet/handler/MappedInterceptorTests.java
浏览文件 @
a931ff12
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -88,6 +88,15 @@ class MappedInterceptorTests {
assertThat
(
interceptor
.
matches
(
requestFactory
.
apply
(
"/admin/foo"
))).
isFalse
();
}
@PathPatternsParameterizedTest
// gh-26690
void
includePatternWithFallbackOnPathMatcher
(
Function
<
String
,
MockHttpServletRequest
>
requestFactory
)
{
MappedInterceptor
interceptor
=
new
MappedInterceptor
(
new
String
[]
{
"/path1/**/path2"
},
null
,
delegate
);
assertThat
(
interceptor
.
matches
(
requestFactory
.
apply
(
"/path1/foo/bar/path2"
))).
isTrue
();
assertThat
(
interceptor
.
matches
(
requestFactory
.
apply
(
"/path1/foo/bar/path3"
))).
isFalse
();
assertThat
(
interceptor
.
matches
(
requestFactory
.
apply
(
"/path3/foo/bar/path2"
))).
isFalse
();
}
@PathPatternsParameterizedTest
void
customPathMatcher
(
Function
<
String
,
MockHttpServletRequest
>
requestFactory
)
{
MappedInterceptor
interceptor
=
new
MappedInterceptor
(
new
String
[]
{
"/foo/[0-9]*"
},
null
,
delegate
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录