Giriş
Şu satırı dahil ederiz
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
Bu sınıf HttpBasic Authentication kullanılıyorsa tercih edilebilir.
constructor
Şöyle yaparız
@Beanpublic InMemoryUserDetailsManager inMemoryUserDetailsManager() { UserDetails admin = User.builder() .username("gurkan") .password(passwordEncoder.encode("pass")) .authorities("ADMIN") .build(); UserDetails user = User.builder() .username("mehmet") .password(passwordEncoder.encode("pass")) .authorities("USER") .build(); return new InMemoryUserDetailsManager(admin, user); }
createUser metodu
Örnek
Şöyle yaparız
@Configurationpublic class SecurityConfiguration extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().httpBasic();}@Override@Beanprotected UserDetailsService userDetailsService() {InMemoryUserDetailsManager inMemoryUserDetailsManager =
new InMemoryUserDetailsManager();UserDetails user1 = User.builder().username("user").password("password")
.roles("USER").build();UserDetails user2 = User.builder().username("admin").password("password")
.roles("ADMIN").build();inMemoryUserDetailsManager.createUser(user1);inMemoryUserDetailsManager.createUser(user2);return inMemoryUserDetailsManager;}@Beanpublic PasswordEncoder passwordEncoder() {return NoOpPasswordEncoder.getInstance();}}
Hiç yorum yok:
Yorum Gönder