mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Use method references when possible in test code
See gh-40974
This commit is contained in:
parent
dcccb3b2b1
commit
207327d97c
|
@ -45,6 +45,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity.CsrfSpec;
|
||||
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
@ -161,7 +162,7 @@ class GraphQlWebFluxSecurityAutoConfigurationTests {
|
|||
|
||||
@Bean
|
||||
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
|
||||
return http.csrf((spec) -> spec.disable())
|
||||
return http.csrf(CsrfSpec::disable)
|
||||
// Demonstrate that method security works
|
||||
// Best practice to use both for defense in depth
|
||||
.authorizeExchange((requests) -> requests.anyExchange().permitAll())
|
||||
|
|
|
@ -41,6 +41,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
|
@ -154,7 +155,7 @@ class GraphQlWebMvcSecurityAutoConfigurationTests {
|
|||
|
||||
@Bean
|
||||
DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Exception {
|
||||
return http.csrf((c) -> c.disable())
|
||||
return http.csrf(CsrfConfigurer::disable)
|
||||
// Demonstrate that method security works
|
||||
// Best practice to use both for defense in depth
|
||||
.authorizeHttpRequests((requests) -> requests.anyRequest().permitAll())
|
||||
|
|
|
@ -58,7 +58,7 @@ class ConditionEvaluationReportLoggingListenerTests {
|
|||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
this.initializer.initialize(context);
|
||||
context.register(Config.class);
|
||||
withDebugLogging(() -> context.refresh());
|
||||
withDebugLogging(context::refresh);
|
||||
assertThat(output).contains("CONDITIONS EVALUATION REPORT");
|
||||
}
|
||||
|
||||
|
|
|
@ -297,7 +297,7 @@ class FileSystemWatcherTests {
|
|||
private void setupWatcher(long pollingInterval, long quietPeriod, SnapshotStateRepository snapshotStateRepository) {
|
||||
this.watcher = new FileSystemWatcher(false, Duration.ofMillis(pollingInterval), Duration.ofMillis(quietPeriod),
|
||||
snapshotStateRepository);
|
||||
this.watcher.addListener((changeSet) -> FileSystemWatcherTests.this.changes.add(changeSet));
|
||||
this.watcher.addListener(FileSystemWatcherTests.this.changes::add);
|
||||
}
|
||||
|
||||
private File startWithNewDirectory() {
|
||||
|
|
|
@ -115,7 +115,7 @@ class DefaultRunningServiceTests {
|
|||
}
|
||||
|
||||
private DefaultRunningService createRunningService(boolean psResponseHasImage) {
|
||||
DockerHost host = DockerHost.get("192.168.1.1", () -> Collections.emptyList());
|
||||
DockerHost host = DockerHost.get("192.168.1.1", Collections::emptyList);
|
||||
String id = "123";
|
||||
String name = "my-service";
|
||||
String image = "redis";
|
||||
|
|
|
@ -52,7 +52,7 @@ class DockerHostTests {
|
|||
|
||||
private static final Function<String, String> NO_SYSTEM_ENV = (key) -> null;
|
||||
|
||||
private static final Supplier<List<DockerCliContextResponse>> NO_CONTEXT = () -> Collections.emptyList();
|
||||
private static final Supplier<List<DockerCliContextResponse>> NO_CONTEXT = Collections::emptyList;
|
||||
|
||||
@Test
|
||||
void getWhenHasHost() {
|
||||
|
|
|
@ -58,14 +58,14 @@ class TcpConnectServiceReadinessCheckTests {
|
|||
|
||||
@Test
|
||||
void checkWhenServerWritesData() throws Exception {
|
||||
withServer((socket) -> socket.getOutputStream().write('!'), (port) -> check(port));
|
||||
withServer((socket) -> socket.getOutputStream().write('!'), this::check);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkWhenNoSocketOutput() throws Exception {
|
||||
// Simulate waiting for traffic from client to server. The sleep duration must
|
||||
// be longer than the read timeout of the ready check!
|
||||
withServer((socket) -> sleep(Duration.ofSeconds(10)), (port) -> check(port));
|
||||
withServer((socket) -> sleep(Duration.ofSeconds(10)), this::check);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -40,7 +40,7 @@ class ExcludeFilterApplicationContextInitializerTests {
|
|||
void testConfigurationIsExcluded() {
|
||||
SpringApplication application = new SpringApplication(TestApplication.class);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
AssertableApplicationContext applicationContext = AssertableApplicationContext.get(() -> application.run());
|
||||
AssertableApplicationContext applicationContext = AssertableApplicationContext.get(application::run);
|
||||
assertThat(applicationContext).hasSingleBean(TestApplication.class);
|
||||
assertThat(applicationContext).doesNotHaveBean(ExcludedTestConfiguration.class);
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ class ContainerConnectionDetailsFactoryTests {
|
|||
void getContainerWhenNotInitializedThrowsException() {
|
||||
TestContainerConnectionDetailsFactory factory = new TestContainerConnectionDetailsFactory();
|
||||
TestContainerConnectionDetails connectionDetails = getConnectionDetails(factory, this.source);
|
||||
assertThatIllegalStateException().isThrownBy(() -> connectionDetails.callGetContainer())
|
||||
assertThatIllegalStateException().isThrownBy(connectionDetails::callGetContainer)
|
||||
.withMessage("Container cannot be obtained before the connection details bean has been initialized");
|
||||
}
|
||||
|
||||
|
|
|
@ -255,7 +255,7 @@ class ImageReferenceTests {
|
|||
void inTaggedFormWhenHasDigestThrowsException() {
|
||||
ImageReference reference = ImageReference
|
||||
.of("ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
|
||||
assertThatIllegalStateException().isThrownBy(() -> reference.inTaggedForm())
|
||||
assertThatIllegalStateException().isThrownBy(reference::inTaggedForm)
|
||||
.withMessage(
|
||||
"Image reference 'docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d' cannot contain a digest");
|
||||
}
|
||||
|
|
|
@ -719,7 +719,7 @@ class JarFileTests {
|
|||
Iterator<JarEntry> iterator = this.jarFile.iterator();
|
||||
iterator.next();
|
||||
this.jarFile.close();
|
||||
assertThatZipFileClosedIsThrownBy(() -> iterator.hasNext());
|
||||
assertThatZipFileClosedIsThrownBy(iterator::hasNext);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -240,7 +240,7 @@ class NestedJarFileTests {
|
|||
void getCommentWhenClosedThrowsException() throws IOException {
|
||||
try (NestedJarFile jar = new NestedJarFile(this.file)) {
|
||||
jar.close();
|
||||
assertThatIllegalStateException().isThrownBy(() -> jar.getComment()).withMessage("Zip file closed");
|
||||
assertThatIllegalStateException().isThrownBy(jar::getComment).withMessage("Zip file closed");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,7 +269,7 @@ class NestedJarFileTests {
|
|||
void sizeWhenClosedThrowsException() throws Exception {
|
||||
try (NestedJarFile jar = new NestedJarFile(this.file)) {
|
||||
jar.close();
|
||||
assertThatIllegalStateException().isThrownBy(() -> jar.size()).withMessage("Zip file closed");
|
||||
assertThatIllegalStateException().isThrownBy(jar::size).withMessage("Zip file closed");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -242,7 +242,7 @@ class JarUrlConnectionTests {
|
|||
@Test
|
||||
void getInputStreamWhenNotNestedAndHasNoEntryThrowsException() throws Exception {
|
||||
JarUrlConnection connection = JarUrlConnection.open(JarUrl.create(this.file));
|
||||
assertThatIOException().isThrownBy(() -> connection.getInputStream()).withMessage("no entry name specified");
|
||||
assertThatIOException().isThrownBy(connection::getInputStream).withMessage("no entry name specified");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -37,10 +37,14 @@ class ModifiedClassPathExtensionForkParameterizedTests {
|
|||
@ParameterizedTest
|
||||
@ValueSource(strings = { "one", "two", "three" })
|
||||
void testIsInvokedOnceForEachArgument(String argument) {
|
||||
switch (argument) {
|
||||
case "one" -> assertThat(arguments).isEmpty();
|
||||
case "two" -> assertThat(arguments).doesNotContain("two", "three");
|
||||
case "three" -> assertThat(arguments).doesNotContain("three");
|
||||
if (argument.equals("one")) {
|
||||
assertThat(arguments).isEmpty();
|
||||
}
|
||||
else if (argument.equals("two")) {
|
||||
assertThat(arguments).doesNotContain("two", "three");
|
||||
}
|
||||
else if (argument.equals("three")) {
|
||||
assertThat(arguments).doesNotContain("three");
|
||||
}
|
||||
arguments.add(argument);
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ class PropertySourcesDeducerTests {
|
|||
Environment environment = mock(Environment.class);
|
||||
given(applicationContext.getEnvironment()).willReturn(environment);
|
||||
PropertySourcesDeducer deducer = new PropertySourcesDeducer(applicationContext);
|
||||
assertThatIllegalStateException().isThrownBy(() -> deducer.getPropertySources())
|
||||
assertThatIllegalStateException().isThrownBy(deducer::getPropertySources)
|
||||
.withMessage("Unable to obtain PropertySources from PropertySourcesPlaceholderConfigurer or Environment");
|
||||
}
|
||||
|
||||
|
|
|
@ -160,7 +160,7 @@ class LoggerConfigurationTests {
|
|||
@Test
|
||||
void getLevelWhenCustomThrowsException() {
|
||||
LevelConfiguration configuration = LevelConfiguration.ofCustom("FINE");
|
||||
assertThatIllegalStateException().isThrownBy(() -> configuration.getLevel())
|
||||
assertThatIllegalStateException().isThrownBy(configuration::getLevel)
|
||||
.withMessage("Unable to provide LogLevel for 'FINE'");
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.logging.log4j2;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -46,8 +47,7 @@ class SpringBootPropertySourceTests {
|
|||
|
||||
@Test
|
||||
void allDefaultMethodsAreImplemented() {
|
||||
assertThat(Stream.of(SpringBootPropertySource.class.getMethods()).filter((method) -> method.isDefault()))
|
||||
.isEmpty();
|
||||
assertThat(Stream.of(SpringBootPropertySource.class.getMethods()).filter(Method::isDefault)).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ class LogbackLoggingSystemParallelInitializationTests {
|
|||
List<Thread> threads = new ArrayList<>();
|
||||
List<Throwable> exceptions = new CopyOnWriteArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Thread thread = new Thread(() -> this.loggingSystem.beforeInitialize());
|
||||
Thread thread = new Thread(this.loggingSystem::beforeInitialize);
|
||||
thread.setUncaughtExceptionHandler((t, ex) -> exceptions.add(ex));
|
||||
threads.add(thread);
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public abstract class AbstractScriptDatabaseInitializerTests<T extends AbstractS
|
|||
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
|
||||
settings.setDataLocations(Arrays.asList("data.sql"));
|
||||
T initializer = createEmbeddedDatabaseInitializer(settings);
|
||||
assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> initializer.initializeDatabase());
|
||||
assertThatExceptionOfType(DataAccessException.class).isThrownBy(initializer::initializeDatabase);
|
||||
assertThatDatabaseWasAccessed(initializer);
|
||||
}
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ class DefaultSslManagerBundleTests {
|
|||
SslStoreBundle storeBundle = SslStoreBundle.of(keyStore, null, null);
|
||||
DefaultSslManagerBundle bundle = new TestDefaultSslManagerBundle(storeBundle,
|
||||
SslBundleKey.of("secret", "alias"));
|
||||
assertThatIllegalStateException().isThrownBy(() -> bundle.getKeyManagerFactory())
|
||||
assertThatIllegalStateException().isThrownBy(bundle::getKeyManagerFactory)
|
||||
.withMessage("Keystore does not contain alias 'alias'");
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ class DefaultSslManagerBundleTests {
|
|||
SslStoreBundle storeBundle = SslStoreBundle.of(keyStore, null, null);
|
||||
DefaultSslManagerBundle bundle = new TestDefaultSslManagerBundle(storeBundle,
|
||||
SslBundleKey.of("secret", "alias"));
|
||||
assertThatIllegalStateException().isThrownBy(() -> bundle.getKeyManagerFactory())
|
||||
assertThatIllegalStateException().isThrownBy(bundle::getKeyManagerFactory)
|
||||
.withMessage("Could not determine if keystore contains alias 'alias'");
|
||||
}
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ class ReactiveWebServerApplicationContextTests {
|
|||
|
||||
@Test
|
||||
void whenThereIsNoWebServerFactoryBeanThenContextRefreshWillFail() {
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
|
||||
.havingRootCause()
|
||||
.withMessageContaining(
|
||||
"Unable to start ReactiveWebServerApplicationContext due to missing ReactiveWebServerFactory bean");
|
||||
|
@ -68,7 +68,7 @@ class ReactiveWebServerApplicationContextTests {
|
|||
@Test
|
||||
void whenThereIsNoHttpHandlerBeanThenContextRefreshWillFail() {
|
||||
addWebServerFactoryBean();
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
|
||||
.havingRootCause()
|
||||
.withMessageContaining("Unable to start ReactiveWebApplicationContext due to missing HttpHandler bean");
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ class ReactiveWebServerApplicationContextTests {
|
|||
void whenThereAreMultipleWebServerFactoryBeansThenContextRefreshWillFail() {
|
||||
addWebServerFactoryBean();
|
||||
addWebServerFactoryBean("anotherWebServerFactory");
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
|
||||
.havingRootCause()
|
||||
.withMessageContaining(
|
||||
"Unable to start ReactiveWebApplicationContext due to multiple ReactiveWebServerFactory beans");
|
||||
|
@ -88,7 +88,7 @@ class ReactiveWebServerApplicationContextTests {
|
|||
addWebServerFactoryBean();
|
||||
addHttpHandlerBean("httpHandler1");
|
||||
addHttpHandlerBean("httpHandler2");
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
|
||||
.havingRootCause()
|
||||
.withMessageContaining("Unable to start ReactiveWebApplicationContext due to multiple HttpHandler beans");
|
||||
}
|
||||
|
@ -164,7 +164,7 @@ class ReactiveWebServerApplicationContextTests {
|
|||
addWebServerFactoryBean();
|
||||
addHttpHandlerBean();
|
||||
this.context.refresh();
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.context.refresh())
|
||||
assertThatIllegalStateException().isThrownBy(this.context::refresh)
|
||||
.withMessageContaining("multiple refresh attempts");
|
||||
}
|
||||
|
||||
|
|
|
@ -205,7 +205,7 @@ abstract class AbstractFilterRegistrationBeanTests {
|
|||
|
||||
@Test
|
||||
void failsWithDoubleRegistration() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> doubleRegistration())
|
||||
assertThatIllegalStateException().isThrownBy(this::doubleRegistration)
|
||||
.withMessage("Failed to register 'filter double-registration' on the "
|
||||
+ "servlet context. Possibly already registered?");
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ class ServletRegistrationBeanTests {
|
|||
|
||||
@Test
|
||||
void failsWithDoubleRegistration() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> doubleRegistration())
|
||||
assertThatIllegalStateException().isThrownBy(this::doubleRegistration)
|
||||
.withMessage("Failed to register 'servlet double-registration' on "
|
||||
+ "the servlet context. Possibly already registered?");
|
||||
}
|
||||
|
|
|
@ -215,7 +215,7 @@ class ServletWebServerApplicationContextTests {
|
|||
void cannotSecondRefresh() {
|
||||
addWebServerFactoryBean();
|
||||
this.context.refresh();
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.context.refresh());
|
||||
assertThatIllegalStateException().isThrownBy(this.context::refresh);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -229,7 +229,7 @@ class ServletWebServerApplicationContextTests {
|
|||
|
||||
@Test
|
||||
void missingServletWebServerFactory() {
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
|
||||
.havingRootCause()
|
||||
.withMessageContaining("Unable to start ServletWebServerApplicationContext due to missing "
|
||||
+ "ServletWebServerFactory bean");
|
||||
|
@ -240,7 +240,7 @@ class ServletWebServerApplicationContextTests {
|
|||
addWebServerFactoryBean();
|
||||
this.context.registerBeanDefinition("webServerFactory2",
|
||||
new RootBeanDefinition(MockServletWebServerFactory.class));
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
|
||||
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
|
||||
.havingRootCause()
|
||||
.withMessageContaining("Unable to start ServletWebServerApplicationContext due to "
|
||||
+ "multiple ServletWebServerFactory beans");
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.springframework.context.annotation.Configuration;
|
|||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
|
@ -74,7 +75,7 @@ class ShutdownSampleActuatorApplicationTests {
|
|||
|
||||
@Bean
|
||||
SecurityFilterChain configure(HttpSecurity http) throws Exception {
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import org.springframework.context.annotation.Configuration;
|
|||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
|
@ -35,7 +36,7 @@ public class SecurityConfig {
|
|||
|
||||
@Bean
|
||||
public DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Exception {
|
||||
return http.csrf((csrf) -> csrf.disable())
|
||||
return http.csrf(CsrfConfigurer::disable)
|
||||
// Demonstrate that method security works
|
||||
// Best practice to use both for defense in depth
|
||||
.authorizeHttpRequests((requests) -> requests.anyRequest().permitAll())
|
||||
|
|
|
@ -21,6 +21,7 @@ import org.springframework.boot.actuate.health.HealthEndpoint;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
import static org.springframework.security.config.Customizer.withDefaults;
|
||||
|
@ -41,7 +42,7 @@ class SecurityConfiguration {
|
|||
});
|
||||
http.formLogin(withDefaults());
|
||||
http.httpBasic(withDefaults());
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import org.springframework.boot.actuate.health.HealthEndpoint;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
import static org.springframework.security.config.Customizer.withDefaults;
|
||||
|
@ -41,7 +42,7 @@ class SecurityConfiguration {
|
|||
});
|
||||
http.formLogin(withDefaults());
|
||||
http.httpBasic(withDefaults());
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import org.springframework.boot.actuate.health.HealthEndpoint;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
import static org.springframework.security.config.Customizer.withDefaults;
|
||||
|
@ -41,7 +42,7 @@ class SecurityConfiguration {
|
|||
});
|
||||
http.formLogin(withDefaults());
|
||||
http.httpBasic(withDefaults());
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import org.springframework.boot.actuate.health.HealthEndpoint;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
import static org.springframework.security.config.Customizer.withDefaults;
|
||||
|
@ -41,7 +42,7 @@ class SecurityConfiguration {
|
|||
});
|
||||
http.formLogin(withDefaults());
|
||||
http.httpBasic(withDefaults());
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ import org.springframework.core.annotation.Order;
|
|||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
@ -75,7 +76,7 @@ public class SampleMethodSecurityApplication implements WebMvcConfigurer {
|
|||
|
||||
@Bean
|
||||
SecurityFilterChain configure(HttpSecurity http) throws Exception {
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
http.authorizeHttpRequests((requests) -> {
|
||||
requests.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll();
|
||||
requests.anyRequest().fullyAuthenticated();
|
||||
|
@ -94,7 +95,7 @@ public class SampleMethodSecurityApplication implements WebMvcConfigurer {
|
|||
|
||||
@Bean
|
||||
SecurityFilterChain actuatorSecurity(HttpSecurity http) throws Exception {
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
http.securityMatcher(EndpointRequest.toAnyEndpoint());
|
||||
http.authorizeHttpRequests((requests) -> requests.anyRequest().authenticated());
|
||||
http.httpBasic(withDefaults());
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
@ -45,7 +46,7 @@ public class SampleWebSecureCustomApplication implements WebMvcConfigurer {
|
|||
|
||||
@Bean
|
||||
SecurityFilterChain configure(HttpSecurity http) throws Exception {
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
http.authorizeHttpRequests((requests) -> {
|
||||
requests.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll();
|
||||
requests.anyRequest().fullyAuthenticated();
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.provisioning.JdbcUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
|
@ -48,7 +49,7 @@ public class SampleWebSecureJdbcApplication implements WebMvcConfigurer {
|
|||
|
||||
@Bean
|
||||
SecurityFilterChain configure(HttpSecurity http) throws Exception {
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
http.authorizeHttpRequests((requests) -> {
|
||||
requests.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll();
|
||||
requests.anyRequest().fullyAuthenticated();
|
||||
|
|
|
@ -34,6 +34,7 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
@ -96,7 +97,7 @@ class SampleWebSecureApplicationTests {
|
|||
|
||||
@Bean
|
||||
SecurityFilterChain configure(HttpSecurity http) throws Exception {
|
||||
http.csrf((csrf) -> csrf.disable());
|
||||
http.csrf(CsrfConfigurer::disable);
|
||||
http.authorizeHttpRequests((requests) -> {
|
||||
requests.requestMatchers("/public/**").permitAll();
|
||||
requests.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll();
|
||||
|
|
Loading…
Reference in New Issue
Block a user