Commit 2b657ce0 by Patryk Czarnik

uproszczenie SecurityConfig zgodnie z rozwiązaniem znalezionym przez

Pawła
parent a097ca64
......@@ -8,13 +8,8 @@ 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;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.provisioning.JdbcUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import jakarta.servlet.DispatcherType;
......@@ -23,28 +18,27 @@ public class SecurityConfig {
// W tej wersji zapisujemy reguły dostępu ("autoryzacji").
// Określamy też, że do uwierzytelnienia służyć będzie formularz generowany automatycznie przez Springa.
@Bean
SecurityFilterChain configHttpSecurity(HttpSecurity httpSecurity, HandlerMappingIntrospector hmi) throws Exception {
MvcRequestMatcher.Builder mvc = new MvcRequestMatcher.Builder(hmi);
SecurityFilterChain configHttpSecurity(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeHttpRequests(authz -> authz
// zezwalamy na działanie przekierowań wewnętrznych (szablony) i błędów
.dispatcherTypeMatchers(DispatcherType.FORWARD, DispatcherType.ERROR).permitAll()
.requestMatchers("/", "/hello", "/czas", "/skrypt").permitAll()
.requestMatchers(mvc.pattern("/whoami")).permitAll()
.requestMatchers("/whoami").permitAll()
.requestMatchers("/*.css", "/*.js").permitAll()
.requestMatchers(mvc.pattern("/alt?/**")).authenticated() // zalogowany jako ktokolwiek
.requestMatchers("/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(mvc.pattern("/products/new"), mvc.pattern("/products/*/edit")).hasAuthority("ROLE_manager")
.requestMatchers(mvc.pattern("/products/szukaj")).authenticated()
.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(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"
.requestMatchers("/products/new", "/products/*/edit").hasAuthority("ROLE_manager")
.requestMatchers("/products/szukaj").authenticated()
.requestMatchers("/products/**").permitAll() // pozostałe zapytanie w obrębie products - dopuszczamy wszystkich; kolejność reguł ma znaczenie
.requestMatchers("/customers/new", "/customers/*/edit").hasRole("manager") // skrót na hasAuthority("ROLE_...")
.requestMatchers("/customers/**").authenticated()
.requestMatchers(HttpMethod.GET, "/rest/**").permitAll()
.requestMatchers(HttpMethod.POST, "/rest/**").hasRole("manager")
.requestMatchers(HttpMethod.PUT, "/rest/**").hasRole("manager")
.requestMatchers(HttpMethod.DELETE, "/rest/**").hasRole("manager")
.anyRequest().denyAll() // dobra praktyka - odrzucanie pozostałych zapytań
)
.formLogin(Customizer.withDefaults())
// .formLogin(config -> config
......@@ -53,11 +47,11 @@ public class SecurityConfig {
// .passwordParameter("haslo")
// .failureForwardUrl("/porazka-logowania")
// )
.csrf(config -> config.ignoringRequestMatchers("/rest/**"))
.cors(config -> config.disable());
// Dla zapytań restowych większy sens ma takie ustawienie:
// .httpBasic(Customizer.withDefaults())
// .csrf(config -> config.disable())
.csrf(config -> config.ignoringRequestMatchers("/rest/**"))
.cors(config -> config.disable());
return httpSecurity.build();
}
......
@Bean
SecurityFilterChain configHttpSecurity(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeHttpRequests(authz -> authz
.dispatcherTypeMatchers(DispatcherType.FORWARD, DispatcherType.ERROR).permitAll()
.requestMatchers("/", "/hello", "/czas", "/skrypt").permitAll()
.requestMatchers("/whoami").permitAll()
.requestMatchers("/*.css", "/*.js").permitAll()
.requestMatchers("/alt?/**").authenticated()
.requestMatchers("/products/new", "/products/*/edit").hasRole("manager")
.requestMatchers("/products/szukaj").authenticated()
.requestMatchers("/products/**").permitAll()
.requestMatchers("/customers/new", "/customers/*/edit").hasRole("manager")
.requestMatchers("/customers/**").authenticated()
.requestMatchers(HttpMethod.GET, "/rest/**").permitAll()
.requestMatchers(HttpMethod.POST, "/rest/**").hasRole("manager")
.requestMatchers(HttpMethod.PUT, "/rest/**").hasRole("manager")
.requestMatchers(HttpMethod.DELETE, "/rest/**").hasRole("manager")
.anyRequest().denyAll()
)
.formLogin(Customizer.withDefaults())
.csrf(csrf -> csrf.ignoringRequestMatchers("/rest/**"))
.cors(cors -> cors.disable());
return httpSecurity.build();
}
\ No newline at end of file
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