Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import jakarta.servlet.ServletContext;
import org.springframework.beans.factory.ObjectProvider;
Expand Down Expand Up @@ -112,11 +113,11 @@ public static class ServletConfiguration {
public ApplicationFactory applicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, ObjectProvider<List<MetadataContributor>> metadataContributors,
DispatcherServletPath dispatcherServletPath) {
DispatcherServletPath dispatcherServletPath, ObjectProvider<?> inetUtilsProvider) {
return new ServletApplicationFactory(instance, management, server, servletContext, pathMappedEndpoints,
webEndpoint,
new CompositeMetadataContributor(metadataContributors.getIfAvailable(Collections::emptyList)),
dispatcherServletPath);
dispatcherServletPath, Optional.ofNullable(inetUtilsProvider.getIfAvailable()));
}

}
Expand All @@ -130,10 +131,11 @@ public static class ReactiveConfiguration {
@ConditionalOnMissingBean
public ApplicationFactory applicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
ObjectProvider<List<MetadataContributor>> metadataContributors, WebFluxProperties webFluxProperties) {
ObjectProvider<List<MetadataContributor>> metadataContributors, WebFluxProperties webFluxProperties,
ObjectProvider<?> inetUtilsProvider) {
return new ReactiveApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint,
new CompositeMetadataContributor(metadataContributors.getIfAvailable(Collections::emptyList)),
webFluxProperties);
webFluxProperties, Optional.ofNullable(inetUtilsProvider.getIfAvailable()));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.UnknownHostException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
Expand All @@ -45,6 +46,8 @@
*/
public class DefaultApplicationFactory implements ApplicationFactory {

// Removed InetUtils dependency; using standard Java APIs for host resolution.

private final InstanceProperties instance;

private final ServerProperties server;
Expand All @@ -57,6 +60,8 @@ public class DefaultApplicationFactory implements ApplicationFactory {

private final MetadataContributor metadataContributor;

private final Optional<?> inetUtils;

@Nullable
private Integer localServerPort;

Expand All @@ -72,6 +77,19 @@ public DefaultApplicationFactory(InstanceProperties instance, ManagementServerPr
this.pathMappedEndpoints = pathMappedEndpoints;
this.webEndpoint = webEndpoint;
this.metadataContributor = metadataContributor;
this.inetUtils = Optional.empty();
}

public DefaultApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor, Optional<?> inetUtils) {
this.instance = instance;
this.management = management;
this.server = server;
this.pathMappedEndpoints = pathMappedEndpoints;
this.webEndpoint = webEndpoint;
this.metadataContributor = metadataContributor;
this.inetUtils = (inetUtils != null) ? inetUtils : Optional.empty();
}

@Override
Expand Down Expand Up @@ -192,11 +210,31 @@ protected String getManagementHost() {
}

protected InetAddress getLocalHost() {
// Try using Spring Cloud Commons InetUtils if available (optional dependency)
if (this.inetUtils != null && this.inetUtils.isPresent()) {
Object utils = this.inetUtils.get();
try {
java.lang.reflect.Method m = utils.getClass().getMethod("findFirstNonLoopbackHostInfo");
Object host = m.invoke(utils);
if (host instanceof String && StringUtils.hasText((String) host)) {
try {
return InetAddress.getByName((String) host);
}
catch (UnknownHostException ex) {
// fall through to default
}
}
}
catch (Exception ex) {
// ignore and fall back
}
}

try {
return InetAddress.getLocalHost();
}
catch (UnknownHostException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
throw new IllegalStateException("Cannot determine local host address", ex);
}
}

Expand Down Expand Up @@ -236,11 +274,14 @@ protected String getHost(InetAddress address) {
return address.getHostAddress();
}

return switch (this.instance.getServiceHostType()) {
case IP -> address.getHostAddress();
case HOST_NAME -> address.getHostName();
default -> address.getCanonicalHostName();
};
switch (this.instance.getServiceHostType()) {
case IP:
return address.getHostAddress();
case HOST_NAME:
return address.getHostName();
default:
return address.getCanonicalHostName();
}
}

@EventListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package de.codecentric.boot.admin.client.registration;

import java.util.Optional;

import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
Expand All @@ -40,14 +42,22 @@ public class ReactiveApplicationFactory extends DefaultApplicationFactory {

public ReactiveApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor, WebFluxProperties webFluxProperties) {
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor);
MetadataContributor metadataContributor, WebFluxProperties webFluxProperties, Optional<?> inetUtils) {
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor, inetUtils);
this.management = management;
this.server = server;
this.webflux = webFluxProperties;
this.instance = instance;
}

// Backward-compatible constructor for tests and callers that don't provide InetUtils
public ReactiveApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor, WebFluxProperties webFluxProperties) {
this(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor, webFluxProperties,
Optional.empty());
}

@Override
protected String getServiceUrl() {
if (instance.getServiceUrl() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package de.codecentric.boot.admin.client.registration;

import java.util.Optional;

import jakarta.servlet.ServletContext;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
Expand Down Expand Up @@ -44,15 +46,24 @@ public class ServletApplicationFactory extends DefaultApplicationFactory {
public ServletApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, MetadataContributor metadataContributor,
DispatcherServletPath dispatcherServletPath) {
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor);
DispatcherServletPath dispatcherServletPath, Optional<?> inetUtils) {
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor, inetUtils);
this.servletContext = servletContext;
this.server = server;
this.management = management;
this.instance = instance;
this.dispatcherServletPath = dispatcherServletPath;
}

// Backward-compatible constructor for callers that don't provide InetUtils
public ServletApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, MetadataContributor metadataContributor,
DispatcherServletPath dispatcherServletPath) {
this(instance, management, server, servletContext, pathMappedEndpoints, webEndpoint, metadataContributor,
dispatcherServletPath, Optional.empty());
}

@Override
protected String getServiceUrl() {
if (instance.getServiceUrl() != null) {
Expand Down
3 changes: 2 additions & 1 deletion spring-boot-admin-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@

<modules>
<module>spring-boot-admin-sample-custom-ui</module>
<module>spring-boot-admin-sample-servlet</module>
<module>spring-boot-admin-sample-reactive</module>
<module>spring-boot-admin-sample-war</module>
<module>spring-boot-admin-sample-hazelcast</module>
<module>spring-boot-admin-sample-oauth2</module>
<module>spring-boot-admin-sample-oauth2-client</module>
</modules>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-samples</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-admin-sample-oauth2-client</artifactId>
<name>Spring Boot Admin OAuth2 Client Sample</name>
<description>Sample monitored app configured as OAuth2 Resource Server</description>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>de.codecentric.boot.admin.sample.client.OAuth2ClientApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.codecentric.boot.admin.sample.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;

@SpringBootApplication
public class OAuth2ClientApplication {

public static void main(String[] args) {
SpringApplication app = new SpringApplication(OAuth2ClientApplication.class);
app.setApplicationStartup(new BufferingApplicationStartup(1500));
app.run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.codecentric.boot.admin.sample.client;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class OAuth2ResourceServerConfig {

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(
(auth) -> auth.requestMatchers("/actuator/**").authenticated().anyRequest().permitAll())
.oauth2ResourceServer((oauth2) -> oauth2.jwt((jwt) -> {
}));
return http.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
spring:
application:
name: spring-boot-admin-sample-oauth2-client

security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://YOUR_ISSUER_URL

management:
endpoints:
web:
exposure:
include: "*"

spring:
boot:
admin:
client:
url: http://localhost:8080
Loading
Loading