113 lines
20 KiB
HTML
113 lines
20 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>4. Route Predicate Factories</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="multi_spring-cloud-gateway.html" title="Spring Cloud Gateway"><link rel="up" href="multi_spring-cloud-gateway.html" title="Spring Cloud Gateway"><link rel="prev" href="multi_gateway-how-it-works.html" title="3. How It Works"><link rel="next" href="multi_gateway-route-filters.html" title="5. GatewayFilter Factories"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">4. Route Predicate Factories</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi_gateway-how-it-works.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="multi_gateway-route-filters.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="gateway-request-predicates-factories" href="#gateway-request-predicates-factories"></a>4. Route Predicate Factories</h1></div></div></div><p>Spring Cloud Gateway matches routes as part of the Spring WebFlux <code class="literal">HandlerMapping</code> infrastructure. Spring Cloud Gateway includes many built-in Route Predicate Factories. All of these predicates match on different attributes of the HTTP request. Multiple Route Predicate Factories can be combined and are combined via logical <code class="literal">and</code>.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_after_route_predicate_factory" href="#_after_route_predicate_factory"></a>4.1 After Route Predicate Factory</h2></div></div></div><p>The After Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen after the current datetime.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: after_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - After</span>=<span class="hl-number">2017</span>-<span class="hl-number">01</span>-<span class="hl-number">20</span>T17:<span class="hl-number">42</span>:<span class="hl-number">47.789</span>-<span class="hl-number">07</span>:<span class="hl-number">00</span>[America/Denver<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">]</span></pre><p>
|
|
</p><p>This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver).</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_before_route_predicate_factory" href="#_before_route_predicate_factory"></a>4.2 Before Route Predicate Factory</h2></div></div></div><p>The Before Route Predicate Factory takes one parameter, a datetime. This predicate matches requests that happen before the current datetime.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: before_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Before</span>=<span class="hl-number">2017</span>-<span class="hl-number">01</span>-<span class="hl-number">20</span>T17:<span class="hl-number">42</span>:<span class="hl-number">47.789</span>-<span class="hl-number">07</span>:<span class="hl-number">00</span>[America/Denver<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">]</span></pre><p>
|
|
</p><p>This route matches any request before Jan 20, 2017 17:42 Mountain Time (Denver).</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_between_route_predicate_factory" href="#_between_route_predicate_factory"></a>4.3 Between Route Predicate Factory</h2></div></div></div><p>The Between Route Predicate Factory takes two parameters, datetime1 and datetime2. This predicate matches requests that happen after datetime1 and before datetime2. The datetime2 parameter must be after datetime1.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: between_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Betweeen</span>=<span class="hl-number">2017</span>-<span class="hl-number">01</span>-<span class="hl-number">20</span>T17:<span class="hl-number">42</span>:<span class="hl-number">47.789</span>-<span class="hl-number">07</span>:<span class="hl-number">00</span>[America/Denver]<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span> <span class="hl-number">2017</span>-<span class="hl-number">01</span>-<span class="hl-number">21</span>T17:<span class="hl-number">42</span>:<span class="hl-number">47.789</span>-<span class="hl-number">07</span>:<span class="hl-number">00</span>[America/Denver<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">]</span></pre><p>
|
|
</p><p>This route matches any request after Jan 20, 2017 17:42 Mountain Time (Denver) and before Jan 21, 2017 17:42 Mountain Time (Denver). This could be useful for maintenance windows.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_cookie_route_predicate_factory" href="#_cookie_route_predicate_factory"></a>4.4 Cookie Route Predicate Factory</h2></div></div></div><p>The Cookie Route Predicate Factory takes two parameters, the cookie name and a regular expression. This predicate matches cookies that have the given name and the value matches the regular expression.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: cookie_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Cookie</span>=chocolate<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span> ch.p</pre><p>
|
|
</p><p>This route matches the request has a cookie named <code class="literal">chocolate</code> who’s value matches the <code class="literal">ch.p</code> regular expression.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_header_route_predicate_factory" href="#_header_route_predicate_factory"></a>4.5 Header Route Predicate Factory</h2></div></div></div><p>The Header Route Predicate Factory takes two parameters, the header name and a regular expression. This predicate matches with a header that has the given name and the value matches the regular expression.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: header_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Header</span>=X-Request-Id<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span> \d+</pre><p>
|
|
</p><p>This route matches if the request has a header named <code class="literal">X-Request-Id</code> whos value matches the <code class="literal">\d+</code> regular expression (has a value of one or more digits).</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_host_route_predicate_factory" href="#_host_route_predicate_factory"></a>4.6 Host Route Predicate Factory</h2></div></div></div><p>The Host Route Predicate Factory takes one parameter: the host name pattern. The pattern is an Ant style pattern with <code class="literal">.</code> as the separator. This predicates matches the <code class="literal">Host</code> header that matches the pattern.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: host_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Host</span>=**.somehost.org</pre><p>
|
|
</p><p>This route would match if the request has a <code class="literal">Host</code> header has the value <code class="literal">www.somehost.org</code> or <code class="literal">beta.somehost.org</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_method_route_predicate_factory" href="#_method_route_predicate_factory"></a>4.7 Method Route Predicate Factory</h2></div></div></div><p>The Method Route Predicate Factory takes one parameter: the HTTP method to match.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: method_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Method</span>=GET</pre><p>
|
|
</p><p>This route would match if the request method was a <code class="literal">GET</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_path_route_predicate_factory" href="#_path_route_predicate_factory"></a>4.8 Path Route Predicate Factory</h2></div></div></div><p>The Path Route Predicate Factory takes one parameter: a Spring <code class="literal">PathMatcher</code> pattern.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: host_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Path</span>=/foo/{segment<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span></pre><p>
|
|
</p><p>This route would match if the request path was, for example: <code class="literal">/foo/1</code> or <code class="literal">/foo/bar</code>.</p><p>This predicate extracts the URI template variables (like <code class="literal">segment</code> defined in the example above) as a map of names and values and places it in the <code class="literal">ServerWebExchange.getAttributes()</code> with a key defined in <code class="literal">PathRoutePredicate.URL_PREDICATE_VARS_ATTR</code>. Those values are then available for use by <a class="link" href="multi_gateway-route-filters.html" title="5. GatewayFilter Factories">GatewayFilter Factories</a></p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_query_route_predicate_factory" href="#_query_route_predicate_factory"></a>4.9 Query Route Predicate Factory</h2></div></div></div><p>The Query Route Predicate Factory takes two parameters: a required <code class="literal">param</code> and an optional <code class="literal">regexp</code>.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: query_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Query</span>=baz</pre><p>
|
|
</p><p>This route would match if the request contained a <code class="literal">baz</code> query parameter.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: query_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - Query</span>=foo<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span> ba.</pre><p>
|
|
</p><p>This route would match if the request contained a <code class="literal">foo</code> query parameter whose value matched the <code class="literal">ba.</code> regexp, so <code class="literal">bar</code> and <code class="literal">baz</code> would match.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_remoteaddr_route_predicate_factory" href="#_remoteaddr_route_predicate_factory"></a>4.10 RemoteAddr Route Predicate Factory</h2></div></div></div><p>The RemoteAddr Route Predicate Factory takes a list (min size 1) of CIDR-notation strings, e.g. <code class="literal">192.168.0.1/16</code> (where <code class="literal">192.168.0.1</code> is an IP address and <code class="literal">16</code> is a subnet mask.</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> cloud</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> gateway</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> routes</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> # </span>=====================================
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - id</span>: remoteaddr_route
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://example.org
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> predicates</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - RemoteAddr</span>=<span class="hl-number">192.168</span>.<span class="hl-number">1.1</span>/<span class="hl-number">24</span></pre><p>
|
|
</p><p>This route would match if the remote address of the request was, for example, <code class="literal">192.168.1.10</code>.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi_gateway-how-it-works.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="multi_gateway-route-filters.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3. How It Works </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-gateway.html">Home</a></td><td width="40%" align="right" valign="top"> 5. GatewayFilter Factories</td></tr></table></div></body></html> |