Skip to content

Commit 4882544

Browse files
committed
Polish contribution
Closes gh-6540
1 parent dced154 commit 4882544

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.security.Principal;
2020
import java.util.Arrays;
21-
import java.util.Collections;
2221
import java.util.HashMap;
2322
import java.util.List;
2423
import java.util.Map;
@@ -190,10 +189,7 @@ private boolean isSecure(Principal principal) {
190189
if (isSpringSecurityAuthentication(principal)) {
191190
Authentication authentication = (Authentication) principal;
192191
List<String> roles = Arrays.asList(StringUtils.trimArrayElements(StringUtils
193-
.commaDelimitedListToStringArray(this.roleResolver.getProperty("roles"))));
194-
if (roles.isEmpty()) {
195-
roles = Collections.singletonList("ROLE_ADMIN");
196-
}
192+
.commaDelimitedListToStringArray(this.roleResolver.getProperty("roles", "ROLE_ADMIN"))));
197193
for (GrantedAuthority authority : authentication.getAuthorities()) {
198194
String name = authority.getAuthority();
199195
for (String role : roles) {

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ public class HealthMvcEndpointTests {
6060

6161
private MockEnvironment environment;
6262

63-
private UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken(
64-
"user", "password",
65-
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
63+
private UsernamePasswordAuthenticationToken user = createAuthenticationToken("ROLE_USER");
6664

67-
private UsernamePasswordAuthenticationToken admin = new UsernamePasswordAuthenticationToken(
68-
"user", "password",
69-
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"));
65+
private UsernamePasswordAuthenticationToken admin = createAuthenticationToken("ROLE_ADMIN");
66+
67+
private UsernamePasswordAuthenticationToken hero = createAuthenticationToken("ROLE_HERO");
68+
69+
private UsernamePasswordAuthenticationToken createAuthenticationToken(String authority) {
70+
return new UsernamePasswordAuthenticationToken(
71+
"user", "password",
72+
AuthorityUtils.commaSeparatedStringToAuthorityList(authority));
73+
}
7074

7175
@Before
7276
public void init() {
@@ -147,17 +151,26 @@ public void secureNonAdmin() {
147151

148152
@Test
149153
public void secureCustomRole() {
150-
this.mvc = new HealthMvcEndpoint(this.endpoint, false);
151-
this.mvc.setEnvironment(this.environment);
152154
this.environment.getPropertySources().addLast(SECURITY_ROLES);
153155
given(this.endpoint.invoke())
154156
.willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
155-
Object result = this.mvc.invoke(this.user);
157+
Object result = this.mvc.invoke(this.hero);
156158
assertThat(result instanceof Health).isTrue();
157159
assertThat(((Health) result).getStatus() == Status.UP).isTrue();
158160
assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar");
159161
}
160162

163+
@Test
164+
public void secureCustomRoleNoAccess() {
165+
this.environment.getPropertySources().addLast(SECURITY_ROLES);
166+
given(this.endpoint.invoke())
167+
.willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
168+
Object result = this.mvc.invoke(this.admin);
169+
assertThat(result instanceof Health).isTrue();
170+
assertThat(((Health) result).getStatus() == Status.UP).isTrue();
171+
assertThat(((Health) result).getDetails().get("foo")).isNull();
172+
}
173+
161174
@Test
162175
public void healthIsCached() {
163176
given(this.endpoint.getTimeToLive()).willReturn(10000L);

0 commit comments

Comments
 (0)