-
[SpringBoot] Spring Security์์ ํน์ url ์ ์ธํ๊ณ , ์ ์ธํ๋๋ฐ ํํฐ ์ ์ฉ๋๋ ๋ฌธ์ ํด๊ฒฐ๐ปProgramming/SpringBoot 2024. 1. 12. 11:23
@Configuration @RequiredArgsConstructor @EnableWebSecurity public class SecurityConfig { private final JwtUtil jwtUtil; private final ResponseUtil responseUtil; @Bean public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { httpSecurity .formLogin(AbstractHttpConfigurer::disable) .csrf(AbstractHttpConfigurer::disable) .sessionManagement((sessionManagement) -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(httpRequests -> httpRequests .requestMatchers(CorsUtils::isPreFlightRequest).permitAll() .requestMatchers(new AntPathRequestMatcher("/") , new AntPathRequestMatcher("/css/**") , new AntPathRequestMatcher("/images/**") ).permitAll() .requestMatchers("/api/v1/member/login/oauth/google/**").permitAll() .requestMatchers("/api/v1/member/login/oauth/google/callback").permitAll() .requestMatchers("/favicon.ico").permitAll() .requestMatchers("/api/v1/event").permitAll() .anyRequest().authenticated() ) .addFilterAfter(jwtAuthenticationProcessingFilter(), LogoutFilter.class) .addFilterBefore(new ExceptionHandlerFilter(responseUtil), JwtAuthenticationProcessingFilter.class); return httpSecurity.build(); } @Bean public WebSecurityCustomizer configure() { return (web) -> web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations()); } @Bean public JwtAuthenticationProcessingFilter jwtAuthenticationProcessingFilter() { return new JwtAuthenticationProcessingFilter(jwtUtil, responseUtil); } }
์๊พธ ์ด๋ ๊ฒ ๋ก๊ทธ์ธ uri์ด๋ ๋ก๊ทธ์ธ ์ฝ๋ฐฑ uri๋ฑ๋ฑ ์ธ์ฆ๋์ง ์์ ์ ์ ๋ ์ ๊ทผ ๊ฐ๋ฅํ uri๋ค์ ๋ฑ๋กํด ์ฃผ์๋๋ฐ๋, ์๊พธ jwtAuthenticationProcessingFilter(์ปค์คํ ํํฐ)๋ฅผ ๋จผ์ ๊ฑฐ์ณ์ ํ ํฐ์ด ์๋ค๊ณ Exception์ด ๋ฐ์ํ๋ ๋ฌธ์ ๊ฐ ์์๋ค.
์๊ณ ๋ณด๋, jwtAuthenticationProcessingFilter๋ฅผ Bean์ผ๋ก ๋ฑ๋กํด์ฃผ์ด์ ๊ทธ๋ฐ ๊ฒ์ด์๋ค...!!!!
๊ทธ๋์ ๊ทธ๋ฅ SecurityFilterChain์ ๋ฑ๋ก๋๋๋ก,
.addFilterAfter(new JwtAuthenticationProcessingFilter(jwtUtil, responseUtil), LogoutFilter.class)
์๋์๊ฐ์ด ๋ฃ์ด์ฃผ์๋๋, SecurityConfig์์ ํ์ฉํ uri๋ค์ JwtAuthenticationProcessingFilter๋ฅผ ๊ฑฐ์น์ง ์์๋ค!!!๐๐ป๐
๐ Reference
์ ๋ง๊ฐ์ฌํฉ๋๋ค.. ๊ณ์ ์ด๋ฌธ์ ๋๋ฌธ์ ์ด๋ฐ์์์๋๋ฐ ๋น์ ์ด ์ ์ด๋ ธ์๋๋ค
'๐ปProgramming > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