본문 바로가기

Spring/SpringSecurity

SpringSecurity- SecurityConfig 설정

@EnableWebSecurity
@EnableMethodSecurity
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {
    
    
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers("/예외처리하고 싶은 url", "/예외처리하고 싶은 url");
    }
    
    @Bean
    protected SecurityFilterChain webSecurityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests()
                .requestMatchers("/로그인페이지", "/css/**", "/images/**", "/js/**").permitAll()
                .anyRequest().authenticated()

            .and()
                .formLogin()
                .loginPage("/로그인페이지")
                .loginProcessingUrl("/실제 로그인이 되는 url")
                .permitAll()
                .successHandler(로그인 성공 시 실행할 커스터마이즈드 핸들러)
                .failureHandler(로그인 실패 시 실행할 커스터마이즈드 핸들러);

        http
                .sessionManagement()
                .invalidSessionUrl("/로그인페이지")

            .and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/실제 로그아웃이 되는 url"))
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID")
                .permitAll();


        //CSRF 토큰
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());

        return http.build();
    }
}

 


Reference

최신 Spring Security 사용법 - SecurityFilterChain (tistory.com)