11package com .back .global .config ;
22
3+ import java .io .IOException ;
4+
35import org .springframework .beans .factory .annotation .Value ;
46import org .springframework .context .annotation .Bean ;
57import org .springframework .context .annotation .Configuration ;
1416import org .springframework .web .cors .CorsConfiguration ;
1517import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
1618
19+ import com .back .global .error .code .AuthErrorCode ;
20+ import com .back .global .error .code .ErrorCode ;
1721import com .back .global .properties .CorsProperties ;
22+ import com .back .global .response .ApiResponse ;
1823import com .back .global .security .CustomAuthenticationFilter ;
24+ import com .fasterxml .jackson .databind .ObjectMapper ;
1925
2026import jakarta .servlet .http .HttpServletResponse ;
2127import lombok .RequiredArgsConstructor ;
@@ -28,6 +34,7 @@ public class SecurityConfig {
2834
2935 private final CorsProperties corsProperties ;
3036 private final CustomAuthenticationFilter authenticationFilter ;
37+ private final ObjectMapper objectMapper ;
3138
3239 @ Bean
3340 SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
@@ -40,8 +47,12 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4047 .requestMatchers ("/h2-console/**" ).permitAll () // H2 콘솔 접근 허용
4148 .requestMatchers ("/swagger-ui/**" , "/v3/api-docs/**" ).permitAll () // Swagger 접근 허용
4249 .requestMatchers ("/.well-known/**" ).permitAll ()
43- //.requestMatchers("/api/v1/admin/**").hasRole("ADMIN") //추후 주석 해제
50+ .requestMatchers ("/api/v1/auth/signup" ).permitAll ()
51+ .requestMatchers ("/api/v1/auth/login" ).permitAll ()
52+ .requestMatchers ("/api/v1/admin/auth/**" ).permitAll ()
53+ .requestMatchers ("/api/v1/admin/**" ).hasRole ("ADMIN" )
4454 .requestMatchers ("/actuator/**" ).permitAll () // 모니터링/Actuator 관련
55+ // .requestMatchers("/api/v1/**").authenticated() // TODO: 개발 후 인증 활성화
4556 .anyRequest ().permitAll () // TODO: 보안 인증 설정 시 제거, 현재는 모든 API 요청을 인증없이 허용
4657 )
4758 .csrf (csrf -> csrf
@@ -56,27 +67,11 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
5667 //401 403 커스텀 에러
5768 .exceptionHandling (exceptionHandling -> exceptionHandling
5869 .authenticationEntryPoint ((request , response , authException ) -> {
59- response .setContentType ("application/json; charset=UTF-8" );
60- response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
61- response .getWriter ().write ("""
62- {
63- "status": "UNAUTHORIZED",
64- "message": "로그인 후 이용해주세요.",
65- "data": null
66- }
67- """ );
70+ writeError (response , AuthErrorCode .UNAUTHORIZED );
6871 })
6972
7073 .accessDeniedHandler ((request , response , accessDeniedException ) -> {
71- response .setContentType ("application/json; charset=UTF-8" );
72- response .setStatus (HttpServletResponse .SC_FORBIDDEN );
73- response .getWriter ().write ("""
74- {
75- "status": "FORBIDDEN",
76- "message": "접근 권한이 없습니다.",
77- "data": null
78- }
79- """ );
74+ writeError (response , AuthErrorCode .FORBIDDEN );
8075 })
8176 );
8277
@@ -105,4 +100,12 @@ public PasswordEncoder passwordEncoder(
105100 ) {
106101 return new BCryptPasswordEncoder (strength );
107102 }
103+
104+ private void writeError (HttpServletResponse response , ErrorCode code ) throws IOException {
105+ response .setStatus (code .getHttpStatus ().value ());
106+ response .setContentType ("application/json; charset=UTF-8" );
107+
108+ ApiResponse <?> body = ApiResponse .fail (code );
109+ response .getWriter ().write (objectMapper .writeValueAsString (body ));
110+ }
108111}
0 commit comments