Commit 143f6e90 by Patryk Czarnik

Poprawka reguł autoryzacji do najnowszego Springa

parent 23837b01
......@@ -15,6 +15,8 @@ import org.springframework.security.provisioning.JdbcUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import jakarta.servlet.DispatcherType;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
// https://docs.spring.io/spring-security/reference/servlet/getting-started.html
......@@ -29,23 +31,28 @@ import jakarta.servlet.DispatcherType;
@EnableWebSecurity
public class SecurityConfig {
// Konfiguracja właściwa dla SprSec >= 6.1.2 na podstawie
// https://github.com/spring-projects/spring-security/issues/13568
// błąd opisany https://spring.io/security/cve-2023-34035
@Bean
SecurityFilterChain configHttpSecurity(HttpSecurity httpSecurity) throws Exception {
SecurityFilterChain configHttpSecurity(HttpSecurity httpSecurity, HandlerMappingIntrospector hmi) throws Exception {
MvcRequestMatcher.Builder mvc = new MvcRequestMatcher.Builder(hmi);
httpSecurity
.authorizeHttpRequests(authz -> authz
.anyRequest().authenticated()
// zezwalamy na działanie przekierowań wewnętrznych (szablony) i błędów
// .dispatcherTypeMatchers(DispatcherType.FORWARD, DispatcherType.ERROR).permitAll()
// .requestMatchers("/", "/whoami", "/*.css").permitAll()
// .requestMatchers("/hello", "/time").permitAll()
// .requestMatchers("/alt?/**").authenticated() // zalogowany jako ktokolwiek
.dispatcherTypeMatchers(DispatcherType.FORWARD, DispatcherType.ERROR).permitAll()
.requestMatchers(mvc.pattern("/")).permitAll()
.requestMatchers(mvc.pattern("/whoami")).permitAll()
.requestMatchers(mvc.pattern("/*.css")).permitAll()
.requestMatchers(mvc.pattern("/hello"), mvc.pattern("/time")).permitAll()
.requestMatchers(mvc.pattern("/alt?/**")).authenticated() // zalogowany jako ktokolwiek
// kolejność reguł ma znaczenie - pierwsza reguła, do której pasuje zapytanie, jest decydująca
// np. /products/new wymaga uprawnień managera, chociaż to zapytanie pasowałoby te z do /products/**
// .requestMatchers("/products/new", "/products/*/edit").hasAuthority("ROLE_manager")
// .requestMatchers("/products/**").permitAll() // kolejność reguł ma znaczenie
// .requestMatchers("/customers/new", "/customers/*/edit").hasRole("manager") // skrót na hasAuthority("ROLE_...")
// .requestMatchers("/customers/**").authenticated()
// .anyRequest().denyAll() // dobra praktyka - odrzucanie pozostałych zapytań; Spring domyślnie wymagałby "authenticated"
.requestMatchers(mvc.pattern("/products/new"), mvc.pattern("/products/*/edit")).hasAuthority("ROLE_manager")
.requestMatchers(mvc.pattern("/products/**")).permitAll() // kolejność reguł ma znaczenie
.requestMatchers(mvc.pattern("/customers/new"), mvc.pattern("/customers/*/edit")).hasRole("manager") // skrót na hasAuthority("ROLE_...")
.requestMatchers(mvc.pattern("/customers/**")).authenticated()
.anyRequest().denyAll() // dobra praktyka - odrzucanie pozostałych zapytań; Spring domyślnie wymagałby "authenticated"
)
.formLogin(Customizer.withDefaults()) // albo .httpBasic(Customizer.withDefaults())
// .csrf(authz -> authz.disable())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment