Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
64fff7ed
Unverified
Commit
64fff7ed
authored
Aug 16, 2016
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'context-path-prefix-support' of…
Merge branch 'context-path-prefix-support' of
https://github.com/kakawait/spring-cloud-netflix
into kakawait-context-path-prefix-support
parents
beb84dbe
7ebe1977
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
96 additions
and
10 deletions
+96
-10
PreDecorationFilter.java
...k/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java
+15
-9
PreDecorationFilterTests.java
...ud/netflix/zuul/filters/pre/PreDecorationFilterTests.java
+81
-1
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java
View file @
64fff7ed
...
...
@@ -124,23 +124,29 @@ public class PreDecorationFilter extends ZuulFilter {
String
.
valueOf
(
ctx
.
getRequest
().
getServerPort
()));
ctx
.
addZuulRequestHeader
(
ZuulHeaders
.
X_FORWARDED_PROTO
,
ctx
.
getRequest
().
getScheme
());
String
forwardedPrefix
=
ctx
.
getRequest
().
getHeader
(
"X-Forwarded-Prefix"
);
String
contextPath
=
ctx
.
getRequest
().
getContextPath
();
String
prefix
=
StringUtils
.
hasLength
(
forwardedPrefix
)
?
forwardedPrefix
:
(
StringUtils
.
hasLength
(
contextPath
)
?
contextPath
:
null
);
if
(
StringUtils
.
hasText
(
route
.
getPrefix
()))
{
String
existingPrefix
=
ctx
.
getRequest
()
.
getHeader
(
"X-Forwarded-Prefix"
);
StringBuilder
newPrefixBuilder
=
new
StringBuilder
();
if
(
StringUtils
.
hasLength
(
existingPrefix
)
)
{
if
(
existingP
refix
.
endsWith
(
"/"
)
if
(
prefix
!=
null
)
{
if
(
p
refix
.
endsWith
(
"/"
)
&&
route
.
getPrefix
().
startsWith
(
"/"
))
{
newPrefixBuilder
.
append
(
existingP
refix
,
0
,
existingP
refix
.
length
()
-
1
);
newPrefixBuilder
.
append
(
p
refix
,
0
,
p
refix
.
length
()
-
1
);
}
else
{
newPrefixBuilder
.
append
(
existingP
refix
);
newPrefixBuilder
.
append
(
p
refix
);
}
}
newPrefixBuilder
.
append
(
route
.
getPrefix
());
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Prefix"
,
newPrefixBuilder
.
toString
());
prefix
=
newPrefixBuilder
.
toString
();
}
if
(
prefix
!=
null
)
{
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Prefix"
,
prefix
);
}
String
xforwardedfor
=
ctx
.
getRequest
().
getHeader
(
"X-Forwarded-For"
);
String
remoteAddr
=
ctx
.
getRequest
().
getRemoteAddr
();
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/pre/PreDecorationFilterTests.java
View file @
64fff7ed
...
...
@@ -165,6 +165,86 @@ public class PreDecorationFilterTests {
}
@Test
public
void
routeWithContextPath
()
{
this
.
properties
.
setStripPrefix
(
false
);
this
.
request
.
setRequestURI
(
"/api/foo/1"
);
this
.
request
.
setContextPath
(
"/context-path"
);
this
.
routeLocator
.
addRoute
(
new
ZuulRoute
(
"foo"
,
"/api/foo/**"
,
"foo"
,
null
,
false
,
null
,
null
));
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/api/foo/1"
,
ctx
.
get
(
"requestURI"
));
assertEquals
(
"localhost"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
assertEquals
(
"80"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-port"
));
assertEquals
(
"http"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-proto"
));
assertEquals
(
"/context-path"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
getHeader
(
ctx
.
getOriginResponseHeaders
(),
"x-zuul-serviceid"
));
}
@Test
public
void
prefixRouteWithContextPath
()
{
this
.
properties
.
setPrefix
(
"/api"
);
this
.
properties
.
setStripPrefix
(
true
);
this
.
request
.
setRequestURI
(
"/api/foo/1"
);
this
.
request
.
setContextPath
(
"/context-path"
);
this
.
routeLocator
.
addRoute
(
new
ZuulRoute
(
"foo"
,
"/foo/**"
,
"foo"
,
null
,
false
,
null
,
null
));
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/foo/1"
,
ctx
.
get
(
"requestURI"
));
assertEquals
(
"localhost"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
assertEquals
(
"80"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-port"
));
assertEquals
(
"http"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-proto"
));
assertEquals
(
"/context-path/api"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
getHeader
(
ctx
.
getOriginResponseHeaders
(),
"x-zuul-serviceid"
));
}
@Test
public
void
routeIgnoreContextPathIfPrefixHeader
()
{
this
.
properties
.
setStripPrefix
(
false
);
this
.
request
.
setRequestURI
(
"/api/foo/1"
);
this
.
request
.
setContextPath
(
"/context-path"
);
this
.
request
.
addHeader
(
"X-Forwarded-Prefix"
,
"/prefix"
);
this
.
routeLocator
.
addRoute
(
new
ZuulRoute
(
"foo"
,
"/api/foo/**"
,
"foo"
,
null
,
false
,
null
,
null
));
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/api/foo/1"
,
ctx
.
get
(
"requestURI"
));
assertEquals
(
"localhost"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
assertEquals
(
"80"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-port"
));
assertEquals
(
"http"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-proto"
));
assertEquals
(
"/prefix"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
getHeader
(
ctx
.
getOriginResponseHeaders
(),
"x-zuul-serviceid"
));
}
@Test
public
void
prefixRouteIgnoreContextPathIfPrefixHeader
()
{
this
.
properties
.
setPrefix
(
"/api"
);
this
.
properties
.
setStripPrefix
(
true
);
this
.
request
.
setRequestURI
(
"/api/foo/1"
);
this
.
request
.
setContextPath
(
"/context-path"
);
this
.
request
.
addHeader
(
"X-Forwarded-Prefix"
,
"/prefix"
);
this
.
routeLocator
.
addRoute
(
new
ZuulRoute
(
"foo"
,
"/foo/**"
,
"foo"
,
null
,
false
,
null
,
null
));
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/foo/1"
,
ctx
.
get
(
"requestURI"
));
assertEquals
(
"localhost"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
assertEquals
(
"80"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-port"
));
assertEquals
(
"http"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-proto"
));
assertEquals
(
"/prefix/api"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
getHeader
(
ctx
.
getOriginResponseHeaders
(),
"x-zuul-serviceid"
));
}
@Test
public
void
forwardRouteAddsLocation
()
throws
Exception
{
this
.
properties
.
setPrefix
(
"/api"
);
this
.
properties
.
setStripPrefix
(
true
);
...
...
@@ -405,7 +485,7 @@ public class PreDecorationFilterTests {
assertTrue
(
"sensitiveHeaders is wrong: "
+
sensitiveHeaders
,
sensitiveHeaders
.
containsAll
(
Arrays
.
asList
(
"x-bar"
,
"x-foo"
)));
}
@Test
public
void
urlProperlyDecodedWhenCharacterEncodingIsSet
()
throws
Exception
{
this
.
request
.
setCharacterEncoding
(
"UTF-8"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment