bael-6473: upgrade Spring Security to 6

This commit is contained in:
hmdrzsharifi 2023-09-03 16:54:24 +03:30
parent ad85139fe5
commit 0846aa401f
5 changed files with 25 additions and 22 deletions

View File

@ -24,10 +24,10 @@ public class ConsumerDebuggingApplication {
@Bean @Bean
public SecurityWebFilterChain debuggingConsumerSpringSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain debuggingConsumerSpringSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange() http.authorizeExchange(exchanges -> exchanges
.anyExchange() .anyExchange()
.permitAll(); .permitAll());
http.csrf().disable(); http.csrf(csrf -> csrf.disable());
return http.build(); return http.build();
} }
} }

View File

@ -22,9 +22,9 @@ public class ServerDebuggingApplication {
@Bean @Bean
public SecurityWebFilterChain debuggingServerSpringSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain debuggingServerSpringSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange() http.authorizeExchange(exchanges -> exchanges
.anyExchange() .anyExchange()
.permitAll(); .permitAll());
return http.build(); return http.build();
} }
} }

View File

@ -1,6 +1,7 @@
package com.baeldung.reactive.security; package com.baeldung.reactive.security;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; 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;
@ -12,18 +13,19 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain; import org.springframework.security.web.server.SecurityWebFilterChain;
@EnableWebFluxSecurity @EnableWebFluxSecurity
@Configuration
@EnableReactiveMethodSecurity @EnableReactiveMethodSecurity
public class SecurityConfig { public class SecurityConfig {
@Bean @Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http.authorizeExchange() return http
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN") .authorizeExchange(exchanges -> exchanges
.anyExchange().authenticated() .pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
.and() .anyExchange().authenticated())
.formLogin() .formLogin(formLogin -> formLogin
.and() .loginPage("/login"))
.csrf().disable() .csrf(csrf -> csrf.disable())
.build(); .build();
} }

View File

@ -16,9 +16,8 @@ public class WebClientApplication {
@Bean @Bean
public SecurityWebFilterChain filterChain(ServerHttpSecurity http) { public SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
http.csrf().disable() http.csrf(csrf -> csrf.disable())
.authorizeExchange() .authorizeExchange(exchanges -> exchanges.anyExchange().permitAll());
.anyExchange().permitAll();
return http.build(); return http.build();
} }
} }

View File

@ -1,7 +1,9 @@
package com.baeldung.reactive.webflux.annotation; package com.baeldung.reactive.webflux.annotation;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; 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;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService; import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
@ -12,6 +14,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain; import org.springframework.security.web.server.SecurityWebFilterChain;
@EnableWebFluxSecurity @EnableWebFluxSecurity
@Configuration
public class EmployeeWebSecurityConfig { public class EmployeeWebSecurityConfig {
@Bean @Bean
@ -27,12 +30,11 @@ public class EmployeeWebSecurityConfig {
@Bean @Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http http
.csrf().disable() .csrf(csrf -> csrf.disable())
.authorizeExchange() .authorizeExchange(exchanges -> exchanges
.pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN") .pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN")
.pathMatchers("/**").permitAll() .pathMatchers("/**").permitAll())
.and() .httpBasic(Customizer.withDefaults());
.httpBasic();
return http.build(); return http.build();
} }