자동 설정 유저 정보는
- 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 계정도 사용 가능
문제점
- 여전히 비밀번호가 코드에 있다
- 유저 정보를 등록할 때마다 코드 수정이 필요하다
'스터디-Spring' 카테고리의 다른 글
[스프링 시큐리티] 스프링 시큐리티 커스터마이징 - PasswordEncoder (0) | 2022.01.08 |
---|---|
[스프링 시큐리티] 스프링 시큐리티 커스터마이징 - JPA 연동 (0) | 2022.01.07 |
[스프링 시큐리티] 스프링 시큐리티 설정하기 (0) | 2022.01.07 |
[스프링 시큐리티] 스프링 시큐리티 연동 (0) | 2022.01.07 |
[스프링 핵심 원리 - 기본편] 컴포넌트 스캔과 자동 의존관계 설정 (0) | 2022.01.03 |