Skip to content

Commit c8f2793

Browse files
committed
Use lambda spring security config style
1 parent 6e64eb7 commit c8f2793

File tree

8 files changed

+153
-161
lines changed

8 files changed

+153
-161
lines changed

spring-boot-admin-samples/spring-boot-admin-sample-consul/src/main/java/de/codecentric/boot/admin/SpringBootAdminConsulApplication.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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,6 +22,7 @@
2222
import org.springframework.context.annotation.Configuration;
2323
import org.springframework.context.annotation.Profile;
2424
import org.springframework.http.HttpMethod;
25+
import org.springframework.security.config.Customizer;
2526
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2627
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2728
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@@ -53,12 +54,14 @@ public SecurityPermitAllConfig(AdminServerProperties adminServerProperties) {
5354

5455
@Override
5556
protected void configure(HttpSecurity http) throws Exception {
56-
http.authorizeRequests().anyRequest().permitAll().and().csrf()
57-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(
58-
new AntPathRequestMatcher(this.adminContextPath + "/instances", HttpMethod.POST.toString()),
59-
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
60-
HttpMethod.DELETE.toString()),
61-
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**"));
57+
http.authorizeRequests((authorizeRequests) -> authorizeRequests.anyRequest().permitAll())
58+
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
59+
.ignoringRequestMatchers(
60+
new AntPathRequestMatcher(this.adminContextPath + "/instances",
61+
HttpMethod.POST.toString()),
62+
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
63+
HttpMethod.DELETE.toString()),
64+
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")));
6265
}
6366

6467
}
@@ -75,27 +78,24 @@ public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
7578

7679
@Override
7780
protected void configure(HttpSecurity http) throws Exception {
78-
// @formatter:off
7981
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
8082
successHandler.setTargetUrlParameter("redirectTo");
8183
successHandler.setDefaultTargetUrl(this.adminContextPath + "/");
8284

83-
http.authorizeRequests()
84-
.antMatchers(this.adminContextPath + "/assets/**").permitAll()
85-
.antMatchers(this.adminContextPath + "/login").permitAll()
86-
.anyRequest().authenticated()
87-
.and()
88-
.formLogin().loginPage(this.adminContextPath + "/login").successHandler(successHandler).and()
89-
.logout().logoutUrl(this.adminContextPath + "/logout").and()
90-
.httpBasic().and()
91-
.csrf()
92-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
93-
.ignoringRequestMatchers(
94-
new AntPathRequestMatcher(this.adminContextPath + "/instances", HttpMethod.POST.toString()),
95-
new AntPathRequestMatcher(this.adminContextPath + "/instances/*", HttpMethod.DELETE.toString()),
96-
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")
97-
);
98-
// @formatter:on
85+
http.authorizeRequests((authorizeRequests) -> authorizeRequests
86+
.antMatchers(this.adminContextPath + "/assets/**").permitAll()
87+
.antMatchers(this.adminContextPath + "/login").permitAll().anyRequest().authenticated())
88+
.formLogin((formLogin) -> formLogin.loginPage(this.adminContextPath + "/login")
89+
.successHandler(successHandler))
90+
.logout((logout) -> logout.logoutUrl(this.adminContextPath + "/logout"))
91+
.httpBasic(Customizer.withDefaults())
92+
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
93+
.ignoringRequestMatchers(
94+
new AntPathRequestMatcher(this.adminContextPath + "/instances",
95+
HttpMethod.POST.toString()),
96+
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
97+
HttpMethod.DELETE.toString()),
98+
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")));
9999
}
100100

101101
}

spring-boot-admin-samples/spring-boot-admin-sample-eureka/src/main/java/de/codecentric/boot/admin/SpringBootAdminEurekaApplication.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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,6 +22,7 @@
2222
import org.springframework.context.annotation.Configuration;
2323
import org.springframework.context.annotation.Profile;
2424
import org.springframework.http.HttpMethod;
25+
import org.springframework.security.config.Customizer;
2526
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2627
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2728
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@@ -53,12 +54,15 @@ public SecurityPermitAllConfig(AdminServerProperties adminServerProperties) {
5354

5455
@Override
5556
protected void configure(HttpSecurity http) throws Exception {
56-
http.authorizeRequests().anyRequest().permitAll().and().csrf()
57-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(
58-
new AntPathRequestMatcher(this.adminContextPath + "/instances", HttpMethod.POST.toString()),
59-
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
60-
HttpMethod.DELETE.toString()),
61-
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**"));
57+
http.authorizeRequests((authorizeRequests) -> authorizeRequests.anyRequest().permitAll())
58+
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
59+
.ignoringRequestMatchers(
60+
new AntPathRequestMatcher(this.adminContextPath + "/instances",
61+
HttpMethod.POST.toString()),
62+
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
63+
HttpMethod.DELETE.toString()),
64+
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")));
65+
6266
}
6367

6468
}
@@ -75,27 +79,24 @@ public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
7579

