|
| 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 | + |
| 17 | +package org.springframework.boot.autoconfigure.security.oauth2.client; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 26 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 27 | +import org.springframework.boot.test.util.TestPropertyValues; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Joe Grandja |
| 36 | + */ |
| 37 | +public class ClientRegistrationPropertiesBindTests { |
| 38 | + private AnnotationConfigWebApplicationContext context; |
| 39 | + |
| 40 | + @After |
| 41 | + public void close() { |
| 42 | + if (this.context != null) { |
| 43 | + this.context.close(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void bindMapOfClientRegistrationProperties() throws Exception { |
| 49 | + this.context = new AnnotationConfigWebApplicationContext(); |
| 50 | + this.context.register(DefaultConfiguration.class); |
| 51 | + |
| 52 | + // Prepare environment |
| 53 | + TestPropertyValues.of("security.oauth2.client.google.client-id=google-client-id") |
| 54 | + .and("security.oauth2.client.google.client-secret=google-client-secret") |
| 55 | + .and("security.oauth2.client.github.client-id=github-client-id") |
| 56 | + .and("security.oauth2.client.github.client-id=github-client-secret") |
| 57 | + .applyTo(this.context.getEnvironment()); |
| 58 | + |
| 59 | + this.context.refresh(); |
| 60 | + |
| 61 | + ClientRegistrationProperties googleClient = this.context |
| 62 | + .getBean(ClientRegistrationProperties.class); |
| 63 | + assertThat(googleClient).isNotNull(); |
| 64 | + assertThat(googleClient.getClientId()).isEqualTo("google-client-id"); |
| 65 | + assertThat(googleClient.getClientSecret()).isEqualTo("google-client-secret"); |
| 66 | + |
| 67 | + OAuth2ClientsProperties clients = this.context |
| 68 | + .getBean(OAuth2ClientsProperties.class); |
| 69 | + assertThat(clients).isNotNull(); |
| 70 | + assertThat(clients.getClients()).isNotEmpty(); |
| 71 | + } |
| 72 | + |
| 73 | + @Configuration |
| 74 | + @EnableConfigurationProperties(OAuth2ClientsProperties.class) |
| 75 | + public static class DefaultConfiguration { |
| 76 | + |
| 77 | + @ConfigurationProperties(prefix = "security.oauth2.client.google") |
| 78 | + @Bean |
| 79 | + public ClientRegistrationProperties googleClient() { |
| 80 | + return new ClientRegistrationProperties(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static class ClientRegistrationProperties { |
| 85 | + private String clientId; |
| 86 | + private String clientSecret; |
| 87 | + |
| 88 | + public String getClientId() { |
| 89 | + return this.clientId; |
| 90 | + } |
| 91 | + |
| 92 | + public void setClientId(String clientId) { |
| 93 | + this.clientId = clientId; |
| 94 | + } |
| 95 | + |
| 96 | + public String getClientSecret() { |
| 97 | + return this.clientSecret; |
| 98 | + } |
| 99 | + |
| 100 | + public void setClientSecret(String clientSecret) { |
| 101 | + this.clientSecret = clientSecret; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @ConfigurationProperties(prefix = "security.oauth2.client") |
| 106 | + public static class OAuth2ClientsProperties { |
| 107 | + private Map<String, ClientRegistrationProperties> clients = new HashMap<>(); |
| 108 | + |
| 109 | + public Map<String, ClientRegistrationProperties> getClients() { |
| 110 | + return this.clients; |
| 111 | + } |
| 112 | + |
| 113 | + public void setClients(Map<String, ClientRegistrationProperties> clients) { |
| 114 | + this.clients = clients; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments