RegisteredOAuth2AuthorizedClientControllerTests.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  17. import okhttp3.mockwebserver.MockResponse;
  18. import okhttp3.mockwebserver.MockWebServer;
  19. import org.junit.jupiter.api.AfterAll;
  20. import org.junit.jupiter.api.Test;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
  23. import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
  24. import org.springframework.boot.test.mock.mockito.MockBean;
  25. import org.springframework.context.annotation.Bean;
  26. import org.springframework.context.annotation.Configuration;
  27. import org.springframework.context.annotation.Import;
  28. import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
  29. import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
  30. import org.springframework.security.oauth2.client.web.server.WebSessionServerOAuth2AuthorizedClientRepository;
  31. import org.springframework.test.web.reactive.server.WebTestClient;
  32. import org.springframework.web.reactive.function.client.WebClient;
  33. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOAuth2Client;
  34. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOAuth2Login;
  35. @WebFluxTest
  36. @Import({ SecurityConfiguration.class, RegisteredOAuth2AuthorizedClientController.class })
  37. @AutoConfigureWebTestClient
  38. public class RegisteredOAuth2AuthorizedClientControllerTests {
  39. private static MockWebServer web = new MockWebServer();
  40. @Autowired
  41. private WebTestClient client;
  42. @MockBean
  43. ReactiveClientRegistrationRepository clientRegistrationRepository;
  44. @AfterAll
  45. static void shutdown() throws Exception {
  46. web.shutdown();
  47. }
  48. @Test
  49. void annotationExplicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
  50. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  51. // @formatter:off
  52. this.client.mutateWith(mockOAuth2Login())
  53. .mutateWith(mockOAuth2Client("client-id"))
  54. .get()
  55. .uri("/annotation/explicit")
  56. .exchange()
  57. .expectStatus().isOk();
  58. // @formatter:on
  59. }
  60. @Test
  61. void annotationImplicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
  62. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  63. // @formatter:off
  64. this.client.mutateWith(mockOAuth2Login())
  65. .get()
  66. .uri("/annotation/implicit")
  67. .exchange()
  68. .expectStatus().isOk();
  69. // @formatter:on
  70. }
  71. @Test
  72. void publicAnnotationExplicitWhenAuthenticatedThenUsesClientIdRegistration() throws Exception {
  73. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  74. // @formatter:off
  75. this.client.mutateWith(mockOAuth2Client("client-id"))
  76. .get()
  77. .uri("/public/annotation/explicit")
  78. .exchange()
  79. .expectStatus().isOk();
  80. // @formatter:on
  81. }
  82. @Test
  83. void publicAnnotationImplicitWhenAuthenticatedThenUsesDefaultRegistration() throws Exception {
  84. web.enqueue(new MockResponse().setBody("body").setResponseCode(200));
  85. // @formatter:off
  86. this.client.mutateWith(mockOAuth2Login())
  87. .get()
  88. .uri("/public/annotation/implicit")
  89. .exchange()
  90. .expectStatus().isOk();
  91. // @formatter:on
  92. }
  93. @Configuration
  94. static class WebClientConfig {
  95. @Bean
  96. WebClient web() {
  97. return WebClient.create(web.url("/").toString());
  98. }
  99. @Bean
  100. ServerOAuth2AuthorizedClientRepository authorizedClientRepository() {
  101. return new WebSessionServerOAuth2AuthorizedClientRepository();
  102. }
  103. }
  104. }