
사실상 Security Config에서 Spring Security에 대한 설정은 거의 다 한다고 봐도 무방합니다.
WebSecurityConfigurerAdapter를 상속받아 사용하는 클래스 입니다.
여기서 Security FilterChain이 등록되기도 합니다.
보안 정책, 로그인, 권한 등등 많은 일을 Security에서 합니다.(보안 정책에 관한건 더 자세히 다루겠음)
여기선 로그인, 권한 설정 등만 다룰 것 입니다.
(참고로 본인의 미니 프로젝트를 진행 하면서 소스코드를 기록하는거라 코드가 매우 본인 주관적임)
@Configuration
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
}
Spring Security에서 보안, 권한 등을 설정하는 메소드 입니다.
지금은 권한만 다룰 예정입니다.
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/signup").permitAll()
.antMatchers("/join").permitAll()
.antMatchers("/main").hasAnyRole("USER","ADMIN")
.antMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/main")
;
}
Spring Security에서 web 보안에 관련된 부분 입니다.
본인도 이부분은 이해가 잘 가지않아 관련하여 이해도가 높으신 분은 댓글로 설명 부탁드립니다.
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/css/**", "/js/**", "/img/**");
}
Spring Security에서 로그인 인증을 관리하는 메소드 입니다.
이부분에 대해선 이후 챕터에서 다룰 예정입니다.(로그인과 관련 되어서 설명하기 위해)
기본적인 메소드 형식은 코드만 보고 넘어가 주시면 됩니다.
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// TODO Auto-generated method stub
auth.authenticationProvider(authenticationProvider());
}
이렇게 Spring Security에서 없어서는 안되는 SecurityConfig에 대해 알아보았습니다.
궁굼한점이나 틀린부분 있으면 댓글로 욕해주세요.