AuthorizationController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2020-2024 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 jakarta.servlet.http.HttpServletRequest;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
  20. import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
  21. import org.springframework.security.oauth2.core.OAuth2Error;
  22. import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
  23. import org.springframework.stereotype.Controller;
  24. import org.springframework.ui.Model;
  25. import org.springframework.util.StringUtils;
  26. import org.springframework.web.bind.annotation.ExceptionHandler;
  27. import org.springframework.web.bind.annotation.GetMapping;
  28. import org.springframework.web.reactive.function.client.WebClient;
  29. import org.springframework.web.reactive.function.client.WebClientResponseException;
  30. import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId;
  31. import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
  32. /**
  33. * @author Joe Grandja
  34. * @since 0.0.1
  35. */
  36. @Controller
  37. public class AuthorizationController {
  38. private final WebClient webClient;
  39. private final String messagesBaseUri;
  40. private final String userMessagesBaseUri;
  41. public AuthorizationController(WebClient webClient,
  42. @Value("${messages.base-uri}") String messagesBaseUri,
  43. @Value("${user-messages.base-uri}") String userMessagesBaseUri) {
  44. this.webClient = webClient;
  45. this.messagesBaseUri = messagesBaseUri;
  46. this.userMessagesBaseUri = userMessagesBaseUri;
  47. }
  48. @GetMapping(value = "/authorize", params = "grant_type=authorization_code")
  49. public String authorizationCodeGrant(Model model,
  50. @RegisteredOAuth2AuthorizedClient("messaging-client-authorization-code")
  51. OAuth2AuthorizedClient authorizedClient) {
  52. String[] messages = this.webClient
  53. .get()
  54. .uri(this.messagesBaseUri)
  55. .attributes(oauth2AuthorizedClient(authorizedClient))
  56. .retrieve()
  57. .bodyToMono(String[].class)
  58. .block();
  59. model.addAttribute("messages", messages);
  60. return "index";
  61. }
  62. // '/authorized' is the registered 'redirect_uri' for authorization_code
  63. @GetMapping(value = "/authorized", params = OAuth2ParameterNames.ERROR)
  64. public String authorizationFailed(Model model, HttpServletRequest request) {
  65. String errorCode = request.getParameter(OAuth2ParameterNames.ERROR);
  66. if (StringUtils.hasText(errorCode)) {
  67. model.addAttribute("error",
  68. new OAuth2Error(
  69. errorCode,
  70. request.getParameter(OAuth2ParameterNames.ERROR_DESCRIPTION),
  71. request.getParameter(OAuth2ParameterNames.ERROR_URI))
  72. );
  73. }
  74. return "index";
  75. }
  76. @GetMapping(value = "/authorize", params = {"grant_type=client_credentials", "client_auth=client_secret"})
  77. public String clientCredentialsGrantUsingClientSecret(Model model) {
  78. String[] messages = this.webClient
  79. .get()
  80. .uri(this.messagesBaseUri)
  81. .attributes(clientRegistrationId("messaging-client-client-credentials"))
  82. .retrieve()
  83. .bodyToMono(String[].class)
  84. .block();
  85. model.addAttribute("messages", messages);
  86. return "index";
  87. }
  88. @GetMapping(value = "/authorize", params = {"grant_type=client_credentials", "client_auth=mtls"})
  89. public String clientCredentialsGrantUsingMutualTLS(Model model) {
  90. String[] messages = this.webClient
  91. .get()
  92. .uri(this.messagesBaseUri)
  93. .attributes(clientRegistrationId("mtls-demo-client-client-credentials"))
  94. .retrieve()
  95. .bodyToMono(String[].class)
  96. .block();
  97. model.addAttribute("messages", messages);
  98. return "index";
  99. }
  100. @GetMapping(value = "/authorize", params = "grant_type=token_exchange")
  101. public String tokenExchangeGrant(Model model) {
  102. String[] messages = this.webClient
  103. .get()
  104. .uri(this.userMessagesBaseUri)
  105. .attributes(clientRegistrationId("user-client-authorization-code"))
  106. .retrieve()
  107. .bodyToMono(String[].class)
  108. .block();
  109. model.addAttribute("messages", messages);
  110. return "index";
  111. }
  112. @GetMapping(value = "/authorize", params = "grant_type=device_code")
  113. public String deviceCodeGrant() {
  114. return "device-activate";
  115. }
  116. @ExceptionHandler(WebClientResponseException.class)
  117. public String handleError(Model model, WebClientResponseException ex) {
  118. model.addAttribute("error", ex.getMessage());
  119. return "index";
  120. }
  121. }