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
718cafcd
Commit
718cafcd
authored
Mar 03, 2016
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add X-Forwarded-Port explicitly for Tomcat (and other servers)
Fixes gh-872
parent
908dcc0e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
17 deletions
+36
-17
PreDecorationFilter.java
...k/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java
+7
-4
SampleZuulProxyApplicationTests.java
...k/cloud/netflix/zuul/SampleZuulProxyApplicationTests.java
+26
-11
PreDecorationFilterTests.java
...ud/netflix/zuul/filters/pre/PreDecorationFilterTests.java
+3
-2
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java
View file @
718cafcd
...
...
@@ -39,7 +39,8 @@ public class PreDecorationFilter extends ZuulFilter {
private
UrlPathHelper
urlPathHelper
=
new
UrlPathHelper
();
public
PreDecorationFilter
(
RouteLocator
routeLocator
,
boolean
addProxyHeaders
,
boolean
removeSemicolonContent
)
{
public
PreDecorationFilter
(
RouteLocator
routeLocator
,
boolean
addProxyHeaders
,
boolean
removeSemicolonContent
)
{
this
.
routeLocator
=
routeLocator
;
this
.
addProxyHeaders
=
addProxyHeaders
;
this
.
urlPathHelper
.
setRemoveSemicolonContent
(
removeSemicolonContent
);
...
...
@@ -59,7 +60,8 @@ public class PreDecorationFilter extends ZuulFilter {
public
boolean
shouldFilter
()
{
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
return
!
ctx
.
containsKey
(
"forward.to"
)
// another filter has already forwarded
&&
!
ctx
.
containsKey
(
"serviceId"
);
// another filter has already determined serviceId
&&
!
ctx
.
containsKey
(
"serviceId"
);
// another filter has already determined
// serviceId
}
@Override
...
...
@@ -97,8 +99,9 @@ public class PreDecorationFilter extends ZuulFilter {
}
if
(
this
.
addProxyHeaders
)
{
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Host"
,
ctx
.
getRequest
().
getServerName
()
+
":"
+
String
.
valueOf
(
ctx
.
getRequest
().
getServerPort
()));
ctx
.
getRequest
().
getServerName
());
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Port"
,
String
.
valueOf
(
ctx
.
getRequest
().
getServerPort
()));
ctx
.
addZuulRequestHeader
(
ZuulHeaders
.
X_FORWARDED_PROTO
,
ctx
.
getRequest
().
getScheme
());
if
(
StringUtils
.
hasText
(
route
.
getPrefix
()))
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SampleZuulProxyApplicationTests.java
View file @
718cafcd
...
...
@@ -62,20 +62,23 @@ import com.netflix.loadbalancer.Server;
import
com.netflix.loadbalancer.ServerList
;
import
com.netflix.niws.client.http.RestClient
;
import
lombok.SneakyThrows
;
import
static
org
.
hamcrest
.
CoreMatchers
.
containsString
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
lombok.SneakyThrows
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
SampleZuulProxyApplication
.
class
)
@WebAppConfiguration
@IntegrationTest
({
"server.port: 0"
,
@IntegrationTest
({
"server.port: 0"
,
"zuul.routes.other: /test/**=http://localhost:7777/local"
,
"zuul.routes.another: /another/twolevel/**"
,
"zuul.routes.simple: /simple/**"
,
"zuul.routes.badhost: /badhost/**"
,
"zuul.ignoredHeaders: X-Header"
,
"zuul.removeSemicolonContent: false"
})
"zuul.routes.rnd: /rnd/**"
,
"rnd.ribbon.listOfServers: ${random.value}"
,
"zuul.removeSemicolonContent: false"
})
@DirtiesContext
public
class
SampleZuulProxyApplicationTests
extends
ZuulProxyTestBase
{
...
...
@@ -140,9 +143,8 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
this
.
routes
.
addRoute
(
"/self/**"
,
"http://localhost:"
+
this
.
port
+
"/"
);
this
.
endpoint
.
reset
();
ResponseEntity
<
String
>
result
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/self/query?foo={foo}"
,
HttpMethod
.
GET
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
,
"weird#chars"
);
"http://localhost:"
+
this
.
port
+
"/self/query?foo={foo}"
,
HttpMethod
.
GET
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
,
"weird#chars"
);
assertEquals
(
HttpStatus
.
OK
,
result
.
getStatusCode
());
assertEquals
(
"/query?foo=weird#chars"
,
result
.
getBody
());
}
...
...
@@ -169,6 +171,18 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
"http://localhost:"
+
this
.
port
+
"/badhost/1"
,
HttpMethod
.
GET
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
);
assertEquals
(
HttpStatus
.
INTERNAL_SERVER_ERROR
,
result
.
getStatusCode
());
// JSON response
assertThat
(
result
.
getBody
(),
containsString
(
"\"status\":500"
));
}
@Test
public
void
ribbonCommandRandomHostFromConfig
()
{
ResponseEntity
<
String
>
result
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/rnd/1"
,
HttpMethod
.
GET
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
);
assertEquals
(
HttpStatus
.
INTERNAL_SERVER_ERROR
,
result
.
getStatusCode
());
// JSON response
assertThat
(
result
.
getBody
(),
containsString
(
"\"status\":500"
));
}
@Test
...
...
@@ -211,9 +225,10 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
@RequestMapping
(
"/matrix/{name}/{another}"
)
public
String
matrix
(
@PathVariable
(
"name"
)
String
name
,
@MatrixVariable
(
value
=
"p"
,
pathVar
=
"name"
)
int
p
,
@MatrixVariable
(
value
=
"q"
,
pathVar
=
"name"
)
int
q
,
@PathVariable
(
"another"
)
String
another
,
@MatrixVariable
(
value
=
"x"
,
pathVar
=
"another"
)
int
x
)
{
@MatrixVariable
(
value
=
"p"
,
pathVar
=
"name"
)
int
p
,
@MatrixVariable
(
value
=
"q"
,
pathVar
=
"name"
)
int
q
,
@PathVariable
(
"another"
)
String
another
,
@MatrixVariable
(
value
=
"x"
,
pathVar
=
"another"
)
int
x
)
{
return
name
+
"="
+
p
+
"-"
+
q
+
";"
+
another
+
"="
+
x
;
}
...
...
@@ -275,7 +290,7 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
}
}
// Load balancer with fixed server list for "simple" pointing to
local
host
// Load balancer with fixed server list for "simple" pointing to
bad
host
@Configuration
static
class
BadHostRibbonClientConfiguration
{
@Bean
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/pre/PreDecorationFilterTests.java
View file @
718cafcd
...
...
@@ -90,7 +90,8 @@ public class PreDecorationFilterTests {
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/foo/1"
,
ctx
.
get
(
"requestURI"
));
assertEquals
(
"localhost:80"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
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
(
"/api"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
...
...
@@ -128,7 +129,7 @@ public class PreDecorationFilterTests {
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/1"
,
ctx
.
get
(
"requestURI"
));
assertEquals
(
"localhost
:80
"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
assertEquals
(
"localhost"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-host"
));
assertEquals
(
"http"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-proto"
));
assertEquals
(
"/api/foo"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
...
...
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