From ac2bffd20eb73ed52b1500a6039af8bc48a62a61 Mon Sep 17 00:00:00 2001 From: Hanbee Lee Date: Thu, 1 Feb 2024 00:12:43 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=95=88?= =?UTF-8?q?=EB=90=9C=20=EC=83=81=ED=83=9C=20=EC=BD=94=EB=93=9C=20500=5F3?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/api/config/SecurityConfig.java | 2 +- .../example/api/config/security/SecurityUtil.java | 4 +++- .../api/user/service/GetUserGenresUseCase.java | 4 ++++ .../exception/SecurityContextNotFoundException.java | 13 +++++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 Api/src/main/java/com/example/api/user/service/GetUserGenresUseCase.java create mode 100644 Core/src/main/java/com/example/error/exception/SecurityContextNotFoundException.java diff --git a/Api/src/main/java/com/example/api/config/SecurityConfig.java b/Api/src/main/java/com/example/api/config/SecurityConfig.java index 38421f2..a2953f1 100644 --- a/Api/src/main/java/com/example/api/config/SecurityConfig.java +++ b/Api/src/main/java/com/example/api/config/SecurityConfig.java @@ -55,7 +55,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti http.authorizeRequests() .requestMatchers("/api/auth/**").permitAll() .requestMatchers( "/api/swagger-ui/**", "/api/v3/api-docs/**").permitAll() - .requestMatchers("/api/**").authenticated(); + .requestMatchers("/api/**").authenticated().and().anonymous().disable(); http.apply(filterConfig); diff --git a/Api/src/main/java/com/example/api/config/security/SecurityUtil.java b/Api/src/main/java/com/example/api/config/security/SecurityUtil.java index 8e0a0c8..ab68e63 100644 --- a/Api/src/main/java/com/example/api/config/security/SecurityUtil.java +++ b/Api/src/main/java/com/example/api/config/security/SecurityUtil.java @@ -1,13 +1,15 @@ package com.example.api.config.security; +import com.example.error.exception.SecurityContextNotFoundException; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; public class SecurityUtil { public static Long getCurrentUserId() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null) { - throw new IllegalArgumentException("잘못된 접근"); + throw SecurityContextNotFoundException.EXCEPTION; } return Long.valueOf(authentication.getName()); } diff --git a/Api/src/main/java/com/example/api/user/service/GetUserGenresUseCase.java b/Api/src/main/java/com/example/api/user/service/GetUserGenresUseCase.java new file mode 100644 index 0000000..69a73bc --- /dev/null +++ b/Api/src/main/java/com/example/api/user/service/GetUserGenresUseCase.java @@ -0,0 +1,4 @@ +package com.example.api.user.service; + +public class GetUserGenresUseCase { +} diff --git a/Core/src/main/java/com/example/error/exception/SecurityContextNotFoundException.java b/Core/src/main/java/com/example/error/exception/SecurityContextNotFoundException.java new file mode 100644 index 0000000..0468398 --- /dev/null +++ b/Core/src/main/java/com/example/error/exception/SecurityContextNotFoundException.java @@ -0,0 +1,13 @@ +package com.example.error.exception; + +import com.example.error.BaseErrorException; +import com.example.error.GlobalErrorCode; + +public class SecurityContextNotFoundException extends BaseErrorException { + + public static final BaseErrorException EXCEPTION = new SecurityContextNotFoundException(); + + private SecurityContextNotFoundException() { + super(GlobalErrorCode.SECURITY_CONTEXT_NOT_FOUND); + } +} \ No newline at end of file