스터디-Spring

[스프링 시큐리티] 스프링 시큐리티 커스터마이징 - 인메모리 유저 추가

일태우 2022. 1. 7. 14:45
반응형

자동 설정 유저 정보는

  • UserDetailsServiceAutoConfiguration
  • SecurityProperties

위의 클래스를 통해서 설정 된다.

 

Properties에서 유저정보 설정하기

application.properties

spring.security.user.name=admin
spring.security.user.password=123
spring.security.user.roles=ADMIN

위와 같이 설정하면 어플리케이션 시작시 자동 설정되던 유저는 사라지고 위의 정보로 세팅된다. roles도 설정되었기 때문에 /admin 페이지도 접근 가능하다

 

SecurityConfig에 설정하기

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .mvcMatchers("/", "/info").permitAll()
                .mvcMatchers("/admin").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .and()
            .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("twlee").password("{noop}123").roles("USER").and()
                .withUser("admin").password("{noop}!@#").roles("ADMIN");
    }
}
  • inMemory 사용자 추가
  • 여러 사용자를 등록할 수 있음
  • Admin 계정도 사용 가능

 

문제점

  • 여전히 비밀번호가 코드에 있다
  • 유저 정보를 등록할 때마다 코드 수정이 필요하다
반응형