Commit bcd2a63b by Patryk Czarnik

inMemoryAuthentication

parent a0b604aa
package sklep.security;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
......@@ -10,6 +14,10 @@ public class SecurityConfig {
// W klasie typu Configuration umieszczamy metody, które w wyniku zwracają obiekty, które dla Springa coś specjalnego znaczą.
// Te metody oznaczamy adnotacją @Bean.
// (w ten sposób można też stworzyć "własne" beany, zamiast podejścia z adnotacją @Component)
// Ta metoda służy ogólnej konfiguracji zabezpieczeń aplikacji webowej.
// M.in. podaje się tutaj reguły dostępu, czyli autoryzacje.
// Tutaj też podaje się sposób uwierzytelnienia, np. formLogin.
@Bean
SecurityFilterChain setHttpSecurity(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeHttpRequests()
......@@ -19,4 +27,22 @@ public class SecurityConfig {
return httpSecurity.build();
}
// W osobnej metodzie określa się źródło wiedzy o użytkownikach: jacy są użytkownicy, jakie mają hasła.
// Są różne rodzaje takich źródeł: inMemory - użytkownicy wpisani na sztywno w kodzie; jdbc - w bazie danych; ldap - w zewnętrznym systemie; można zaimplementować samemu
@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration, ApplicationContext applicationContext) throws Exception {
ObjectPostProcessor<Object> objectPostProcessor = new ObjectPostProcessor<Object>() {
public <O> O postProcess(O object) {
return object;
}
};
return authenticationConfiguration.authenticationManagerBuilder(objectPostProcessor, applicationContext)
.inMemoryAuthentication()
.withUser("ala").password("{noop}kot").roles("manager", "innarola").and()
.withUser("ola").password("{noop}pies").roles("pomocnik").and()
.and()
.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