RegisteredOAuth2AuthorizedClientController.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2002-2020 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample.web;
  17. import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
  18. import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
  19. import org.springframework.stereotype.Controller;
  20. import org.springframework.ui.Model;
  21. import org.springframework.web.bind.annotation.GetMapping;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.reactive.function.client.WebClient;
  24. import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
  25. /**
  26. * @author Joe Grandja
  27. * @author Rob Winch
  28. */
  29. @Controller
  30. @RequestMapping(path = {"/annotation", "/public/annotation"})
  31. public class RegisteredOAuth2AuthorizedClientController {
  32. private final WebClient webClient;
  33. public RegisteredOAuth2AuthorizedClientController(WebClient webClient) {
  34. this.webClient = webClient;
  35. }
  36. @GetMapping("/explicit")
  37. String explicit(Model model, @RegisteredOAuth2AuthorizedClient("client-id") OAuth2AuthorizedClient authorizedClient) {
  38. String body = this.webClient
  39. .get()
  40. .attributes(oauth2AuthorizedClient(authorizedClient))
  41. .retrieve()
  42. .bodyToMono(String.class)
  43. .block();
  44. model.addAttribute("body", body);
  45. return "response";
  46. }
  47. @GetMapping("/implicit")
  48. String implicit(Model model, @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient) {
  49. String body = this.webClient
  50. .get()
  51. .attributes(oauth2AuthorizedClient(authorizedClient))
  52. .retrieve()
  53. .bodyToMono(String.class)
  54. .block();
  55. model.addAttribute("body", body);
  56. return "response";
  57. }
  58. }