diff --git a/src/main/java/com/pyonsnalcolor/config/SecurityConfig.java b/src/main/java/com/pyonsnalcolor/config/SecurityConfig.java new file mode 100644 index 0000000..418180d --- /dev/null +++ b/src/main/java/com/pyonsnalcolor/config/SecurityConfig.java @@ -0,0 +1,80 @@ +package com.pyonsnalcolor.config; + +import com.pyonsnalcolor.member.security.AuthUserDetailsService; +import com.pyonsnalcolor.member.security.JwtAuthenticationFilter; +import com.pyonsnalcolor.handler.JwtAccessDeniedHandler; +import com.pyonsnalcolor.handler.JwtAuthenticationEntryPoint; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.client.RestTemplateBuilder; +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.config.annotation.web.configuration.WebSecurityCustomizer; +import org.springframework.security.config.http.SessionCreationPolicy; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.web.client.RestTemplate; + +@EnableWebSecurity +@Configuration +public class SecurityConfig { + + @Autowired + JwtAuthenticationFilter jwtAuthenticationFilter; + + @Autowired + JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; + + @Autowired + JwtAccessDeniedHandler jwtAccessDeniedHandler; + + @Bean + public WebSecurityCustomizer webSecurityCustomizer() { + return (web) -> web.ignoring() + .antMatchers( "/resources/**", + "/v3/api-docs/**", + "/swagger-ui/**", + "/health-check" + ); + } + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http + .httpBasic().disable() + .csrf().disable() + .sessionManagement() + .sessionCreationPolicy(SessionCreationPolicy.STATELESS) + .and() + .authorizeRequests() + .antMatchers("/auth/**", "/promotions/**", "/fcm/**", "/manage/**").permitAll() + .antMatchers("/member/**").hasRole("USER") + .anyRequest().authenticated() + .and() + .exceptionHandling((exceptions) -> exceptions + .authenticationEntryPoint(jwtAuthenticationEntryPoint) + .accessDeniedHandler(jwtAccessDeniedHandler)) + .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); + + return http.build(); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Bean + public UserDetailsService userDetailsService() { + return new AuthUserDetailsService(); + } + + @Bean + public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { + return restTemplateBuilder.build(); + } +} \ No newline at end of file