Skip to content

Commit d7fa384

Browse files
committed
Merge pull request #31143 from hpoettker
* pr/31143: Use Lambda-based API in Spring Security examples Closes gh-31143
2 parents d3b4cba + 9fdd471 commit d7fa384

File tree

9 files changed

+37
-27
lines changed

9 files changed

+37
-27
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/sql.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ In simple setups, a `SecurityFilterChain` like the following can be used:
351351

352352
[source,java,indent=0,subs="verbatim"]
353353
----
354-
include::{docs-java}/data/sql/h2webconsole/springsecurity/DevProfileSecurityConfiguration.java[]
354+
include::{docs-java}/data/sql/h2webconsole/springsecurity/DevProfileSecurityConfiguration.java[tag=!customizer]
355355
----
356356

357357
WARNING: The H2 console is only intended for use during development.

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuator/endpoints/security/exposeall/MySecurityConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -27,8 +27,8 @@ public class MySecurityConfiguration {
2727

2828
@Bean
2929
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
30-
http.requestMatcher(EndpointRequest.toAnyEndpoint())
31-
.authorizeRequests((requests) -> requests.anyRequest().permitAll());
30+
http.requestMatcher(EndpointRequest.toAnyEndpoint());
31+
http.authorizeRequests((requests) -> requests.anyRequest().permitAll());
3232
return http.build();
3333
}
3434

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/actuator/endpoints/security/typical/MySecurityConfiguration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -22,14 +22,16 @@
2222
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2323
import org.springframework.security.web.SecurityFilterChain;
2424

25+
import static org.springframework.security.config.Customizer.withDefaults;
26+
2527
@Configuration(proxyBeanMethods = false)
2628
public class MySecurityConfiguration {
2729

2830
@Bean
2931
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
30-
http.requestMatcher(EndpointRequest.toAnyEndpoint())
31-
.authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
32-
http.httpBasic();
32+
http.requestMatcher(EndpointRequest.toAnyEndpoint());
33+
http.authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
34+
http.httpBasic(withDefaults());
3335
return http.build();
3436
}
3537

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/data/sql/h2webconsole/springsecurity/DevProfileSecurityConfiguration.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.context.annotation.Profile;
2323
import org.springframework.core.Ordered;
2424
import org.springframework.core.annotation.Order;
25+
import org.springframework.security.config.Customizer;
2526
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2627
import org.springframework.security.web.SecurityFilterChain;
2728

@@ -32,13 +33,18 @@ public class DevProfileSecurityConfiguration {
3233
@Bean
3334
@Order(Ordered.HIGHEST_PRECEDENCE)
3435
SecurityFilterChain h2ConsoleSecurityFilterChain(HttpSecurity http) throws Exception {
35-
// @formatter:off
36-
return http.requestMatcher(PathRequest.toH2Console())
37-
// ... configuration for authorization
38-
.csrf().disable()
39-
.headers().frameOptions().sameOrigin().and()
40-
.build();
41-
// @formatter:on
36+
http.requestMatcher(PathRequest.toH2Console());
37+
http.authorizeRequests(yourCustomAuthorization());
38+
http.csrf((csrf) -> csrf.disable());
39+
http.headers((headers) -> headers.frameOptions().sameOrigin());
40+
return http.build();
4241
}
4342

43+
// tag::customizer[]
44+
<T> Customizer<T> yourCustomAuthorization() {
45+
return (t) -> {
46+
};
47+
}
48+
// end::customizer[]
49+
4450
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/security/oauth2/client/MyOAuthClientConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -26,8 +26,8 @@ public class MyOAuthClientConfiguration {
2626

2727
@Bean
2828
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
29-
http.authorizeRequests().anyRequest().authenticated();
30-
http.oauth2Login().redirectionEndpoint().baseUri("custom-callback");
29+
http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
30+
http.oauth2Login((login) -> login.redirectionEndpoint().baseUri("custom-callback"));
3131
return http.build();
3232
}
3333

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/security/springwebflux/MyWebFluxSecurityConfiguration.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -22,16 +22,18 @@
2222
import org.springframework.security.config.web.server.ServerHttpSecurity;
2323
import org.springframework.security.web.server.SecurityWebFilterChain;
2424

25+
import static org.springframework.security.config.Customizer.withDefaults;
26+
2527
@Configuration(proxyBeanMethods = false)
2628
public class MyWebFluxSecurityConfiguration {
2729

2830
@Bean
2931
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
30-
http.authorizeExchange((spec) -> {
31-
spec.matchers(PathRequest.toStaticResources().atCommonLocations()).permitAll();
32-
spec.pathMatchers("/foo", "/bar").authenticated();
32+
http.authorizeExchange((exchange) -> {
33+
exchange.matchers(PathRequest.toStaticResources().atCommonLocations()).permitAll();
34+
exchange.pathMatchers("/foo", "/bar").authenticated();
3335
});
34-
http.formLogin();
36+
http.formLogin(withDefaults());
3537
return http.build();
3638
}
3739

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/howto/security/enablehttps/MySecurityConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -27,7 +27,7 @@ public class MySecurityConfig {
2727
@Bean
2828
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
2929
// Customize the application security ...
30-
http.requiresChannel().anyRequest().requiresSecure();
30+
http.requiresChannel((channel) -> channel.anyRequest().requiresSecure());
3131
return http.build();
3232
}
3333

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/howto/testing/slicetests/MyConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class MyConfiguration {
3030

3131
@Bean
3232
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
33-
http.authorizeRequests().anyRequest().authenticated();
33+
http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
3434
return http.build();
3535
}
3636

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/howto/testing/slicetests/MySecurityConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class MySecurityConfiguration {
2626

2727
@Bean
2828
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
29-
http.authorizeRequests().anyRequest().authenticated();
29+
http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
3030
return http.build();
3131
}
3232

0 commit comments

Comments
 (0)