7680
@Override
7781
protected void configure(HttpSecurity http) throws Exception {
78-
// @formatter:off
7982
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
8083
successHandler.setTargetUrlParameter("redirectTo");
8184
successHandler.setDefaultTargetUrl(this.adminContextPath + "/");
8285

83-
http.authorizeRequests()
84-
.antMatchers(this.adminContextPath + "/assets/**").permitAll()
85-
.antMatchers(this.adminContextPath + "/login").permitAll()
86-
.anyRequest().authenticated()
87-
.and()
88-
.formLogin().loginPage(this.adminContextPath + "/login").successHandler(successHandler).and()
89-
.logout().logoutUrl(this.adminContextPath + "/logout").and()
90-
.httpBasic().and()
91-
.csrf()
92-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
93-
.ignoringRequestMatchers(
94-
new AntPathRequestMatcher(this.adminContextPath + "/instances", HttpMethod.POST.toString()),
95-
new AntPathRequestMatcher(this.adminContextPath + "/instances/*", HttpMethod.DELETE.toString()),
96-
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")
97-
);
98-
// @formatter:on
86+
http.authorizeRequests((authorizeRequests) -> authorizeRequests
87+
.antMatchers(this.adminContextPath + "/assets/**").permitAll()
88+
.antMatchers(this.adminContextPath + "/login").permitAll().anyRequest().authenticated())
89+
.formLogin((formLogin) -> formLogin.loginPage(this.adminContextPath + "/login")
90+
.successHandler(successHandler))
91+
.logout((logout) -> logout.logoutUrl(this.adminContextPath + "/logout"))
92+
.httpBasic(Customizer.withDefaults())
93+
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
94+
.ignoringRequestMatchers(
95+
new AntPathRequestMatcher(this.adminContextPath + "/instances",
96+
HttpMethod.POST.toString()),
97+
new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
98+
HttpMethod.DELETE.toString()),
99+
new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")));
99100
}
100101

101102
}

spring-boot-admin-samples/spring-boot-admin-sample-hazelcast/src/main/java/de/codecentric/boot/admin/SpringBootAdminHazelcastApplication.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -31,6 +31,7 @@
3131
import org.springframework.context.annotation.Configuration;
3232
import org.springframework.context.annotation.Profile;
3333
import org.springframework.http.HttpMethod;
34+
import org.springframework.security.config.Customizer;
3435
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3536
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
3637
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@@ -106,13 +107,14 @@ public SecurityPermitAllConfig(AdminServerProperties adminServer) {
106107

107108
@Override
108109
protected void configure(HttpSecurity http) throws Exception {
109-
http.authorizeRequests().anyRequest().permitAll().and().csrf()
110-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
111-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(
112-
new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()),
113-
new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
114-
HttpMethod.DELETE.toString()),
115-
new AntPathRequestMatcher(this.adminServer.path("/actuator/**")));
110+
http.authorizeRequests((authorizeRequests) -> authorizeRequests.anyRequest().permitAll())
111+
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
112+
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(
113+
new AntPathRequestMatcher(this.adminServer.path("/instances"),
114+
HttpMethod.POST.toString()),
115+
new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
116+
HttpMethod.DELETE.toString()),
117+
new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))));
116118
}
117119

118120
}
@@ -129,28 +131,24 @@ public SecuritySecureConfig(AdminServerProperties adminServer) {
129131

130132
@Override
131133
protected void configure(HttpSecurity http) throws Exception {
132-
// @formatter:off
133134
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
134135
successHandler.setTargetUrlParameter("redirectTo");
135136
successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
136137

137-
http.authorizeRequests()
138-
.antMatchers(this.adminServer.path("/assets/**")).permitAll()
139-
.antMatchers(this.adminServer.path("/login")).permitAll()
140-
.anyRequest().authenticated()
141-
.and()
142-
.formLogin().loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
143-
.logout().logoutUrl(this.adminServer.path("/logout")).and()
144-
.httpBasic().and()
145-
.csrf()
146-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
147-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
148-
.ignoringRequestMatchers(
149-
new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()),
150-
new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()),
151-
new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))
152-
);
153-
// @formatter:on
138+
http.authorizeRequests((authorizeRequests) -> authorizeRequests
139+
.antMatchers(this.adminServer.path("/assets/**")).permitAll()
140+
.antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated())
141+
.formLogin((formLogin) -> formLogin.loginPage(this.adminServer.path("/login"))
142+
.successHandler(successHandler))
143+
.logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout")))
144+
.httpBasic(Customizer.withDefaults())
145+
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
146+
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(
147+
new AntPathRequestMatcher(this.adminServer.path("/instances"),
148+
HttpMethod.POST.toString()),
149+
new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
150+
HttpMethod.DELETE.toString()),
151+
new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))));
154152
}
155153

156154
}

