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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
15 deletions
+34
-15
PreDecorationFilter.java
...k/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java
+7
-4
SampleZuulProxyApplicationTests.java
...k/cloud/netflix/zuul/SampleZuulProxyApplicationTests.java
+24
-9
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 {
...
@@ -39,7 +39,8 @@ public class PreDecorationFilter extends ZuulFilter {
private
UrlPathHelper
urlPathHelper
=
new
UrlPathHelper
();
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
.
routeLocator
=
routeLocator
;
this
.
addProxyHeaders
=
addProxyHeaders
;
this
.
addProxyHeaders
=
addProxyHeaders
;
this
.
urlPathHelper
.
setRemoveSemicolonContent
(
removeSemicolonContent
);
this
.
urlPathHelper
.
setRemoveSemicolonContent
(
removeSemicolonContent
);
...
@@ -59,7 +60,8 @@ public class PreDecorationFilter extends ZuulFilter {
...
@@ -59,7 +60,8 @@ public class PreDecorationFilter extends ZuulFilter {
public
boolean
shouldFilter
()
{
public
boolean
shouldFilter
()
{
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
return
!
ctx
.
containsKey
(
"forward.to"
)
// another filter has already forwarded
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
@Override
...
@@ -97,8 +99,9 @@ public class PreDecorationFilter extends ZuulFilter {
...
@@ -97,8 +99,9 @@ public class PreDecorationFilter extends ZuulFilter {
}
}
if
(
this
.
addProxyHeaders
)
{
if
(
this
.
addProxyHeaders
)
{
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Host"
,
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Host"
,
ctx
.
getRequest
().
getServerName
()
+
":"
ctx
.
getRequest
().
getServerName
());
+
String
.
valueOf
(
ctx
.
getRequest
().
getServerPort
()));
ctx
.
addZuulRequestHeader
(
"X-Forwarded-Port"
,
String
.
valueOf
(
ctx
.
getRequest
().
getServerPort
()));
ctx
.
addZuulRequestHeader
(
ZuulHeaders
.
X_FORWARDED_PROTO
,
ctx
.
addZuulRequestHeader
(
ZuulHeaders
.
X_FORWARDED_PROTO
,
ctx
.
getRequest
().
getScheme
());
ctx
.
getRequest
().
getScheme
());
if
(
StringUtils
.
hasText
(
route
.
getPrefix
()))
{
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;
...
@@ -62,20 +62,23 @@ import com.netflix.loadbalancer.Server;
import
com.netflix.loadbalancer.ServerList
;
import
com.netflix.loadbalancer.ServerList
;
import
com.netflix.niws.client.http.RestClient
;
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
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
lombok.SneakyThrows
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
SampleZuulProxyApplication
.
class
)
@SpringApplicationConfiguration
(
classes
=
SampleZuulProxyApplication
.
class
)
@WebAppConfiguration
@WebAppConfiguration
@IntegrationTest
({
"server.port: 0"
,
@IntegrationTest
({
"server.port: 0"
,
"zuul.routes.other: /test/**=http://localhost:7777/local"
,
"zuul.routes.other: /test/**=http://localhost:7777/local"
,
"zuul.routes.another: /another/twolevel/**"
,
"zuul.routes.simple: /simple/**"
,
"zuul.routes.another: /another/twolevel/**"
,
"zuul.routes.simple: /simple/**"
,
"zuul.routes.badhost: /badhost/**"
,
"zuul.ignoredHeaders: X-Header"
,
"zuul.routes.badhost: /badhost/**"
,
"zuul.ignoredHeaders: X-Header"
,
"zuul.removeSemicolonContent: false"
})
"zuul.routes.rnd: /rnd/**"
,
"rnd.ribbon.listOfServers: ${random.value}"
,
"zuul.removeSemicolonContent: false"
})
@DirtiesContext
@DirtiesContext
public
class
SampleZuulProxyApplicationTests
extends
ZuulProxyTestBase
{
public
class
SampleZuulProxyApplicationTests
extends
ZuulProxyTestBase
{
...
@@ -140,9 +143,8 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
...
@@ -140,9 +143,8 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
this
.
routes
.
addRoute
(
"/self/**"
,
"http://localhost:"
+
this
.
port
+
"/"
);
this
.
routes
.
addRoute
(
"/self/**"
,
"http://localhost:"
+
this
.
port
+
"/"
);
this
.
endpoint
.
reset
();
this
.
endpoint
.
reset
();
ResponseEntity
<
String
>
result
=
new
TestRestTemplate
().
exchange
(
ResponseEntity
<
String
>
result
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
"/self/query?foo={foo}"
,
"http://localhost:"
+
this
.
port
+
"/self/query?foo={foo}"
,
HttpMethod
.
GET
,
HttpMethod
.
GET
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
,
"weird#chars"
);
"weird#chars"
);
assertEquals
(
HttpStatus
.
OK
,
result
.
getStatusCode
());
assertEquals
(
HttpStatus
.
OK
,
result
.
getStatusCode
());
assertEquals
(
"/query?foo=weird#chars"
,
result
.
getBody
());
assertEquals
(
"/query?foo=weird#chars"
,
result
.
getBody
());
}
}
...
@@ -169,6 +171,18 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
...
@@ -169,6 +171,18 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
"http://localhost:"
+
this
.
port
+
"/badhost/1"
,
HttpMethod
.
GET
,
"http://localhost:"
+
this
.
port
+
"/badhost/1"
,
HttpMethod
.
GET
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
);
new
HttpEntity
<>((
Void
)
null
),
String
.
class
);
assertEquals
(
HttpStatus
.
INTERNAL_SERVER_ERROR
,
result
.
getStatusCode
());
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
@Test
...
@@ -213,7 +227,8 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
...
@@ -213,7 +227,8 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
public
String
matrix
(
@PathVariable
(
"name"
)
String
name
,
public
String
matrix
(
@PathVariable
(
"name"
)
String
name
,
@MatrixVariable
(
value
=
"p"
,
pathVar
=
"name"
)
int
p
,
@MatrixVariable
(
value
=
"p"
,
pathVar
=
"name"
)
int
p
,
@MatrixVariable
(
value
=
"q"
,
pathVar
=
"name"
)
int
q
,
@MatrixVariable
(
value
=
"q"
,
pathVar
=
"name"
)
int
q
,
@PathVariable
(
"another"
)
String
another
,
@MatrixVariable
(
value
=
"x"
,
pathVar
=
"another"
)
int
x
)
{
@PathVariable
(
"another"
)
String
another
,
@MatrixVariable
(
value
=
"x"
,
pathVar
=
"another"
)
int
x
)
{
return
name
+
"="
+
p
+
"-"
+
q
+
";"
+
another
+
"="
+
x
;
return
name
+
"="
+
p
+
"-"
+
q
+
";"
+
another
+
"="
+
x
;
}
}
...
@@ -275,7 +290,7 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
...
@@ -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
@Configuration
static
class
BadHostRibbonClientConfiguration
{
static
class
BadHostRibbonClientConfiguration
{
@Bean
@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 {
...
@@ -90,7 +90,8 @@ public class PreDecorationFilterTests {
this
.
filter
.
run
();
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/foo/1"
,
ctx
.
get
(
"requestURI"
));
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
(
"http"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-proto"
));
assertEquals
(
"/api"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"/api"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
assertEquals
(
"foo"
,
...
@@ -128,7 +129,7 @@ public class PreDecorationFilterTests {
...
@@ -128,7 +129,7 @@ public class PreDecorationFilterTests {
this
.
filter
.
run
();
this
.
filter
.
run
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
RequestContext
ctx
=
RequestContext
.
getCurrentContext
();
assertEquals
(
"/1"
,
ctx
.
get
(
"requestURI"
));
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
(
"http"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-proto"
));
assertEquals
(
"/api/foo"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"/api/foo"
,
ctx
.
getZuulRequestHeaders
().
get
(
"x-forwarded-prefix"
));
assertEquals
(
"foo"
,
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