|
3 | 3 | import io.f1.backend.domain.admin.dto.AdminPrincipal; |
4 | 4 | import io.f1.backend.domain.user.dto.UserPrincipal; |
5 | 5 | 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; |
7 | 8 | import jakarta.servlet.http.HttpSession; |
8 | | - |
| 9 | +import java.util.Collections; |
9 | 10 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
10 | 11 | import org.springframework.security.core.Authentication; |
11 | 12 | import org.springframework.security.core.context.SecurityContextHolder; |
12 | 13 |
|
13 | | -import java.util.Collections; |
14 | | - |
15 | 14 | public class SecurityUtils { |
16 | 15 |
|
17 | | - private SecurityUtils() {} |
| 16 | + private SecurityUtils() { |
| 17 | + } |
18 | 18 |
|
19 | 19 | public static void setAuthentication(User user) { |
20 | 20 | UserPrincipal userPrincipal = new UserPrincipal(user, Collections.emptyMap()); |
21 | 21 | UsernamePasswordAuthenticationToken authentication = |
22 | | - new UsernamePasswordAuthenticationToken( |
23 | | - userPrincipal, null, userPrincipal.getAuthorities()); |
| 22 | + new UsernamePasswordAuthenticationToken( |
| 23 | + userPrincipal, null, userPrincipal.getAuthorities()); |
24 | 24 | SecurityContextHolder.getContext().setAuthentication(authentication); |
25 | 25 | } |
26 | 26 |
|
27 | 27 | public static UserPrincipal getCurrentUserPrincipal() { |
28 | 28 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
29 | 29 | if (authentication != null |
30 | | - && authentication.getPrincipal() instanceof UserPrincipal userPrincipal) { |
| 30 | + && authentication.getPrincipal() instanceof UserPrincipal userPrincipal) { |
31 | 31 | return userPrincipal; |
32 | 32 | } |
33 | 33 | throw new RuntimeException("E401001: 로그인이 필요합니다."); |
@@ -55,10 +55,10 @@ private static void clearAuthentication() { |
55 | 55 | public static AdminPrincipal getCurrentAdminPrincipal() { |
56 | 56 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
57 | 57 | if (authentication != null |
58 | | - && authentication.getPrincipal() instanceof AdminPrincipal adminPrincipal) { |
| 58 | + && authentication.getPrincipal() instanceof AdminPrincipal adminPrincipal) { |
59 | 59 | return adminPrincipal; |
60 | 60 | } |
61 | | - throw new RuntimeException("E401001: 로그인이 필요합니다."); |
| 61 | + throw new CustomException(AuthErrorCode.UNAUTHORIZED); |
62 | 62 | } |
63 | 63 |
|
64 | 64 | public static Authentication getAuthentication() { |
|
0 commit comments