MainController.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2012-2017 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. * http://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.http.HttpHeaders;
  18. import org.springframework.security.core.annotation.AuthenticationPrincipal;
  19. import org.springframework.security.oauth2.client.authentication.OAuth2UserAuthenticationToken;
  20. import org.springframework.security.oauth2.core.user.OAuth2User;
  21. import org.springframework.stereotype.Controller;
  22. import org.springframework.ui.Model;
  23. import org.springframework.util.StringUtils;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.reactive.function.client.ClientRequest;
  26. import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
  27. import org.springframework.web.reactive.function.client.WebClient;
  28. import reactor.core.publisher.Mono;
  29. import java.util.Collections;
  30. import java.util.Map;
  31. /**
  32. * @author Joe Grandja
  33. */
  34. @Controller
  35. public class MainController {
  36. @RequestMapping("/")
  37. public String index(Model model, @AuthenticationPrincipal OAuth2User user, OAuth2UserAuthenticationToken authentication) {
  38. model.addAttribute("userName", user.getName());
  39. model.addAttribute("clientName", authentication.getClientAuthentication().getClientRegistration().getClientName());
  40. return "index";
  41. }
  42. @RequestMapping("/userinfo")
  43. public String userinfo(Model model, OAuth2UserAuthenticationToken authentication) {
  44. Map userAttributes = Collections.emptyMap();
  45. String userInfoEndpointUri = authentication.getClientAuthentication().getClientRegistration()
  46. .getProviderDetails().getUserInfoEndpoint().getUri();
  47. if (!StringUtils.isEmpty(userInfoEndpointUri)) { // userInfoEndpointUri is optional for OIDC Clients
  48. userAttributes = WebClient.builder()
  49. .filter(oauth2Credentials(authentication))
  50. .build()
  51. .get()
  52. .uri(userInfoEndpointUri)
  53. .retrieve()
  54. .bodyToMono(Map.class)
  55. .block();
  56. }
  57. model.addAttribute("userAttributes", userAttributes);
  58. return "userinfo";
  59. }
  60. private ExchangeFilterFunction oauth2Credentials(OAuth2UserAuthenticationToken authentication) {
  61. return ExchangeFilterFunction.ofRequestProcessor(
  62. clientRequest -> {
  63. ClientRequest authorizedRequest = ClientRequest.from(clientRequest)
  64. .header(HttpHeaders.AUTHORIZATION, "Bearer " + authentication.getClientAuthentication().getAccessToken().getTokenValue())
  65. .build();
  66. return Mono.just(authorizedRequest);
  67. });
  68. }
  69. }