Skip to content

Commit b02aba4

Browse files
committed
Polish contribution
Closes gh-5074
1 parent 20fa1b3 commit b02aba4

File tree

7 files changed

+70
-14
lines changed

7 files changed

+70
-14
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CrshAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public SpringAuthenticationProperties springAuthenticationProperties() {
196196
// overridden by ConfigurationProperties.
197197
SpringAuthenticationProperties authenticationProperties = new SpringAuthenticationProperties();
198198
if (this.management != null) {
199-
List<String> roles = this.management.getSecurity().getRole();
199+
List<String> roles = this.management.getSecurity().getRoles();
200200
authenticationProperties.setRoles(roles.toArray(new String[roles.size()]));
201201
}
202202
return authenticationProperties;

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
package org.springframework.boot.actuate.autoconfigure;
1818

1919
import java.net.InetAddress;
20-
import java.util.ArrayList;
21-
import java.util.Arrays;
20+
import java.util.Collections;
2221
import java.util.List;
2322

2423
import javax.validation.constraints.NotNull;
@@ -164,9 +163,9 @@ public static class Security {
164163
private boolean enabled = true;
165164

166165
/**
167-
* Roles required to access the management endpoint.
166+
* Comma-separated list of roles that can access the management endpoint.
168167
*/
169-
private List<String> role = new ArrayList<String>(Arrays.asList("ADMIN"));
168+
private List<String> roles = Collections.singletonList("ADMIN");
170169

171170
/**
172171
* Session creating policy to use (always, never, if_required, stateless).
@@ -181,12 +180,17 @@ public void setSessions(SessionCreationPolicy sessions) {
181180
this.sessions = sessions;
182181
}
183182

184-
public void setRole(List<String> role) {
185-
this.role = role;
183+
public void setRoles(List<String> roles) {
184+
this.roles = roles;
186185
}
187186

188-
public List<String> getRole() {
189-
return this.role;
187+
@Deprecated
188+
public void setRole(String role) {
189+
this.roles = Collections.singletonList(role);
190+
}
191+
192+
public List<String> getRoles() {
193+
return this.roles;
190194
}
191195

192196
public boolean isEnabled() {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementWebSecurityAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public ManagementSecurityPropertiesConfiguration(
124124
public void init() {
125125
if (this.management != null && this.security != null) {
126126
this.security.getUser().getRole()
127-
.addAll(this.management.getSecurity().getRole());
127+
.addAll(this.management.getSecurity().getRoles());
128128
}
129129
}
130130

@@ -297,7 +297,7 @@ private void configurePermittedRequests(
297297
requests.requestMatchers(new LazyEndpointPathRequestMatcher(
298298
this.contextResolver, EndpointPaths.NON_SENSITIVE)).permitAll();
299299
// Restrict the rest to the configured roles
300-
List<String> roles = this.management.getSecurity().getRole();
300+
List<String> roles = this.management.getSecurity().getRoles();
301301
requests.anyRequest().hasAnyRole(roles.toArray(new String[roles.size()]));
302302
}
303303

spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@
163163
"description": "Enable git info.",
164164
"defaultValue": true
165165
},
166+
{
167+
"name": "management.security.role",
168+
"type": "java.lang.String",
169+
"description": "Roles required to access the management endpoint.",
170+
"defaultValue": "ADMIN",
171+
"deprecation": {
172+
"replacement": "management.security.roles"
173+
}
174+
},
166175
{
167176
"name": "spring.git.properties",
168177
"type": "java.lang.String",

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfigurationTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure;
1818

19+
import org.junit.After;
1920
import org.junit.Test;
2021

22+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
23+
import org.springframework.boot.test.util.EnvironmentTestUtils;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
import org.springframework.context.annotation.Configuration;
26+
2127
import static org.assertj.core.api.Assertions.assertThat;
2228

2329
/**
@@ -28,6 +34,15 @@
2834
*/
2935
public class ManagementServerPropertiesAutoConfigurationTests {
3036

37+
private AnnotationConfigApplicationContext context;
38+
39+
@After
40+
public void close() {
41+
if (this.context != null) {
42+
this.context.close();
43+
}
44+
}
45+
3146
@Test
3247
public void defaultManagementServerProperties() {
3348
ManagementServerProperties properties = new ManagementServerProperties();
@@ -58,4 +73,32 @@ public void slashOfContextPathIsDefaultValue() {
5873
assertThat(properties.getContextPath()).isEqualTo("");
5974
}
6075

76+
@Test
77+
@Deprecated
78+
public void managementRoleSetRolesProperly() {
79+
ManagementServerProperties properties = load("management.security.role=FOO");
80+
assertThat(properties.getSecurity().getRoles()).containsOnly("FOO");
81+
}
82+
83+
@Test
84+
public void managementRolesSetMultipleRoles() {
85+
ManagementServerProperties properties = load("management.security.roles=FOO,BAR,BIZ");
86+
assertThat(properties.getSecurity().getRoles()).containsOnly("FOO", "BAR", "BIZ");
87+
}
88+
89+
public ManagementServerProperties load(String... environment) {
90+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
91+
EnvironmentTestUtils.addEnvironment(ctx, environment);
92+
ctx.register(TestConfiguration.class);
93+
ctx.refresh();
94+
this.context = ctx;
95+
return this.context.getBean(ManagementServerProperties.class);
96+
}
97+
98+
@Configuration
99+
@EnableConfigurationProperties(ManagementServerProperties.class)
100+
static class TestConfiguration {
101+
102+
}
103+
61104
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ content into your application; rather pick only the properties that you need.
996996
management.context-path= # Management endpoint context-path. For instance `/actuator`
997997
management.port= # Management endpoint HTTP port. Use the same port as the application by default.
998998
management.security.enabled=true # Enable security.
999-
management.security.role=ADMIN # Roles required to access the management endpoint.
999+
management.security.roles=ADMIN # Comma-separated list of roles that can access the management endpoint.
10001000
management.security.sessions=stateless # Session creating policy to use (always, never, if_required, stateless).
10011001
10021002
# HEALTH INDICATORS (previously health.*)

spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,14 @@ TIP: Generated passwords are logged as the application starts. Search for '`Usin
520520
security password`'.
521521

522522
You can use Spring properties to change the username and password and to change the
523-
security roles required to access the endpoints. For example, you might set the following
523+
security role(s) required to access the endpoints. For example, you might set the following
524524
in your `application.properties`:
525525

526526
[source,properties,indent=0]
527527
----
528528
security.user.name=admin
529529
security.user.password=secret
530-
management.security.role=SUPERUSER
530+
management.security.roles=SUPERUSER
531531
----
532532

533533
TIP: If you don't use Spring Security and your HTTP endpoints are exposed publicly,

0 commit comments

Comments
 (0)