Commit 1d56b30c by Patryk Czarnik

Konfiguracja security dla zaptyań restowych

parent 9444e72b
......@@ -5,6 +5,7 @@ import jakarta.servlet.DispatcherType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
......@@ -35,10 +36,9 @@ public class SecurityConfig {
.authorizeHttpRequests(authz -> authz
// zezwalamy na działanie przekierowań wewnętrznych (szablony) i błędów
.dispatcherTypeMatchers(DispatcherType.FORWARD, DispatcherType.ERROR).permitAll()
.requestMatchers(mvc.pattern("/")).permitAll()
.requestMatchers("/", "/hello", "/czas").permitAll()
.requestMatchers(mvc.pattern("/whoami")).permitAll()
.requestMatchers(mvc.pattern("/*.css")).permitAll()
.requestMatchers(mvc.pattern("/hello"), mvc.pattern("/czas")).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/**
......@@ -47,12 +47,23 @@ public class SecurityConfig {
.requestMatchers(mvc.pattern("/products/**")).permitAll() // pozostałe zapytanie w obrębie products - dopuszczamy wszystkich; 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()
.requestMatchers(mvc.pattern("/rest/**")).permitAll()
.requestMatchers(mvc.pattern(HttpMethod.GET, "/rest/**")).permitAll()
.requestMatchers(mvc.pattern(HttpMethod.POST, "/rest/**")).hasRole("manager")
.requestMatchers(mvc.pattern(HttpMethod.PUT, "/rest/**")).hasRole("manager")
.requestMatchers(mvc.pattern(HttpMethod.DELETE, "/rest/**")).hasRole("manager")
.anyRequest().denyAll() // dobra praktyka - odrzucanie pozostałych zapytań; Spring domyślnie wymagałby "authenticated"
)
.formLogin(Customizer.withDefaults())
// .formLogin(config -> config
// .loginPage("/moja_strona_logowania")
// .usernameParameter("nazwa_uzytkownika")
// .passwordParameter("haslo")
// .failureForwardUrl("/porazka-logowania")
// )
// Dla zapytań restowych większy sens ma takie ustawienie:
// .httpBasic(Customizer.withDefaults())
.csrf(config -> config.disable())
// .csrf(config -> config.disable())
.csrf(config -> config.ignoringRequestMatchers("/rest/**"))
.cors(config -> config.disable());
return httpSecurity.build();
}
......
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