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
beb84dbe
Unverified
Commit
beb84dbe
authored
Aug 16, 2016
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'molindo-1108-proxy-headers'
parents
34befdf1
d9768b53
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
3 deletions
+51
-3
ZuulProperties.java
...gframework/cloud/netflix/zuul/filters/ZuulProperties.java
+5
-0
PreDecorationFilter.java
...k/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java
+18
-3
PreDecorationFilterTests.java
...ud/netflix/zuul/filters/pre/PreDecorationFilterTests.java
+28
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java
View file @
beb84dbe
...
...
@@ -83,6 +83,11 @@ public class ZuulProperties {
private
boolean
addProxyHeaders
=
true
;
/**
* Flag to determine whether the proxy forwards the Host header.
*/
private
boolean
addHostHeader
=
false
;
/**
* Set of service names not to consider for proxying automatically. By default all
* services in the discovery client will be proxied.
*/
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java
View file @
beb84dbe
...
...
@@ -19,6 +19,8 @@ package org.springframework.cloud.netflix.zuul.filters.pre;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper
;
import
org.springframework.cloud.netflix.zuul.filters.Route
;
import
org.springframework.cloud.netflix.zuul.filters.RouteLocator
;
...
...
@@ -36,6 +38,8 @@ import lombok.extern.apachecommons.CommonsLog;
@CommonsLog
public
class
PreDecorationFilter
extends
ZuulFilter
{
public
static
final
int
FILTER_ORDER
=
5
;
private
RouteLocator
routeLocator
;
private
String
dispatcherServletPath
;
...
...
@@ -58,7 +62,7 @@ public class PreDecorationFilter extends ZuulFilter {
@Override
public
int
filterOrder
()
{
return
5
;
return
FILTER_ORDER
;
}
@Override
...
...
@@ -115,8 +119,7 @@ public class PreDecorationFilter extends ZuulFilter {
ctx
.
addOriginResponseHeader
(
"X-Zuul-ServiceId"
,
location
);
}
if
(
this
.
properties
.
isAddProxyHeaders
())
{
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Host"
,
ctx
.
getRequest
().
getServerName
());
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Host"
,
toHostHeader
(
ctx
.
getRequest
()));
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Port"
,
String
.
valueOf
(
ctx
.
getRequest
().
getServerPort
()));
ctx
.
addZuulRequestHeader
(
ZuulHeaders
.
X_FORWARDED_PROTO
,
...
...
@@ -149,6 +152,9 @@ public class PreDecorationFilter extends ZuulFilter {
}
ctx
.
addZuulRequestHeader
(
"X-Forwarded-For"
,
xforwardedfor
);
}
if
(
this
.
properties
.
isAddHostHeader
())
{
ctx
.
addZuulRequestHeader
(
"Host"
,
toHostHeader
(
ctx
.
getRequest
()));
}
}
}
else
{
...
...
@@ -182,6 +188,15 @@ public class PreDecorationFilter extends ZuulFilter {
return
null
;
}
private
String
toHostHeader
(
HttpServletRequest
request
)
{
int
port
=
request
.
getServerPort
();
if
((
port
==
80
&&
"http"
.
equals
(
request
.
getScheme
()))
||
(
port
==
443
&&
"https"
.
equals
(
request
.
getScheme
())))
{
return
request
.
getServerName
();
}
else
{
return
request
.
getServerName
()
+
":"
+
port
;
}
}
private
URL
getUrl
(
String
target
)
{
try
{
return
new
URL
(
target
);
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/pre/PreDecorationFilterTests.java
View file @
beb84dbe
...
...
@@ -91,6 +91,34 @@ public class PreDecorationFilterTests {
}
@Test
public
void
xForwardedHostHasPort
()
throws
Exception
{
this
.
properties
.
setPrefix
(
"/api"
);
this
.
request
.
setRequestURI
(
"/api/foo/1"
);
this
.
request
.
setRemoteAddr
(
"5.6.7.8"
);
this
.
request
.
setServerPort
(
8080
);
this
.
routeLocator
.
addRoute
(
new
ZuulRoute
(
"foo"
,
"/foo/**"
,
"foo"
,
null
,
false
,
null
,
null
));
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"localhost:8080"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
}
@Test
public
void
hostHeaderSet
()
throws
Exception
{
this
.
properties
.
setPrefix
(
"/api"
);
this
.
properties
.
setAddHostHeader
(
true
);
this
.
request
.
setRequestURI
(
"/api/foo/1"
);
this
.
request
.
setRemoteAddr
(
"5.6.7.8"
);
this
.
request
.
setServerPort
(
8080
);
this
.
routeLocator
.
addRoute
(
new
ZuulRoute
(
"foo"
,
"/foo/**"
,
"foo"
,
null
,
false
,
null
,
null
));
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"localhost:8080"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
assertEquals
(
"localhost:8080"
,
ctx
.
getZuulRequestHeaders
().
get
(
"host"
));
}
@Test
public
void
prefixRouteAddsHeader
()
throws
Exception
{
this
.
properties
.
setPrefix
(
"/api"
);
this
.
properties
.
setStripPrefix
(
true
);
...
...
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