|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
|
24 | 24 | import org.springframework.boot.actuate.autoconfigure.endpoint.expose.EndpointExposure;
|
25 | 25 | import org.springframework.boot.actuate.autoconfigure.endpoint.expose.IncludeExcludeEndpointFilter;
|
| 26 | +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType; |
26 | 27 | import org.springframework.boot.actuate.endpoint.EndpointAccessResolver;
|
27 | 28 | import org.springframework.boot.actuate.endpoint.EndpointFilter;
|
28 | 29 | import org.springframework.boot.actuate.endpoint.EndpointsSupplier;
|
|
33 | 34 | import org.springframework.boot.actuate.endpoint.web.AdditionalPathsMapper;
|
34 | 35 | import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
|
35 | 36 | import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
|
| 37 | +import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoint; |
36 | 38 | import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
|
37 | 39 | import org.springframework.boot.actuate.endpoint.web.PathMapper;
|
38 | 40 | import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
|
39 | 41 | import org.springframework.boot.actuate.endpoint.web.WebOperation;
|
| 42 | +import org.springframework.boot.actuate.endpoint.web.WebServerNamespace; |
40 | 43 | import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointDiscoverer;
|
41 | 44 | import org.springframework.boot.autoconfigure.AutoConfiguration;
|
42 | 45 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
47 | 50 | import org.springframework.context.ApplicationContext;
|
48 | 51 | import org.springframework.context.annotation.Bean;
|
49 | 52 | import org.springframework.context.annotation.Configuration;
|
| 53 | +import org.springframework.util.Assert; |
| 54 | +import org.springframework.util.StringUtils; |
50 | 55 |
|
51 | 56 | /**
|
52 | 57 | * {@link EnableAutoConfiguration Auto-configuration} for web {@link Endpoint @Endpoint}
|
53 | 58 | * support.
|
54 | 59 | *
|
55 | 60 | * @author Phillip Webb
|
56 | 61 | * @author Stephane Nicoll
|
| 62 | + * @author Yongjun Hong |
57 | 63 | * @since 2.0.0
|
58 | 64 | */
|
59 | 65 | @AutoConfiguration(after = EndpointAutoConfiguration.class)
|
@@ -109,7 +115,32 @@ public org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoi
|
109 | 115 | @Bean
|
110 | 116 | @ConditionalOnMissingBean
|
111 | 117 | public PathMappedEndpoints pathMappedEndpoints(Collection<EndpointsSupplier<?>> endpointSuppliers) {
|
112 |
| - return new PathMappedEndpoints(this.properties.getBasePath(), endpointSuppliers); |
| 118 | + String basePath = this.properties.getBasePath(); |
| 119 | + PathMappedEndpoints pathMappedEndpoints = new PathMappedEndpoints(basePath, endpointSuppliers); |
| 120 | + if ((!StringUtils.hasText(basePath) || "/".equals(basePath)) |
| 121 | + && ManagementPortType.get(this.applicationContext.getEnvironment()) == ManagementPortType.SAME) { |
| 122 | + assertHasNoRootPaths(pathMappedEndpoints); |
| 123 | + } |
| 124 | + return pathMappedEndpoints; |
| 125 | + } |
| 126 | + |
| 127 | + private void assertHasNoRootPaths(PathMappedEndpoints endpoints) { |
| 128 | + for (PathMappedEndpoint endpoint : endpoints) { |
| 129 | + if (endpoint instanceof ExposableWebEndpoint webEndpoint) { |
| 130 | + Assert.state(!isMappedToRootPath(webEndpoint), |
| 131 | + () -> "Management base path and the '" + webEndpoint.getEndpointId() |
| 132 | + + "' actuator endpoint are both mapped to '/' " |
| 133 | + + "on the server port which will block access to other endpoints. " |
| 134 | + + "Please use a different path for management endpoints or map them to a " |
| 135 | + + "dedicated management port."); |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private boolean isMappedToRootPath(PathMappedEndpoint endpoint) { |
| 142 | + return endpoint.getRootPath().equals("/") |
| 143 | + || endpoint.getAdditionalPaths(WebServerNamespace.SERVER).contains("/"); |
113 | 144 | }
|
114 | 145 |
|
115 | 146 | @Bean
|
|
0 commit comments