|
| 1 | +package io.ticketaka.api.common.infrastructure.security |
| 2 | + |
| 3 | +import io.ticketaka.api.user.application.CustomOAuth2UserService |
| 4 | +import io.ticketaka.api.user.application.CustomOidcUserService |
| 5 | +import io.ticketaka.api.user.infrastructure.CustomAuthorityMapper |
| 6 | +import io.ticketaka.api.user.infrastructure.jwt.JwtAuthenticationFilter |
| 7 | +import io.ticketaka.api.user.infrastructure.jwt.JwtExceptionFilter |
| 8 | +import org.springframework.context.annotation.Bean |
| 9 | +import org.springframework.context.annotation.Configuration |
| 10 | +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity |
| 11 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity |
| 12 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity |
| 13 | +import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper |
| 14 | +import org.springframework.security.web.AuthenticationEntryPoint |
| 15 | +import org.springframework.security.web.SecurityFilterChain |
| 16 | +import org.springframework.security.web.access.AccessDeniedHandler |
| 17 | +import org.springframework.security.web.access.ExceptionTranslationFilter |
| 18 | +import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint |
| 19 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter |
| 20 | + |
| 21 | +@Configuration |
| 22 | +@EnableWebSecurity(debug = true) |
| 23 | +@EnableMethodSecurity |
| 24 | +class SecurityConfig( |
| 25 | + private val accessDeniedHandler: AccessDeniedHandler, |
| 26 | + private val authenticationEntryPoint: AuthenticationEntryPoint, |
| 27 | + private val customOAuth2UserService: CustomOAuth2UserService, |
| 28 | + private val customOidcUserService: CustomOidcUserService, |
| 29 | + private val jwtExceptionFilter: JwtExceptionFilter, |
| 30 | + private val jwtAuthenticationFilter: JwtAuthenticationFilter, |
| 31 | +) { |
| 32 | + /* |
| 33 | + * |
| 34 | +Security filter chain: [ |
| 35 | + DisableEncodeUrlFilter |
| 36 | + WebAsyncManagerIntegrationFilter |
| 37 | + SecurityContextHolderFilter |
| 38 | + HeaderWriterFilter |
| 39 | + CorsFilter |
| 40 | + LogoutFilter |
| 41 | + OAuth2AuthorizationRequestRedirectFilter |
| 42 | + OAuth2LoginAuthenticationFilter |
| 43 | + JwtAuthenticationFilter |
| 44 | + DefaultLoginPageGeneratingFilter |
| 45 | + DefaultLogoutPageGeneratingFilter |
| 46 | + RequestCacheAwareFilter |
| 47 | + SecurityContextHolderAwareRequestFilter |
| 48 | + AnonymousAuthenticationFilter |
| 49 | + SessionManagementFilter |
| 50 | + JwtExceptionFilter |
| 51 | + ExceptionTranslationFilter |
| 52 | + AuthorizationFilter |
| 53 | +] |
| 54 | +
|
| 55 | + * */ |
| 56 | + @Bean |
| 57 | + fun filterChain(http: HttpSecurity): SecurityFilterChain { |
| 58 | + http |
| 59 | + .csrf { it.disable() } |
| 60 | +// .sessionManagement { it.sessionCreationPolicy(SessionCreationPolicy.STATELESS) } |
| 61 | + .authorizeHttpRequests { it.anyRequest().permitAll() } // TODO oauth 구현이후 endpoint별 권한 설정 필요 |
| 62 | + .formLogin { |
| 63 | + it |
| 64 | + .loginPage("/login") |
| 65 | + .loginProcessingUrl("/loginProc") |
| 66 | + .defaultSuccessUrl("/") |
| 67 | + .permitAll() |
| 68 | + }.oauth2Login { oauth2LoginCustomizer -> |
| 69 | + oauth2LoginCustomizer.userInfoEndpoint { userInfoEndpointConfig -> |
| 70 | + userInfoEndpointConfig |
| 71 | + .userService(customOAuth2UserService) |
| 72 | +// .oidcUserService(null) |
| 73 | + } |
| 74 | + }.exceptionHandling { it.authenticationEntryPoint(LoginUrlAuthenticationEntryPoint("/login")) } |
| 75 | + .logout { it.logoutSuccessUrl("/") } |
| 76 | + .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter::class.java) |
| 77 | + .addFilterBefore(jwtExceptionFilter, ExceptionTranslationFilter::class.java) |
| 78 | + return http.build() |
| 79 | + } |
| 80 | + |
| 81 | + @Bean |
| 82 | + fun exceptionTranslationFilter(): ExceptionTranslationFilter { |
| 83 | + val filter = ExceptionTranslationFilter(authenticationEntryPoint) |
| 84 | + filter.setAccessDeniedHandler(accessDeniedHandler) |
| 85 | + return filter |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + fun customAuthorityMapper(): GrantedAuthoritiesMapper = CustomAuthorityMapper() |
| 90 | +} |
0 commit comments