특정한 경로에 요청이 오면 Controller 클래스에 도달하기 전 필터에서 Spring Security가 검증을 함
인가를 설정하는 클래스
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterCHain(HttpSecurity http) throw Exception{
http
.authorizeHttpRequests((auth)->
auth
.requestMatchers("/login","/join").permitAll()
.requestMatchers("/").permitAll()
.requestMatchers("/my/**").hasAnyRole("ADMIN","USER")
.requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticatecd()
);
return http.build();
}
}
스프링 시큐리티 COnfig 클래스 설정 후 특정 경로에 대한 접근 권한이 없는 경우 자동으로 로그인 페이지로 리다이렉팅 되지 않고 오류 페이지가 발생한다.