Spring Boot 3.1.3 SecurityFilterChain 설정

Seokjun Moon·2023년 9월 7일
0

삽질로그

목록 보기
1/10

2.7.13 버전에서 SecurityFilterChain 구현을 위한 Bean을 보면

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
    return httpSecurity
            .csrf().disable()
            .formLogin().disable()
            .httpBasic().disable()            

            .and()
            .authorizeRequests()
            .mvcMatchers("/cart/**", "/option/**", "/order/**", "/user/**").authenticated()
            .mvcMatchers("/admin/**").hasRole("ADMIN")
            .anyRequest().permitAll()

            .and()
            .exceptionHandling().authenticationEntryPoint((request, response, authException) -> {
                createErrorResponse(response, new UnauthorizedRequestException("인증되지 않은 요청입니다."));
            })

            .and()
            .exceptionHandling().accessDeniedHandler((request, response, accessDeniedException) -> {
                createErrorResponse(response, new PermissionDeniedException("권한이 없습니다."));
            })

            .and().build();
}

이런식으로도 설정이 가능했지만, 3.1.3 버전을 사용해보니

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            .csrf(AbstractHttpConfigurer::disable)
            .formLogin(AbstractHttpConfigurer::disable)
            .authorizeHttpRequests(authorize -> authorize
                    .requestMatchers("/join").permitAll()
                    .requestMatchers("/login").permitAll()
                    .anyRequest().authenticated())
            .exceptionHandling(configurer -> {
                configurer.authenticationEntryPoint(((request, response, authException) -> {
                
                }));
                configurer.accessDeniedHandler(((request, response, accessDeniedException) -> {

                }));
            });
    return httpSecurity.build();
}

이렇게 람다식만을 이용하도록 바뀌었다. 다른 메서드들도 다 람다식만을 이용하도록 변경되었다고 ... 이걸 몰라서 30분을 삽질 .... 이래서 한상곤 교수님께서 말씀하셨던 자기가 쓰는 버전의 특징을 빠삭하게 알고 있어야 한다는 말의 의미를 몸소 느끼게 되었다 ...

그럼 이제 spring boot 3.x.x 버전의 특징을 공부해봐야겠다!

profile
차근차근 천천히

0개의 댓글