bearer-tokens.adoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. [tabs]
  10. ======
  11. Java::
  12. +
  13. [source,java,role="primary"]
  14. ----
  15. ServerBearerTokenAuthenticationConverter converter = new ServerBearerTokenAuthenticationConverter();
  16. converter.setBearerTokenHeaderName(HttpHeaders.PROXY_AUTHORIZATION);
  17. http
  18. .oauth2ResourceServer(oauth2 -> oauth2
  19. .bearerTokenConverter(converter)
  20. );
  21. ----
  22. Kotlin::
  23. +
  24. [source,kotlin,role="secondary"]
  25. ----
  26. val converter = ServerBearerTokenAuthenticationConverter()
  27. converter.setBearerTokenHeaderName(HttpHeaders.PROXY_AUTHORIZATION)
  28. return http {
  29. oauth2ResourceServer {
  30. bearerTokenConverter = converter
  31. }
  32. }
  33. ----
  34. ======
  35. == Bearer Token Propagation
  36. Now that you have a bearer token, you can pass that to downstream services.
  37. This is possible with javadoc:org.springframework.security.oauth2.server.resource.web.reactive.function.client.ServerBearerExchangeFilterFunction[]:
  38. [tabs]
  39. ======
  40. Java::
  41. +
  42. [source,java,role="primary"]
  43. ----
  44. @Bean
  45. public WebClient rest() {
  46. return WebClient.builder()
  47. .filter(new ServerBearerExchangeFilterFunction())
  48. .build();
  49. }
  50. ----
  51. Kotlin::
  52. +
  53. [source,kotlin,role="secondary"]
  54. ----
  55. @Bean
  56. fun rest(): WebClient {
  57. return WebClient.builder()
  58. .filter(ServerBearerExchangeFilterFunction())
  59. .build()
  60. }
  61. ----
  62. ======
  63. When the `WebClient` shown in the preceding example performs requests, Spring Security looks up the current `Authentication` and extract any javadoc:org.springframework.security.oauth2.core.AbstractOAuth2Token[] credential.
  64. Then, it propagates that token in the `Authorization` header -- for example:
  65. [tabs]
  66. ======
  67. Java::
  68. +
  69. [source,java,role="primary"]
  70. ----
  71. this.rest.get()
  72. .uri("https://other-service.example.com/endpoint")
  73. .retrieve()
  74. .bodyToMono(String.class)
  75. ----
  76. Kotlin::
  77. +
  78. [source,kotlin,role="secondary"]
  79. ----
  80. this.rest.get()
  81. .uri("https://other-service.example.com/endpoint")
  82. .retrieve()
  83. .bodyToMono<String>()
  84. ----
  85. ======
  86. The prececing example invokes the `https://other-service.example.com/endpoint`, adding the bearer token `Authorization` header for you.
  87. In places where you need to override this behavior, you can supply the header yourself:
  88. [tabs]
  89. ======
  90. Java::
  91. +
  92. [source,java,role="primary"]
  93. ----
  94. this.rest.get()
  95. .uri("https://other-service.example.com/endpoint")
  96. .headers(headers -> headers.setBearerAuth(overridingToken))
  97. .retrieve()
  98. .bodyToMono(String.class)
  99. ----
  100. Kotlin::
  101. +
  102. [source,kotlin,role="secondary"]
  103. ----
  104. rest.get()
  105. .uri("https://other-service.example.com/endpoint")
  106. .headers { it.setBearerAuth(overridingToken) }
  107. .retrieve()
  108. .bodyToMono<String>()
  109. ----
  110. ======
  111. In this case, the filter falls back and forwards the request onto the rest of the web filter chain.
  112. [NOTE]
  113. ====
  114. 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.
  115. ====