Skip to content

Commit 0afcd97

Browse files
committed
Adapt to changes in Spring Boot 4.0 and Spring Security 7.
See #693
1 parent bd792ea commit 0afcd97

File tree

6 files changed

+62
-59
lines changed

6 files changed

+62
-59
lines changed
Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -27,7 +27,6 @@
2727

2828
import org.springframework.beans.factory.annotation.Autowired;
2929
import org.springframework.boot.test.context.SpringBootTest;
30-
import org.springframework.restdocs.JUnitRestDocumentation;
3130
import org.springframework.restdocs.RestDocumentationContextProvider;
3231
import org.springframework.restdocs.RestDocumentationExtension;
3332
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
@@ -42,51 +41,47 @@
4241
*/
4342
@SpringBootTest
4443
@ExtendWith(RestDocumentationExtension.class)
45-
public class WebIntegrationTests {
44+
class WebIntegrationTests {
4645

47-
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
46+
@Autowired WebApplicationContext context;
47+
@Autowired CustomerRepository customers;
4848

49-
@Autowired
50-
WebApplicationContext context;
51-
@Autowired
52-
CustomerRepository customers;
49+
private MockMvc mvc;
5350

54-
private MockMvc mvc;
51+
@BeforeEach
52+
void setUp(RestDocumentationContextProvider restDocumentation) {
5553

56-
@BeforeEach
57-
public void setUp(RestDocumentationContextProvider restDocumentation) {
54+
this.mvc = MockMvcBuilders.webAppContextSetup(context).//
55+
apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation)).//
56+
build();
57+
}
5858

59-
this.mvc = MockMvcBuilders.webAppContextSetup(context).//
60-
apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation)).//
61-
build();
62-
}
59+
@Test
60+
void executeConditionalGetRequests() throws Exception {
6361

64-
@Test
65-
public void executeConditionalGetRequests() throws Exception {
62+
var customer = customers.findAll().iterator().next();
63+
var uri = new UriTemplate("/customers/{id}").expand(customer.getId());
6664

67-
var customer = customers.findAll().iterator().next();
68-
var uri = new UriTemplate("/customers/{id}").expand(customer.getId());
65+
var response = mvc.perform(get(uri)).//
66+
andExpect(header().string(ETAG, is(notNullValue()))).//
67+
andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
68+
andReturn().getResponse();
6969

70-
var response = mvc.perform(get(uri)).//
71-
andExpect(header().string(ETAG, is(notNullValue()))).//
72-
andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
73-
andReturn().getResponse();
70+
// ETag-based
7471

75-
// ETag-based
72+
response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).//
73+
andExpect(status().isNotModified()).//
74+
andExpect(header().string(ETAG, is(notNullValue()))).//
75+
andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
76+
andDo(document("if-none-match")).//
77+
andReturn().getResponse();
7678

77-
response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).//
78-
andExpect(status().isNotModified()).//
79-
andExpect(header().string(ETAG, is(notNullValue()))).//
80-
andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
81-
andDo(document("if-none-match")).//
82-
andReturn().getResponse();
79+
// Last-modified-based
8380

84-
// Last-modified-based
85-
86-
mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).//
87-
andExpect(status().isNotModified()).//
88-
andExpect(header().string(ETAG, is(notNullValue()))).//
89-
andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
90-
andDo(document("if-modified-since"));
91-
}
81+
mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).//
82+
andExpect(status().isNotModified()).//
83+
andExpect(header().string(ETAG, is(notNullValue()))).//
84+
andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
85+
andDo(document("if-modified-since"));
86+
}
9287
}

