15 Haziran 2021 Salı

SpringSecurity InMemoryUserDetailsManager Sınıfı - Default UserDetailsManager Sınıfı Budur

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
@Bean
public 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
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
  }

  @Override
  @Bean
  protected 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;
  }

  @Bean
  public PasswordEncoder passwordEncoder() {
   return NoOpPasswordEncoder.getInstance();
  }
}

Hiç yorum yok:

Yorum Gönder