spring-boot-admin-samples/spring-boot-admin-sample-reactive/src/main/java/de/codecentric/boot/admin/SpringBootAdminReactiveApplication.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -21,6 +21,7 @@
2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.context.annotation.Configuration;
2323
import org.springframework.context.annotation.Profile;
24+
import org.springframework.security.config.Customizer;
2425
import org.springframework.security.config.web.server.ServerHttpSecurity;
2526
import org.springframework.security.web.server.SecurityWebFilterChain;
2627

@@ -47,26 +48,20 @@ public static void main(String[] args) {
4748
@Bean
4849
@Profile("insecure")
4950
public SecurityWebFilterChain securityWebFilterChainPermitAll(ServerHttpSecurity http) {
50-
return http.authorizeExchange().anyExchange().permitAll()//
51-
.and().csrf().disable()//
52-
.build();
51+
return http.authorizeExchange((authorizeExchange) -> authorizeExchange.anyExchange().permitAll())
52+
.csrf(ServerHttpSecurity.CsrfSpec::disable).build();
5353
}
5454

5555
@Bean
5656
@Profile("secure")
5757
public SecurityWebFilterChain securityWebFilterChainSecure(ServerHttpSecurity http) {
58-
// @formatter:off
59-
return http.authorizeExchange()
60-
.pathMatchers(this.adminServer.path("/assets/**")).permitAll()
61-
.pathMatchers(this.adminServer.path("/login")).permitAll()
62-
.anyExchange().authenticated()
63-
.and()
64-
.formLogin().loginPage(this.adminServer.path("/login")).and()
65-
.logout().logoutUrl(this.adminServer.path("/logout")).and()
66-
.httpBasic().and()
67-
.csrf().disable()
68-
.build();
69-
// @formatter:on
58+
return http
59+
.authorizeExchange((authorizeExchange) -> authorizeExchange
60+
.pathMatchers(this.adminServer.path("/assets/**")).permitAll()
61+
.pathMatchers(this.adminServer.path("/login")).permitAll().anyExchange().authenticated())
62+
.formLogin((formLogin) -> formLogin.loginPage(this.adminServer.path("/login")))
63+
.logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout")))
64+
.httpBasic(Customizer.withDefaults()).csrf(ServerHttpSecurity.CsrfSpec::disable).build();
7065
}
7166

7267
@Bean

spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/SecurityPermitAllConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -38,11 +38,11 @@ public SecurityPermitAllConfig(AdminServerProperties adminServer) {
3838

3939
@Override
4040
protected void configure(HttpSecurity http) throws Exception {
41-
http.authorizeRequests().anyRequest().permitAll().and().csrf()
41+
http.authorizeRequests((authorizeRequest) -> authorizeRequest.anyRequest().permitAll()).csrf((csrf) -> csrf
4242
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(
4343
new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()),
4444
new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()),
45-
new AntPathRequestMatcher(this.adminServer.path("/actuator/**")));
45+
new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))));
4646
}
4747

4848
}

spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/SecuritySecureConfig.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -21,6 +21,7 @@
2121
import org.springframework.context.annotation.Configuration;
2222
import org.springframework.context.annotation.Profile;
2323
import org.springframework.http.HttpMethod;
24+
import org.springframework.security.config.Customizer;
2425
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2526
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2627
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -43,29 +44,25 @@ public SecuritySecureConfig(AdminServerProperties adminServer) {
4344

4445
@Override
4546
protected void configure(HttpSecurity http) throws Exception {
46-
// @formatter:off
4747
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
4848
successHandler.setTargetUrlParameter("redirectTo");
4949
successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
5050

51-
http.authorizeRequests()
52-
.antMatchers(this.adminServer.path("/assets/**")).permitAll() // <1>
53-
.antMatchers(this.adminServer.path("/login")).permitAll()
54-
.anyRequest().authenticated() // <2>
55-
.and()
56-
.formLogin().loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() // <3>
57-
.logout().logoutUrl(this.adminServer.path("/logout")).and()
58-
.httpBasic().and() // <4>
59-
.csrf()
60-
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // <5>
61-
.ignoringRequestMatchers(
62-
new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()), // <6>
63-
new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()), // <6>
64-
new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) // <7>
65-
)
66-
.and()
67-
.rememberMe().key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600);
68-
// @formatter:on
51+
http.authorizeRequests(
52+
(authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() // <1>
53+
.antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() // <2>
54+
).formLogin(
55+
(formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() // <3>
56+
).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) // <4>
57+
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // <5>
58+
.ignoringRequestMatchers(
59+
new AntPathRequestMatcher(this.adminServer.path("/instances"),
60+
HttpMethod.POST.toString()), // <6>
61+
new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
62+
HttpMethod.DELETE.toString()), // <6>
63+
new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) // <7>
64+
))
65+
.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
6966
}
7067

7168
// Required to provide UserDetailsService for "remember functionality"

0 commit comments

Comments
 (0)