211 lines
26 KiB
HTML
211 lines
26 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>Spring Cloud Security</title><link rel="stylesheet" type="text/css" href="css/manual-singlepage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div lang="en" class="book"><div class="titlepage"><div><div><h1 class="title"><a name="d0e3"></a>Spring Cloud Security</h1></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="preface"><a href="#d0e9"></a></span></dt><dt><span class="chapter"><a href="#_quickstart">1. Quickstart</a></span></dt><dd><dl><dt><span class="section"><a href="#_oauth2_single_sign_on">1.1. OAuth2 Single Sign On</a></span></dt><dt><span class="section"><a href="#_oauth2_protected_resource">1.2. OAuth2 Protected Resource</a></span></dt></dl></dd><dt><span class="chapter"><a href="#_more_detail">2. More Detail</a></span></dt><dd><dl><dt><span class="section"><a href="#_single_sign_on">2.1. Single Sign On</a></span></dt><dt><span class="section"><a href="#_token_relay">2.2. Token Relay</a></span></dt><dd><dl><dt><span class="section"><a href="#_client_token_relay_in_spring_cloud_gateway">2.2.1. Client Token Relay in Spring Cloud Gateway</a></span></dt><dt><span class="section"><a href="#_client_token_relay">2.2.2. Client Token Relay</a></span></dt><dt><span class="section"><a href="#_client_token_relay_in_zuul_proxy">2.2.3. Client Token Relay in Zuul Proxy</a></span></dt><dt><span class="section"><a href="#_resource_server_token_relay">2.2.4. Resource Server Token Relay</a></span></dt></dl></dd></dl></dd><dt><span class="chapter"><a href="#_configuring_authentication_downstream_of_a_zuul_proxy">3. Configuring Authentication Downstream of a Zuul Proxy</a></span></dt></dl></div><div class="preface"><div class="titlepage"><div><div><h1 class="title"><a name="d0e9" href="#d0e9"></a></h1></div></div></div><p>Spring Cloud Security offers a set of primitives for building secure
|
|
applications and services with minimum fuss. A declarative model which
|
|
can be heavily configured externally (or centrally) lends itself to
|
|
the implementation of large systems of co-operating, remote components,
|
|
usually with a central indentity management service. It is also extremely
|
|
easy to use in a service platform like Cloud Foundry. Building on
|
|
Spring Boot and Spring Security OAuth2 we can quickly create systems that
|
|
implement common patterns like single sign on, token relay and token
|
|
exchange.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Spring Cloud is released under the non-restrictive Apache 2.0 license. If you would like to contribute to this section of the documentation or if you find an error, please find the source code and issue trackers in the project at <a class="link" href="https://github.com/spring-cloud/spring-cloud-security/tree/master/src/main/asciidoc" target="_top">github</a>.</p></td></tr></table></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_quickstart" href="#_quickstart"></a>1. Quickstart</h1></div></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_oauth2_single_sign_on" href="#_oauth2_single_sign_on"></a>1.1 OAuth2 Single Sign On</h2></div></div></div><p>Here’s a Spring Cloud "Hello World" app with HTTP Basic
|
|
authentication and a single user account:</p><p><b>app.groovy. </b>
|
|
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Grab('spring-boot-starter-security')</xslthl:annotation>
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Controller</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Application {
|
|
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@RequestMapping('/')</xslthl:annotation>
|
|
String home() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">'Hello World'</span>
|
|
}
|
|
|
|
}</pre><p>
|
|
</p><p>You can run it with <code class="literal">spring run app.groovy</code> and watch the logs for the password (username is "user"). So far this is just the default for a Spring Boot app.</p><p>Here’s a Spring Cloud app with OAuth2 SSO:</p><p><b>app.groovy. </b>
|
|
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Controller</xslthl:annotation>
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@EnableOAuth2Sso</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Application {
|
|
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@RequestMapping('/')</xslthl:annotation>
|
|
String home() {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">'Hello World'</span>
|
|
}
|
|
|
|
}</pre><p>
|
|
</p><p>Spot the difference? This app will actually behave exactly the same as
|
|
the previous one, because it doesn’t know it’s OAuth2 credentals
|
|
yet.</p><p>You can register an app in github quite easily, so try that if you
|
|
want a production app on your own domain. If you are happy to test on
|
|
localhost:8080, then set up these properties in your application
|
|
configuration:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">security</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> oauth2</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> client</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> clientId</span>: bd1c0a783ccdd1c9b9e4
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> clientSecret</span>: <xslthl:number xmlns:xslthl="http://xslthl.sourceforge.net/">1</xslthl:number>a9030fbca47a5b2c28e92f19050bb77824b5ad1
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> accessTokenUri</span>: https://github.com/login/oauth/access_token
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> userAuthorizationUri</span>: https://github.com/login/oauth/authorize
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> clientAuthenticationScheme</span>: form
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> resource</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> userInfoUri</span>: https://api.github.com/user
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> preferTokenInfo</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span></pre><p>
|
|
</p><p>run the app above and it will redirect to github for authorization. If
|
|
you are already signed into github you won’t even notice that it has
|
|
authenticated. These credentials will only work if your app is
|
|
running on port 8080.</p><p>To limit the scope that the client asks for when it obtains an access token
|
|
you can set <code class="literal">security.oauth2.client.scope</code> (comma separated or an array in YAML). By
|
|
default the scope is empty and it is up to to Authorization Server to
|
|
decide what the defaults should be, usually depending on the settings in
|
|
the client registration that it holds.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The examples above are all Groovy scripts. If you want to write the
|
|
same code in Java (or Groovy) you need to add Spring Security OAuth2
|
|
to the classpath (e.g. see the
|
|
<a class="link" href="https://github.com/spring-cloud-samples/sso" target="_top">sample here</a>).</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_oauth2_protected_resource" href="#_oauth2_protected_resource"></a>1.2 OAuth2 Protected Resource</h2></div></div></div><p>You want to protect an API resource with an OAuth2 token? Here’s a
|
|
simple example (paired with the client above):</p><p><b>app.groovy. </b>
|
|
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Grab('spring-cloud-starter-security')</xslthl:annotation>
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@RestController</xslthl:annotation>
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@EnableResourceServer</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Application {
|
|
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@RequestMapping('/')</xslthl:annotation>
|
|
def home() {
|
|
[message: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">'Hello World'</span>]
|
|
}
|
|
|
|
}</pre><p>
|
|
</p><p>and</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">security</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> oauth2</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> resource</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> userInfoUri</span>: https://api.github.com/user
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> preferTokenInfo</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span></pre><p>
|
|
</p></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_more_detail" href="#_more_detail"></a>2. More Detail</h1></div></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_single_sign_on" href="#_single_sign_on"></a>2.1 Single Sign On</h2></div></div></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>All of the OAuth2 SSO and resource server features moved to Spring Boot
|
|
in version 1.3. You can find documentation in the
|
|
<a class="link" href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/" target="_top">Spring Boot user guide</a>.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_token_relay" href="#_token_relay"></a>2.2 Token Relay</h2></div></div></div><p>A Token Relay is where an OAuth2 consumer acts as a Client and
|
|
forwards the incoming token to outgoing resource requests. The
|
|
consumer can be a pure Client (like an SSO application) or a Resource
|
|
Server.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_client_token_relay_in_spring_cloud_gateway" href="#_client_token_relay_in_spring_cloud_gateway"></a>2.2.1 Client Token Relay in Spring Cloud Gateway</h3></div></div></div><p>If your app also has a
|
|
<a class="link" href="https://cloud.spring.io/spring-cloud-static/current/single/spring-cloud.html#_spring_cloud_gateway" target="_top">Spring
|
|
Cloud Gateway</a> embedded reverse proxy then you
|
|
can ask it to forward OAuth2 access tokens downstream to the services
|
|
it is proxying. Thus the SSO app above can be enhanced simply like
|
|
this:</p><p><b>App.java. </b>
|
|
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Autowired</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> TokenRelayGatewayFilterFactory filterFactory;
|
|
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Bean</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> builder.routes()
|
|
.route(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"resource"</span>, r -> r.path(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"/resource"</span>)
|
|
.filters(f -> f.filter(filterFactory.apply()))
|
|
.uri(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"http://localhost:9000"</span>))
|
|
.build();
|
|
}</pre><p>
|
|
</p><p>or this</p><p><b>application.yaml. </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"> - id</span>: resource
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> uri</span>: http://localhost:<xslthl:number xmlns:xslthl="http://xslthl.sourceforge.net/">9000</xslthl:number>
|
|
<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>=/resource
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> filters</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> - TokenRelay</span>=</pre><p>
|
|
</p><p>and it will (in addition to logging the user in and grabbing a token)
|
|
pass the authentication token downstream to the services (in this case
|
|
<code class="literal">/resource</code>).</p><p>To enable this for Spring Cloud Gateway add the following dependencies</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">org.springframework.boot:spring-boot-starter-oauth2-client</code></li><li class="listitem"><code class="literal">org.springframework.cloud:spring-cloud-starter-security</code></li></ul></div><p>How does it work? The
|
|
<a class="link" href="https://github.com/spring-cloud/spring-cloud-security/tree/master/src/main/java/org/springframework/cloud/security/oauth2/gateway/TokenRelayGatewayFilterFactory.java" target="_top">filter</a>
|
|
extracts an access token from the currently authenticated user,
|
|
and puts it in a request header for the downstream requests.</p><p>For a full working sample see <a class="link" href="https://github.com/spring-cloud-samples/sample-gateway-oauth2login" target="_top">this project</a>.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The default implementation of <code class="literal">ReactiveOAuth2AuthorizedClientService</code> used by <code class="literal">TokenRelayGatewayFilterFactory</code>
|
|
uses an in-memory data store. You will need to provide your own implementation <code class="literal">ReactiveOAuth2AuthorizedClientService</code>
|
|
if you need a more robust solution.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_client_token_relay" href="#_client_token_relay"></a>2.2.2 Client Token Relay</h3></div></div></div><p>If your app is a user facing OAuth2 client (i.e. has declared
|
|
<code class="literal">@EnableOAuth2Sso</code> or <code class="literal">@EnableOAuth2Client</code>) then it has an
|
|
<code class="literal">OAuth2ClientContext</code> in request scope from Spring Boot. You can
|
|
create your own <code class="literal">OAuth2RestTemplate</code> from this context and an
|
|
autowired <code class="literal">OAuth2ProtectedResourceDetails</code>, and then the context will
|
|
always forward the access token downstream, also refreshing the access
|
|
token automatically if it expires. (These are features of Spring
|
|
Security and Spring Boot.)</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Spring Boot (1.4.1) does not create an
|
|
<code class="literal">OAuth2ProtectedResourceDetails</code> automatically if you are using
|
|
<code class="literal">client_credentials</code> tokens. In that case you need to create your own
|
|
<code class="literal">ClientCredentialsResourceDetails</code> and configure it with
|
|
<code class="literal">@ConfigurationProperties("security.oauth2.client")</code>.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_client_token_relay_in_zuul_proxy" href="#_client_token_relay_in_zuul_proxy"></a>2.2.3 Client Token Relay in Zuul Proxy</h3></div></div></div><p>If your app also has a
|
|
<a class="link" href="https://cloud.spring.io/spring-cloud.html#netflix-zuul-reverse-proxy" target="_top">Spring
|
|
Cloud Zuul</a> embedded reverse proxy (using <code class="literal">@EnableZuulProxy</code>) then you
|
|
can ask it to forward OAuth2 access tokens downstream to the services
|
|
it is proxying. Thus the SSO app above can be enhanced simply like
|
|
this:</p><p><b>app.groovy. </b>
|
|
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Controller</xslthl:annotation>
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@EnableOAuth2Sso</xslthl:annotation>
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@EnableZuulProxy</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Application {
|
|
|
|
}</pre><p>
|
|
</p><p>and it will (in addition to logging the user in and grabbing a token)
|
|
pass the authentication token downstream to the <code class="literal">/proxy/*</code>
|
|
services. If those services are implemented with
|
|
<code class="literal">@EnableResourceServer</code> then they will get a valid token in the
|
|
correct header.</p><p>How does it work? The <code class="literal">@EnableOAuth2Sso</code> annotation pulls in
|
|
<code class="literal">spring-cloud-starter-security</code> (which you could do manually in a
|
|
traditional app), and that in turn triggers some autoconfiguration for
|
|
a <code class="literal">ZuulFilter</code>, which itself is activated because Zuul is on the
|
|
classpath (via <code class="literal">@EnableZuulProxy</code>). The
|
|
<a class="link" href="https://github.com/spring-cloud/spring-cloud-security/tree/master/src/main/java/org/springframework/cloud/security/oauth2/proxy/OAuth2TokenRelayFilter.java" target="_top">filter</a>
|
|
just extracts an access token from the currently authenticated user,
|
|
and puts it in a request header for the downstream requests.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Spring Boot does not create an <code class="literal">OAuth2RestOperations</code> automatically which is needed for <code class="literal">refresh_token</code>. In that case you need to create your own
|
|
<code class="literal">OAuth2RestOperations</code> so <code class="literal">OAuth2TokenRelayFilter</code> can refresh the token if needed.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_resource_server_token_relay" href="#_resource_server_token_relay"></a>2.2.4 Resource Server Token Relay</h3></div></div></div><p>If your app has <code class="literal">@EnableResourceServer</code> you might want to relay the
|
|
incoming token downstream to other services. If you use a
|
|
<code class="literal">RestTemplate</code> to contact the downstream services then this is just a
|
|
matter of how to create the template with the right context.</p><p>If your service uses <code class="literal">UserInfoTokenServices</code> to authenticate incoming
|
|
tokens (i.e. it is using the <code class="literal">security.oauth2.user-info-uri</code>
|
|
configuration), then you can simply create an <code class="literal">OAuth2RestTemplate</code>
|
|
using an autowired <code class="literal">OAuth2ClientContext</code> (it will be populated by the
|
|
authentication process before it hits the backend code). Equivalently
|
|
(with Spring Boot 1.4), you could inject a
|
|
<code class="literal">UserInfoRestTemplateFactory</code> and grab its <code class="literal">OAuth2RestTemplate</code> in
|
|
your configuration. For example:</p><p><b>MyConfiguration.java. </b>
|
|
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Bean</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> OAuth2RestTemplate restTemplate(UserInfoRestTemplateFactory factory) {
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> factory.getUserInfoRestTemplate();
|
|
}</pre><p>
|
|
</p><p>This rest template will then have the same <code class="literal">OAuth2ClientContext</code>
|
|
(request-scoped) that is used by the authentication filter, so you can
|
|
use it to send requests with the same access token.</p><p>If your app is not using <code class="literal">UserInfoTokenServices</code> but is still a client
|
|
(i.e. it declares <code class="literal">@EnableOAuth2Client</code> or <code class="literal">@EnableOAuth2Sso</code>), then
|
|
with Spring Security Cloud any <code class="literal">OAuth2RestOperations</code> that the user
|
|
creates from an <code class="literal">@Autowired</code> <code class="literal">OAuth2Context</code> will also forward
|
|
tokens. This feature is implemented by default as an MVC handler
|
|
interceptor, so it only works in Spring MVC. If you are not using MVC
|
|
you could use a custom filter or AOP interceptor wrapping an
|
|
<code class="literal">AccessTokenContextRelay</code> to provide the same feature.</p><p>Here’s a basic
|
|
example showing the use of an autowired rest template created
|
|
elsewhere ("foo.com" is a Resource Server accepting the same tokens as
|
|
the surrounding app):</p><p><b>MyController.java. </b>
|
|
</p><pre class="programlisting"><xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@Autowired</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> OAuth2RestOperations restTemplate;
|
|
|
|
<xslthl:annotation xmlns:xslthl="http://xslthl.sourceforge.net/">@RequestMapping("/relay")</xslthl:annotation>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String relay() {
|
|
ResponseEntity<String> response =
|
|
restTemplate.getForEntity(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"https://foo.com/bar"</span>, String.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>);
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Success! ("</span> + response.getBody() + <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">")"</span>;
|
|
}</pre><p>
|
|
</p><p>If you don’t want to forward tokens (and that is a valid
|
|
choice, since you might want to act as yourself, rather than the
|
|
client that sent you the token), then you only need to create your own
|
|
<code class="literal">OAuth2Context</code> instead of autowiring the default one.</p><p>Feign clients will also pick up an interceptor that uses the
|
|
<code class="literal">OAuth2ClientContext</code> if it is available, so they should also do a
|
|
token relay anywhere where a <code class="literal">RestTemplate</code> would.</p></div></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_configuring_authentication_downstream_of_a_zuul_proxy" href="#_configuring_authentication_downstream_of_a_zuul_proxy"></a>3. Configuring Authentication Downstream of a Zuul Proxy</h1></div></div></div><p>You can control the authorization behaviour downstream of an
|
|
<code class="literal">@EnableZuulProxy</code> through the <code class="literal">proxy.auth.*</code> settings. Example:</p><p><b>application.yml. </b>
|
|
</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">proxy</span>:
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> auth</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"> customers</span>: oauth2
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> stores</span>: passthru
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> recommendations</span>: none</pre><p>
|
|
</p><p>In this example the "customers" service gets an OAuth2 token relay,
|
|
the "stores" service gets a passthrough (the authorization header is
|
|
just passed downstream), and the "recommendations" service has its
|
|
authorization header removed. The default behaviour is to do a token
|
|
relay if there is a token available, and passthru otherwise.</p><p>See
|
|
<a class="link" href="https://github.com/spring-cloud/spring-cloud-security/tree/master/src/main/java/org/springframework/cloud/security/oauth2/proxy/ProxyAuthenticationProperties" target="_top">
|
|
ProxyAuthenticationProperties</a> for full details.</p></div></div></body></html> |