org.springframework.cloud.config.client.ConfigClientProperties Java Examples
The following examples show how to use
org.springframework.cloud.config.client.ConfigClientProperties.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SentinelRuleLocator.java From Sentinel with Apache License 2.0 | 7 votes |
private RestTemplate getSecureRestTemplate(ConfigClientProperties client) { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); if (client.getRequestReadTimeout() < 0) { throw new IllegalStateException("Invalid Value for Read Timeout set."); } requestFactory.setReadTimeout(client.getRequestReadTimeout()); RestTemplate template = new RestTemplate(requestFactory); Map<String, String> headers = new HashMap<>(client.getHeaders()); if (headers.containsKey(AUTHORIZATION)) { // To avoid redundant addition of header headers.remove(AUTHORIZATION); } if (!headers.isEmpty()) { template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList( new GenericRequestHeaderInterceptor(headers))); } return template; }
Example #2
Source File: MultiModuleConfigServicePropertySourceLocator.java From spring-cloud-formula with Apache License 2.0 | 6 votes |
private void loadDepEnvironments(org.springframework.core.env.Environment environment, CompositePropertySource composite, String state) { depApplications.forEach(dep -> { ConfigClientProperties properties = this.defaultProperties.override(environment); properties.setName(dep.getApplicationName()); RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties) : this.restTemplate; Environment result = getRemoteEnvironment(restTemplate, properties, "master", state); if (result != null) { logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s", result.getName(), result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), result.getLabel(), result.getVersion(), result.getState())); processApplicationResult(composite, result); } }); }
Example #3
Source File: DiscoveryClientConfigServiceAutoConfigurationTests.java From spring-cloud-consul with Apache License 2.0 | 6 votes |
@Test public void onWhenRequested() throws Exception { setup("server.port=0", "spring.cloud.config.discovery.enabled=true", "logging.level.org.springframework.cloud.config.client=DEBUG", "spring.cloud.consul.discovery.test.enabled:true", "spring.application.name=discoveryclientconfigservicetest", "spring.jmx.enabled=false", "spring.cloud.consul.discovery.port:7001", "spring.cloud.consul.discovery.hostname:foo", "spring.cloud.config.discovery.service-id:configserver"); assertThat(this.context .getBeanNamesForType(ConsulConfigServerAutoConfiguration.class).length) .isEqualTo(1); ConsulDiscoveryClient client = this.context.getParent() .getBean(ConsulDiscoveryClient.class); verify(client, atLeast(2)).getInstances("configserver"); ConfigClientProperties locator = this.context .getBean(ConfigClientProperties.class); assertThat(locator.getUri()[0]).isEqualTo("http://foo:7001/"); }
Example #4
Source File: HttpRequestConfigTokenProvider.java From spring-cloud-config with Apache License 2.0 | 6 votes |
@Override public String getToken() { HttpServletRequest request = httpRequest.getIfAvailable(); if (request == null) { throw new IllegalStateException("No HttpServletRequest available"); } String token = request.getHeader(ConfigClientProperties.TOKEN_HEADER); if (!StringUtils.hasLength(token)) { throw new IllegalArgumentException( "Missing required header in HttpServletRequest: " + ConfigClientProperties.TOKEN_HEADER); } return token; }
Example #5
Source File: DiscoveryClientConfigServiceAutoConfigurationTests.java From spring-cloud-zookeeper with Apache License 2.0 | 6 votes |
private void setup(String... env) { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); TestPropertyValues.of(env).applyTo(parent); parent.register(UtilAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EnvironmentKnobbler.class, ZookeeperDiscoveryClientConfigServiceBootstrapConfiguration.class, DiscoveryClientConfigServiceBootstrapConfiguration.class, ConfigClientProperties.class); parent.refresh(); this.context = new AnnotationConfigApplicationContext(); this.context.setParent(parent); this.context.register(PropertyPlaceholderAutoConfiguration.class, ZookeeperConfigServerAutoConfiguration.class, ZookeeperAutoConfiguration.class, ZookeeperDiscoveryClientConfiguration.class); this.context.refresh(); }
Example #6
Source File: DiscoveryClientConfigServiceAutoConfigurationTests.java From spring-cloud-zookeeper with Apache License 2.0 | 6 votes |
@Test public void onWhenRequested() { setup("server.port=7000", "spring.cloud.config.discovery.enabled=true", "spring.cloud.zookeeper.discovery.instance-port:7001", "spring.cloud.zookeeper.discovery.instance-host:foo", "spring.cloud.config.discovery.service-id:configserver"); assertThat(this.context .getBeanNamesForType(ZookeeperConfigServerAutoConfiguration.class).length) .isEqualTo(1); ZookeeperDiscoveryClient client = this.context.getParent() .getBean(ZookeeperDiscoveryClient.class); verify(client, atLeast(2)).getInstances("configserver"); ConfigClientProperties locator = this.context .getBean(ConfigClientProperties.class); assertThat(locator.getUri()[0]).isEqualTo("http://foo:7001/"); }
Example #7
Source File: ConfigClientAutoConfigResourceTest.java From spring-cloud-services-starters with Apache License 2.0 | 6 votes |
@Test public void plainTextConfigClientIsCreated() throws Exception { this.contextRunner.withPropertyValues("spring.cloud.config.client.oauth2.client-id=acme", "spring.cloud.config.client.oauth2.client-secret=acmesecret", "spring.cloud.config.client.oauth2.access-token-uri=acmetokenuri").run(context -> { assertThat(context).hasSingleBean(ConfigClientProperties.class); assertThat(context).hasSingleBean(OAuth2ConfigResourceClient.class); OAuth2ConfigResourceClient plainTextConfigClient = context .getBean(OAuth2ConfigResourceClient.class); RestTemplate restTemplate = (RestTemplate) ReflectionTestUtils.getField(plainTextConfigClient, "restTemplate"); assertThat(restTemplate).isNotNull(); assertThat(restTemplate.getInterceptors()).hasSize(1); assertThat(restTemplate.getInterceptors().get(0)) .isInstanceOf(OAuth2AuthorizedClientHttpRequestInterceptor.class); OAuth2AuthorizedClientHttpRequestInterceptor interceptor = (OAuth2AuthorizedClientHttpRequestInterceptor) restTemplate .getInterceptors().get(0); ClientRegistration clientRegistration = interceptor.clientRegistration; assertThat(clientRegistration.getClientId()).isEqualTo("acme"); assertThat(clientRegistration.getClientSecret()).isEqualTo("acmesecret"); assertThat(clientRegistration.getProviderDetails().getTokenUri()).isEqualTo("acmetokenuri"); assertThat(clientRegistration.getAuthorizationGrantType()) .isEqualTo(AuthorizationGrantType.CLIENT_CREDENTIALS); }); }
Example #8
Source File: VaultTokenRenewalAutoConfiguration.java From spring-cloud-services-starters with Apache License 2.0 | 6 votes |
@Bean public VaultTokenRefresher vaultTokenRefresher(ConfigClientProperties configClientProperties, ConfigClientOAuth2Properties configClientOAuth2Properties, @Qualifier("vaultTokenRenewal") RestTemplate restTemplate, @Value("${spring.cloud.config.token}") String vaultToken, // Default to a 300 second (5 minute) TTL @Value("${vault.token.ttl:300000}") long renewTTL) { ClientRegistration clientRegistration = ClientRegistration.withRegistrationId("config-client") .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) .clientId(configClientOAuth2Properties.getClientId()) .clientSecret(configClientOAuth2Properties.getClientSecret()) .tokenUri(configClientOAuth2Properties.getAccessTokenUri()).build(); restTemplate.getInterceptors().add(new OAuth2AuthorizedClientHttpRequestInterceptor(clientRegistration)); String obscuredToken = vaultToken.substring(0, 4) + "[*]" + vaultToken.substring(vaultToken.length() - 4); String refreshUri = configClientProperties.getUri()[0] + "/vault/v1/auth/token/renew-self"; // convert to seconds, since that's what Vault wants long renewTTLInMS = renewTTL / 1000; HttpEntity<Map<String, Long>> request = buildTokenRenewRequest(vaultToken, renewTTLInMS); return new VaultTokenRefresher(restTemplate, obscuredToken, renewTTL, refreshUri, request); }
Example #9
Source File: KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests.java From spring-cloud-kubernetes with Apache License 2.0 | 6 votes |
private void setup(String... env) { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); TestPropertyValues.of(env).applyTo(parent); parent.register(UtilAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EnvironmentKnobbler.class, KubernetesDiscoveryClientConfigClientBootstrapConfiguration.class, DiscoveryClientConfigServiceBootstrapConfiguration.class, ConfigClientProperties.class); parent.refresh(); this.context = new AnnotationConfigApplicationContext(); this.context.setParent(parent); this.context.register(PropertyPlaceholderAutoConfiguration.class, KubernetesAutoConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class); this.context.refresh(); }
Example #10
Source File: MultiModuleConfigServicePropertySourceLocator.java From spring-cloud-formula with Apache License 2.0 | 6 votes |
private RestTemplate getSecureRestTemplate(ConfigClientProperties properties) { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); if (properties.getRequestReadTimeout() < 0) { throw new IllegalStateException("Invalid Value for Real Timeout set."); } requestFactory.setReadTimeout(properties.getRequestReadTimeout()); RestTemplate template = new RestTemplate(requestFactory); Map<String, String> headers = new HashMap<>(properties.getHeaders()); headers.remove(AUTHORIZATION); // To avoid redundant addition of header if (!headers.isEmpty()) { template.setInterceptors(Collections.singletonList(new GenericRequestHeaderInterceptor(headers))); } return template; }
Example #11
Source File: SentinelRuleLocator.java From Sentinel with Apache License 2.0 | 6 votes |
private void addAuthorizationToken(ConfigClientProperties configClientProperties, HttpHeaders httpHeaders, String username, String password) { String authorization = configClientProperties.getHeaders().get(AUTHORIZATION); if (password != null && authorization != null) { throw new IllegalStateException( "You must set either 'password' or 'authorization'"); } if (password != null) { byte[] token = Base64Utils.encode((username + ":" + password).getBytes()); httpHeaders.add("Authorization", "Basic " + new String(token)); } else if (authorization != null) { httpHeaders.add("Authorization", authorization); } }
Example #12
Source File: KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests.java From spring-cloud-kubernetes with Apache License 2.0 | 5 votes |
@Test public void onWhenRequested() throws Exception { setup("server.port=7000", "spring.cloud.config.discovery.enabled=true", "spring.cloud.kubernetes.discovery.enabled:true", "spring.cloud.kubernetes.enabled:true", "spring.application.name:test", "spring.cloud.config.discovery.service-id:configserver"); assertEquals(1, this.context.getParent() .getBeanNamesForType(DiscoveryClient.class).length); DiscoveryClient client = this.context.getParent().getBean(DiscoveryClient.class); verify(client, atLeast(2)).getInstances("configserver"); ConfigClientProperties locator = this.context .getBean(ConfigClientProperties.class); assertEquals("http://fake:8888/", locator.getUri()[0]); }
Example #13
Source File: MultiModuleConfigServicePropertySourceLocator.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
public MultiModuleConfigServicePropertySourceLocator(ConfigClientProperties defaultProperties) { super(defaultProperties); this.defaultProperties = defaultProperties; // init depNames: this.depApplications = loadApplicationNames(Thread.currentThread().getContextClassLoader()); depApplications.sort(Comparator.comparingInt(ModuleConfiguration::getOrder)); }
Example #14
Source File: MultiModuleConfigServicePropertySourceLocator.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
private void addAuthorizationToken(ConfigClientProperties configClientProperties, HttpHeaders httpHeaders, String username, String password) { String authorization = configClientProperties.getHeaders().get(AUTHORIZATION); if (password != null && authorization != null) { throw new IllegalStateException("You must set either 'password' or 'authorization'"); } if (password != null) { byte[] token = Base64Utils.encode((username + ":" + password).getBytes()); httpHeaders.add("Authorization", "Basic " + new String(token)); } else if (authorization != null) { httpHeaders.add("Authorization", authorization); } }
Example #15
Source File: MultiModuleConfigServiceBootstrapConfiguration.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean(ConfigServicePropertySourceLocator.class) @ConditionalOnProperty(value = "spring.cloud.config.enabled", matchIfMissing = true) public ConfigServicePropertySourceLocator configServicePropertySource( ConfigClientProperties client, ObjectProvider<ConfigEnvironmentPreprocessor> preprocessorObjectProvider) { MultiModuleConfigServicePropertySourceLocator l = new MultiModuleConfigServicePropertySourceLocator(client); preprocessorObjectProvider.ifAvailable(l::setPreprocessor); return l; }
Example #16
Source File: OAuth2ConfigResourceClientTest.java From spring-cloud-services-starters with Apache License 2.0 | 5 votes |
@Before public void setUp() { configClientProperties = new ConfigClientProperties(new MockEnvironment()); configClientProperties.setName("app"); configClientProperties.setProfile(null); configClientProperties.setUri(new String[] { "http://localhost:" + port }); configClient = new OAuth2ConfigResourceClient(new RestTemplate(), configClientProperties); }
Example #17
Source File: ConfigResourceClientDefaultLabelTest.java From spring-cloud-services-starters with Apache License 2.0 | 5 votes |
@Before public void setUp() { ConfigClientProperties configClientProperties = new ConfigClientProperties(new MockEnvironment()); configClientProperties.setName("spring-application-name"); configClientProperties.setUri(new String[] { "http://localhost:" + port }); configClient = new OAuth2ConfigResourceClient(new RestTemplate(), configClientProperties); }
Example #18
Source File: ConfigClientAutoConfigResourceTest.java From spring-cloud-services-starters with Apache License 2.0 | 5 votes |
@Test public void plainTextConfigClientIsNotCreated() throws Exception { this.contextRunner.run(context -> { assertThat(context).hasSingleBean(ConfigClientProperties.class); assertThat(context).doesNotHaveBean(PlainTextConfigClient.class); }); }
Example #19
Source File: ConfigResourceClientAutoConfiguration.java From spring-cloud-services-starters with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean(ConfigResourceClient.class) @ConditionalOnProperty(prefix = "spring.cloud.config.client.oauth2", name = { "client-id", "client-secret", "access-token-uri" }) public ConfigResourceClient configResourceClient(ConfigClientProperties configClientProperties, ConfigClientOAuth2Properties configClientOAuth2Properties) { ClientRegistration clientRegistration = ClientRegistration.withRegistrationId("config-client") .clientId(configClientOAuth2Properties.getClientId()) .clientSecret(configClientOAuth2Properties.getClientSecret()) .tokenUri(configClientOAuth2Properties.getAccessTokenUri()) .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS).build(); RestTemplate restTemplate = new RestTemplate(); restTemplate.getInterceptors().add(new OAuth2AuthorizedClientHttpRequestInterceptor(clientRegistration)); return new OAuth2ConfigResourceClient(restTemplate, configClientProperties); }
Example #20
Source File: ConfigClientAutoConfigResourceTest.java From spring-cloud-services-connector with Apache License 2.0 | 5 votes |
@Test public void plainTextConfigClientIsCreated() throws Exception { this.contextRunner .withPropertyValues( "spring.cloud.config.client.oauth2.client-id=acme", "spring.cloud.config.client.oauth2.client-secret=acmesecret") .run(context -> { assertThat(context).hasSingleBean(ConfigClientOAuth2ResourceDetails.class); ConfigClientOAuth2ResourceDetails details = context.getBean(ConfigClientOAuth2ResourceDetails.class); assertThat(details.getClientId()).isNotEmpty().isEqualTo("acme"); assertThat(details.getClientSecret()).isNotEmpty().isEqualTo("acmesecret"); assertThat(context).hasSingleBean(ConfigClientProperties.class); assertThat(context).hasSingleBean(PlainTextConfigClient.class); }); }
Example #21
Source File: ConfigClientAutoConfigResourceTest.java From spring-cloud-services-connector with Apache License 2.0 | 5 votes |
@Test public void plainTextConfigClientIsNotCreated() throws Exception { this.contextRunner .run(context -> { assertThat(context).doesNotHaveBean(ConfigClientOAuth2ResourceDetails.class); assertThat(context).hasSingleBean(ConfigClientProperties.class); assertThat(context).doesNotHaveBean(PlainTextConfigClient.class); }); }
Example #22
Source File: ConfigResourceClientAutoConfiguration.java From spring-cloud-services-connector with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean(PlainTextConfigClient.class) @ConditionalOnProperty(prefix = "spring.cloud.config.client.oauth2", name = { "client-id", "client-secret" }) public ConfigResourceClient configResourceClient( ConfigClientOAuth2ResourceDetails resource, ConfigClientProperties configClientProperties) { return new OAuth2ConfigResourceClient(resource, configClientProperties); }
Example #23
Source File: VaultTokenRenewalAutoConfiguration.java From spring-cloud-services-connector with Apache License 2.0 | 5 votes |
@Autowired public VaultTokenRenewalAutoConfiguration( ConfigClientOAuth2ResourceDetails configClientOAuth2ResourceDetails, ConfigClientProperties configClientProps, @Value("${spring.cloud.config.token}") String vaultToken, @Value("${vault.token.ttl:300000}") long renewTTL) { // <-- Default to a 300 second (5 minute) TTL this.rest = new OAuth2RestTemplate(configClientOAuth2ResourceDetails); this.refreshUri = configClientProps.getUri()[0] + "/vault/v1/auth/token/renew-self"; long renewTTLInMS = renewTTL / 1000; // convert to seconds, since that's what Vault wants this.request = buildTokenRenewRequest(vaultToken, renewTTLInMS); this.obscuredToken = vaultToken.substring(0, 4) + "[*]" + vaultToken.substring(vaultToken.length() - 4); this.renewTTL = renewTTL; }
Example #24
Source File: ConfigClientConfiguration.java From onetwo with Apache License 2.0 | 5 votes |
/**** * copy form ConfigServicePropertySourceLocator#getSecureRestTemplate * @author weishao zeng * @param client * @return */ private RestTemplate getSecureRestTemplate(ConfigClientProperties client) { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setReadTimeout((60 * 1000 * 3) + 5000); //TODO 3m5s, make configurable? RestTemplate template = new RestTemplate(requestFactory); String username = client.getUsername(); String password = client.getPassword(); String authorization = client.getAuthorization(); Map<String, String> headers = new HashMap<>(client.getHeaders()); if (password != null && authorization != null) { throw new IllegalStateException( "You must set either 'password' or 'authorization'"); } if (password != null) { byte[] token = Base64Utils.encode((username + ":" + password).getBytes()); headers.put("Authorization", "Basic " + new String(token)); } else if (authorization != null) { headers.put("Authorization", authorization); } if (!headers.isEmpty()) { template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList( new GenericRequestHeaderInterceptor(headers))); } return template; }
Example #25
Source File: ConfigClientConfiguration.java From onetwo with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnProperty(value = "spring.cloud.config.enabled", matchIfMissing = true) public ConfigServicePropertySourceLocator configServicePropertySource(ConfigClientProperties properties) { ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator(properties); RestTemplate restTemplate = getSecureRestTemplate(properties); restTemplate.setMessageConverters(Arrays.asList(new ExtJackson2HttpMessageConverter())); locator.setRestTemplate(restTemplate); return locator; }
Example #26
Source File: SentinelRuleLocatorTests.java From Sentinel with Apache License 2.0 | 5 votes |
/** * Before run this test case, please start the Config Server ${@link ConfigServer} */ public void testLocate() { ConfigClientProperties configClientProperties = new ConfigClientProperties(environment); configClientProperties.setLabel("master"); configClientProperties.setProfile("dev"); configClientProperties.setUri(new String[]{"http://localhost:10086/"}); SentinelRuleLocator sentinelRulesSourceLocator = new SentinelRuleLocator(configClientProperties, environment); sentinelRulesSourceLocator.locate(environment); Assert.assertTrue(StringUtil.isNotBlank(SentinelRuleStorage.retrieveRule("flow_rule"))); }
Example #27
Source File: VaultTokenRenewalAutoConfigurationTest.java From spring-cloud-services-connector with Apache License 2.0 | 4 votes |
@Test public void contextLoads() { assertThat(context.getBeansOfType(ConfigClientProperties.class)).hasSize(1); }
Example #28
Source File: OAuth2ConfigResourceClient.java From spring-cloud-services-starters with Apache License 2.0 | 4 votes |
protected OAuth2ConfigResourceClient(RestTemplate restTemplate, final ConfigClientProperties configClientProperties) { this.restTemplate = restTemplate; this.configClientProperties = configClientProperties; }
Example #29
Source File: SentinelRuleLocator.java From Sentinel with Apache License 2.0 | 4 votes |
public SentinelRuleLocator(ConfigClientProperties defaultProperties, org.springframework.core.env.Environment environment) { this.defaultProperties = defaultProperties; this.environment = environment; }
Example #30
Source File: OAuth2ConfigResourceClient.java From spring-cloud-services-connector with Apache License 2.0 | 4 votes |
protected OAuth2ConfigResourceClient(final OAuth2ProtectedResourceDetails resource, final ConfigClientProperties configClientProperties) { this.restTemplate = new OAuth2RestTemplate(resource); this.configClientProperties = configClientProperties; }