bearer-tokens.adoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. = OAuth 2.0 Resource Server Bearer Tokens
  2. [[webflux-oauth2resourceserver-bearertoken-resolver]]
  3. == Bearer Token Resolution
  4. By default, Resource Server looks for a bearer token in the `Authorization` header.
  5. However, you can verify this token.
  6. For example, you may have a need to read the bearer token from a custom header.
  7. To do so, you can wire an instance of `ServerBearerTokenAuthenticationConverter` into the DSL:
  8. .Custom Bearer Token Header
  9. ====
  10. .Java
  11. [source,java,role="primary"]
  12. ----
  13. ServerBearerTokenAuthenticationConverter converter = new ServerBearerTokenAuthenticationConverter();
  14. converter.setBearerTokenHeaderName(HttpHeaders.PROXY_AUTHORIZATION);
  15. http
  16. .oauth2ResourceServer(oauth2 -> oauth2
  17. .bearerTokenConverter(converter)
  18. );
  19. ----
  20. .Kotlin
  21. [source,kotlin,role="secondary"]
  22. ----
  23. val converter = ServerBearerTokenAuthenticationConverter()
  24. converter.setBearerTokenHeaderName(HttpHeaders.PROXY_AUTHORIZATION)
  25. return http {
  26. oauth2ResourceServer {
  27. bearerTokenConverter = converter
  28. }
  29. }
  30. ----
  31. ====
  32. == Bearer Token Propagation
  33. Now that you have a bearer token, you can pass that to downstream services.
  34. This is possible with `{security-api-url}org/springframework/security/oauth2/server/resource/web/reactive/function/client/ServerBearerExchangeFilterFunction.html[ServerBearerExchangeFilterFunction]`:
  35. ====
  36. .Java
  37. [source,java,role="primary"]
  38. ----
  39. @Bean
  40. public WebClient rest() {
  41. return WebClient.builder()
  42. .filter(new ServerBearerExchangeFilterFunction())
  43. .build();
  44. }
  45. ----
  46. .Kotlin
  47. [source,kotlin,role="secondary"]
  48. ----
  49. @Bean
  50. fun rest(): WebClient {
  51. return WebClient.builder()
  52. .filter(ServerBearerExchangeFilterFunction())
  53. .build()
  54. }
  55. ----
  56. ====
  57. When the `WebClient` shown in the preceding example performs requests, Spring Security looks up the current `Authentication` and extract any `{security-api-url}org/springframework/security/oauth2/core/AbstractOAuth2Token.html[AbstractOAuth2Token]` credential.
  58. Then, it propagates that token in the `Authorization` header -- for example:
  59. ====
  60. .Java
  61. [source,java,role="primary"]
  62. ----
  63. this.rest.get()
  64. .uri("https://other-service.example.com/endpoint")
  65. .retrieve()
  66. .bodyToMono(String.class)
  67. ----
  68. .Kotlin
  69. [source,kotlin,role="secondary"]
  70. ----
  71. this.rest.get()
  72. .uri("https://other-service.example.com/endpoint")
  73. .retrieve()
  74. .bodyToMono<String>()
  75. ----
  76. ====
  77. The prececing example invokes the `https://other-service.example.com/endpoint`, adding the bearer token `Authorization` header for you.
  78. In places where you need to override this behavior, you can supply the header yourself:
  79. ====
  80. .Java
  81. [source,java,role="primary"]
  82. ----
  83. this.rest.get()
  84. .uri("https://other-service.example.com/endpoint")
  85. .headers(headers -> headers.setBearerAuth(overridingToken))
  86. .retrieve()
  87. .bodyToMono(String.class)
  88. ----
  89. .Kotlin
  90. [source,kotlin,role="secondary"]
  91. ----
  92. rest.get()
  93. .uri("https://other-service.example.com/endpoint")
  94. .headers { it.setBearerAuth(overridingToken) }
  95. .retrieve()
  96. .bodyToMono<String>()
  97. ----
  98. ====
  99. In this case, the filter falls back and forwards the request onto the rest of the web filter chain.
  100. [NOTE]
  101. ====
  102. Unlike the https://docs.spring.io/spring-security/site/docs/current-SNAPSHOT/api/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.html[OAuth 2.0 Client filter function], this filter function makes no attempt to renew the token, should it be expired.
  103. ====