RegisteredOAuth2AuthorizedClientController.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2002-2018 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 reactor.core.publisher.Mono;
  18. import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
  19. import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.ui.Model;
  22. import org.springframework.web.bind.annotation.GetMapping;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.reactive.function.client.WebClient;
  25. import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
  26. /**
  27. * @author Joe Grandja
  28. * @author Rob Winch
  29. */
  30. @Controller
  31. @RequestMapping(path = {"/annotation", "/public/annotation"})
  32. public class RegisteredOAuth2AuthorizedClientController {
  33. private final WebClient webClient;
  34. public RegisteredOAuth2AuthorizedClientController(WebClient webClient) {
  35. this.webClient = webClient;
  36. }
  37. @GetMapping("/explicit")
  38. String explicit(Model model, @RegisteredOAuth2AuthorizedClient("client-id") OAuth2AuthorizedClient authorizedClient) {
  39. Mono<String> body = this.webClient
  40. .get()
  41. .attributes(oauth2AuthorizedClient(authorizedClient))
  42. .retrieve()
  43. .bodyToMono(String.class);
  44. model.addAttribute("body", body);
  45. return "response";
  46. }
  47. @GetMapping("/implicit")
  48. String implicit(Model model, @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient) {
  49. Mono<String> body = this.webClient
  50. .get()
  51. .attributes(oauth2AuthorizedClient(authorizedClient))
  52. .retrieve()
  53. .bodyToMono(String.class);
  54. model.addAttribute("body", body);
  55. return "response";
  56. }
  57. }