Skip to content

Commit 4cf3947

Browse files
committed
♻️ refactor: SecurityUtils 커스텀 예외처리 적용
1 parent 275462a commit 4cf3947

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

backend/src/main/java/io/f1/backend/global/util/SecurityUtils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@
33
import io.f1.backend.domain.admin.dto.AdminPrincipal;
44
import io.f1.backend.domain.user.dto.UserPrincipal;
55
import io.f1.backend.domain.user.entity.User;
6-
6+
import io.f1.backend.global.exception.CustomException;
7+
import io.f1.backend.global.exception.errorcode.AuthErrorCode;
78
import jakarta.servlet.http.HttpSession;
8-
9+
import java.util.Collections;
910
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1011
import org.springframework.security.core.Authentication;
1112
import org.springframework.security.core.context.SecurityContextHolder;
1213

13-
import java.util.Collections;
14-
1514
public class SecurityUtils {
1615

17-
private SecurityUtils() {}
16+
private SecurityUtils() {
17+
}
1818

1919
public static void setAuthentication(User user) {
2020
UserPrincipal userPrincipal = new UserPrincipal(user, Collections.emptyMap());
2121
UsernamePasswordAuthenticationToken authentication =
22-
new UsernamePasswordAuthenticationToken(
23-
userPrincipal, null, userPrincipal.getAuthorities());
22+
new UsernamePasswordAuthenticationToken(
23+
userPrincipal, null, userPrincipal.getAuthorities());
2424
SecurityContextHolder.getContext().setAuthentication(authentication);
2525
}
2626

2727
public static UserPrincipal getCurrentUserPrincipal() {
2828
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
2929
if (authentication != null
30-
&& authentication.getPrincipal() instanceof UserPrincipal userPrincipal) {
30+
&& authentication.getPrincipal() instanceof UserPrincipal userPrincipal) {
3131
return userPrincipal;
3232
}
3333
throw new RuntimeException("E401001: 로그인이 필요합니다.");
@@ -55,10 +55,10 @@ private static void clearAuthentication() {
5555
public static AdminPrincipal getCurrentAdminPrincipal() {
5656
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
5757
if (authentication != null
58-
&& authentication.getPrincipal() instanceof AdminPrincipal adminPrincipal) {
58+
&& authentication.getPrincipal() instanceof AdminPrincipal adminPrincipal) {
5959
return adminPrincipal;
6060
}
61-
throw new RuntimeException("E401001: 로그인이 필요합니다.");
61+
throw new CustomException(AuthErrorCode.UNAUTHORIZED);
6262
}
6363

6464
public static Authentication getAuthentication() {

0 commit comments

Comments
 (0)