rest/security/src/main/java/example/springdata/rest/security/Application.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,9 +23,11 @@
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
2525
import org.springframework.http.HttpMethod;
26-
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
26+
import org.springframework.security.config.Customizer;
27+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
2728
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2829
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
30+
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
2931
import org.springframework.security.core.context.SecurityContextHolder;
3032
import org.springframework.security.core.userdetails.User;
3133
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
@@ -56,7 +58,7 @@ public static void main(String[] args) {
5658
employeeRepository.save(new Employee("Frodo", "Baggins", "ring bearer"));
5759
employeeRepository.save(new Employee("Gandalf", "the Wizard", "servant of the Secret Fire"));
5860

59-
/**
61+
/*
6062
* Due to method-level protections on {@link example.company.ItemRepository}, the security context must be loaded
6163
* with an authentication token containing the necessary privileges.
6264
*/
@@ -70,14 +72,13 @@ public static void main(String[] args) {
7072

7173
/**
7274
* This application is secured at both the URL level for some parts, and the method level for other parts. The URL
73-
* security is shown inside this code, while method-level annotations are enabled at by
74-
* {@link EnableGlobalMethodSecurity}.
75+
* security is shown inside this code, while method-level annotations are enabled at by {@link EnableMethodSecurity}.
7576
*
7677
* @author Greg Turnquist
7778
* @author Oliver Gierke
7879
*/
7980
@Configuration
80-
@EnableGlobalMethodSecurity(prePostEnabled = true)
81+
@EnableMethodSecurity(prePostEnabled = true)
8182
@EnableWebSecurity
8283
static class SecurityConfiguration {
8384

@@ -111,11 +112,14 @@ InMemoryUserDetailsManager userDetailsManager() {
111112
@Bean
112113
protected SecurityFilterChain configure(HttpSecurity http) throws Exception {
113114

114-
return http.httpBasic().and().authorizeRequests().//
115-
requestMatchers(HttpMethod.POST, "/employees").hasRole("ADMIN").//
116-
requestMatchers(HttpMethod.PUT, "/employees/**").hasRole("ADMIN").//
117-
requestMatchers(HttpMethod.PATCH, "/employees/**").hasRole("ADMIN").and().//
118-
csrf().disable().build();
115+
return http.authorizeHttpRequests((authorize) -> {
116+
117+
authorize //
118+
.requestMatchers(HttpMethod.POST, "/employees").hasRole("ADMIN") //
119+
.requestMatchers(HttpMethod.PUT, "/employees/**").hasRole("ADMIN") //
120+
.requestMatchers(HttpMethod.PATCH, "/employees/**").hasRole("ADMIN") //
121+
.anyRequest().permitAll();
122+
}).httpBasic(Customizer.withDefaults()).csrf(AbstractHttpConfigurer::disable).build();
119123
}
120124
}
121125
}

rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -20,6 +20,8 @@
2020
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2121
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
2222

23+
import tools.jackson.databind.ObjectMapper;
24+
2325
import java.util.Base64;
2426

2527
import org.junit.jupiter.api.BeforeEach;
@@ -35,7 +37,6 @@
3537
import org.springframework.test.web.servlet.MockMvc;
3638
import org.springframework.web.context.WebApplicationContext;
3739

38-
import com.fasterxml.jackson.databind.ObjectMapper;
3940

4041
/**
4142
* Test cases that verify the URL level of security by using the Spring MVC test framework.
@@ -91,8 +92,8 @@ void allowsGetRequestsButRejectsPostForUser() throws Exception {
9192

9293
mvc.perform(get("/employees").//
9394
headers(headers)).//
94-
andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)).//
95-
andExpect(status().isOk());
95+
andExpect(status().isOk()). //
96+
andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON));
9697

9798
mvc.perform(post("/employees").//
9899
headers(headers)).//
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Spring Data REST
22
spring.data.rest.base-path=/api
3+
spring.mongodb.representation.uuid=standard

rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2014-2024 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -44,7 +44,7 @@ class StoreRepositoryIntegrationTests {
4444

4545
@BeforeEach
4646
@AfterEach
47-
public void clearDb() {
47+
void clearDb() {
4848
repository.deleteAll();
4949
}
5050

web/projection/src/test/java/example/users/UserControllerClientTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.restclient.RestTemplateBuilder;
2626
import org.springframework.boot.resttestclient.TestRestTemplate;
27+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
2728
import org.springframework.boot.test.context.SpringBootTest;
2829
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2930
import org.springframework.context.annotation.Bean;
@@ -46,6 +47,7 @@
4647
* @author Jens Schauder
4748
*/
4849
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
50+
@AutoConfigureTestRestTemplate
4951
class UserControllerClientTests {
5052

5153
@Autowired TestRestTemplate template;

0 commit comments

Comments
 (0)