Skip to content

Commit 58f0e25

Browse files
committed
Merge branch '2.4.x'
Closes gh-24543
2 parents b51caf2 + a1ea5b4 commit 58f0e25

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ SecurityFilterChain managementSecurityFilterChain(HttpSecurity http) throws Exce
6363
requests.requestMatchers(EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class)).permitAll();
6464
requests.anyRequest().authenticated();
6565
});
66+
http.cors();
6667
http.formLogin(Customizer.withDefaults());
6768
http.httpBasic(Customizer.withDefaults());
6869
return http.build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.actuator;
18+
19+
import java.net.URI;
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
28+
import org.springframework.boot.test.web.client.TestRestTemplate;
29+
import org.springframework.boot.web.client.RestTemplateBuilder;
30+
import org.springframework.context.ApplicationContext;
31+
import org.springframework.http.HttpStatus;
32+
import org.springframework.http.RequestEntity;
33+
import org.springframework.http.ResponseEntity;
34+
import org.springframework.test.context.ActiveProfiles;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* Integration test for cors preflight requests to management endpoints.
40+
*
41+
* @author Madhura Bhave
42+
*/
43+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
44+
@ActiveProfiles("cors")
45+
class CorsSampleActuatorApplicationTests {
46+
47+
private TestRestTemplate testRestTemplate;
48+
49+
@Autowired
50+
private ApplicationContext applicationContext;
51+
52+
@BeforeEach
53+
void setUp() {
54+
RestTemplateBuilder builder = new RestTemplateBuilder();
55+
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(this.applicationContext.getEnvironment(),
56+
"http");
57+
builder = builder.uriTemplateHandler(handler);
58+
this.testRestTemplate = new TestRestTemplate(builder);
59+
}
60+
61+
@Test
62+
void endpointShouldReturnUnauthorized() {
63+
ResponseEntity<?> entity = this.testRestTemplate.getForEntity("/actuator/env", Map.class);
64+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
65+
}
66+
67+
@Test
68+
void preflightRequestToEndpointShouldReturnOk() throws Exception {
69+
RequestEntity<?> healthRequest = RequestEntity.options(new URI("/actuator/env"))
70+
.header("Origin", "http://localhost:8080").header("Access-Control-Request-Method", "GET").build();
71+
ResponseEntity<?> exchange = this.testRestTemplate.exchange(healthRequest, Map.class);
72+
assertThat(exchange.getStatusCode()).isEqualTo(HttpStatus.OK);
73+
}
74+
75+
@Test
76+
void preflightRequestWhenCorsConfigInvalidShouldReturnForbidden() throws Exception {
77+
RequestEntity<?> entity = RequestEntity.options(new URI("/actuator/env"))
78+
.header("Origin", "http://localhost:9095").header("Access-Control-Request-Method", "GET").build();
79+
ResponseEntity<byte[]> exchange = this.testRestTemplate.exchange(entity, byte[].class);
80+
assertThat(exchange.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
management.endpoints.web.cors.allowed-origins=http://localhost:8080
2+
management.endpoints.web.cors.allowed-methods=GET

0 commit comments

Comments
 (0)