Commit b64f2ea9 by Patryk Czarnik

security: użytkownicy InMemory

parent 90f7865c
...@@ -2,19 +2,40 @@ package sklep.security; ...@@ -2,19 +2,40 @@ package sklep.security;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 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.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
@Configuration @Configuration
public class SecurityConfig { public class SecurityConfig {
// Uproszczona konfiguracja zabezpieczeń HTTP. // W tej wersji dla każdego zapytanie będziemy wymagać uwierzytelnienia
// Określamy bardzo liberalną politykę autoryzacji - każdy do dostęp do każdego elementu aplikacji. // (czyli użytkownik musi się zalogować)
// Określamy też, że do uwierzytelnienia służyć będzie formularz generowany automatycznie przez Springa.
@Bean @Bean
SecurityFilterChain configureHttpSecurity(HttpSecurity httpSecurity) throws Exception { SecurityFilterChain configureHttpSecurity(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeHttpRequests(authz -> authz.anyRequest().permitAll()); httpSecurity.authorizeHttpRequests(authz -> authz.anyRequest().authenticated())
httpSecurity.csrf(csrf -> csrf.disable()); .formLogin(Customizer.withDefaults())
.csrf(csrf -> csrf.disable());
return httpSecurity.build(); return httpSecurity.build();
} }
// Druga metoda służy do określenia, skąd biorą się użytkownicy i ich hasła.
// W tej wersji definiujemy użytkowników "w pamięci" wpisując nazwy i hasła bezpośrednio w kodzie.
// Obecnie Spring używa domyślnie "DelegatePasswordEncoder", który zapisuje hasło w takiej postaci:
// {rodzaj_algorytmu}ZAKODOWANEHASŁO
// przy czym dostępnych jest wiele algorytmów kodowania haseł, w tym popularny bcrypt
// Jako algorytm kodowania haseł może być też użyty 'noop', co oznacza, że hasło jest zapisane jawnie.
@Bean
InMemoryUserDetailsManager userDetailsService() {
UserDetails[] users = {
User.withUsername("ala").password("{noop}ala123").roles("manager", "worker").build(),
User.withUsername("ola").password("{noop}ola123").roles("worker").build(),
User.withUsername("ela").password("{bcrypt}$2a$12$zauO4uOXT7OxOsYN.CoZ6em.OPwrf7AMb0LQIoyLlxYaDTzyfzjnG").roles("worker").build(),
// hasło Eli zakodowane za pomocą bcrypt: abc123
};
return new InMemoryUserDetailsManager(users);
}
} }
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