Skip to content

Commit 45de049

Browse files
committed
✨ feat: 사용자/관리자 인증 정보 조회 기능
1 parent 5e98631 commit 45de049

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.f1.backend.domain.auth.api;
2+
3+
import static io.f1.backend.global.util.SecurityUtils.getAuthentication;
4+
5+
import io.f1.backend.domain.admin.dto.AdminPrincipal;
6+
import io.f1.backend.domain.auth.dto.CurrentUserAndAdminResponse;
7+
import io.f1.backend.domain.user.dto.UserPrincipal;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.security.core.Authentication;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
@RestController
16+
@RequestMapping("/auth")
17+
@RequiredArgsConstructor
18+
public class AuthController {
19+
20+
@GetMapping("/me")
21+
public ResponseEntity<?> getCurrentUserOrAdmin() {
22+
Authentication authentication = getAuthentication();
23+
Object principal = authentication.getPrincipal();
24+
25+
if (principal instanceof UserPrincipal userPrincipal) {
26+
return ResponseEntity.ok(CurrentUserAndAdminResponse.from(userPrincipal));
27+
}
28+
return ResponseEntity.ok(CurrentUserAndAdminResponse.from((AdminPrincipal) principal));
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.f1.backend.domain.auth.dto;
2+
3+
import io.f1.backend.domain.admin.dto.AdminPrincipal;
4+
import io.f1.backend.domain.user.dto.UserPrincipal;
5+
6+
public record CurrentUserAndAdminResponse(Long id, String name, String role) {
7+
8+
public static CurrentUserAndAdminResponse from(UserPrincipal userPrincipal) {
9+
return new CurrentUserAndAdminResponse(
10+
userPrincipal.getUserId(),
11+
userPrincipal.getUserNickname(),
12+
UserPrincipal.ROLE_USER
13+
);
14+
}
15+
16+
public static CurrentUserAndAdminResponse from(AdminPrincipal adminPrincipal) {
17+
return new CurrentUserAndAdminResponse(
18+
adminPrincipal.getAuthenticationAdmin().adminId(),
19+
adminPrincipal.getUsername(),
20+
AdminPrincipal.ROLE_ADMIN
21+
);
22+
}
23+
}

backend/src/main/java/io/f1/backend/global/config/SecurityConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public SecurityFilterChain userFilterChain(HttpSecurity http) throws Exception {
5151
.hasRole("USER")
5252
.requestMatchers("/admin/**")
5353
.hasRole("ADMIN")
54+
.requestMatchers("/auth/me")
55+
.hasAnyRole("USER", "ADMIN")
5456
.anyRequest()
5557
.authenticated())
5658
.formLogin(form -> form

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,29 @@
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-
76
import jakarta.servlet.http.HttpSession;
8-
7+
import java.util.Collections;
98
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
109
import org.springframework.security.core.Authentication;
1110
import org.springframework.security.core.context.SecurityContextHolder;
1211

13-
import java.util.Collections;
14-
1512
public class SecurityUtils {
1613

17-
private SecurityUtils() {}
14+
private SecurityUtils() {
15+
}
1816

1917
public static void setAuthentication(User user) {
2018
UserPrincipal userPrincipal = new UserPrincipal(user, Collections.emptyMap());
2119
UsernamePasswordAuthenticationToken authentication =
22-
new UsernamePasswordAuthenticationToken(
23-
userPrincipal, null, userPrincipal.getAuthorities());
20+
new UsernamePasswordAuthenticationToken(
21+
userPrincipal, null, userPrincipal.getAuthorities());
2422
SecurityContextHolder.getContext().setAuthentication(authentication);
2523
}
2624

2725
public static UserPrincipal getCurrentUserPrincipal() {
2826
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
2927
if (authentication != null
30-
&& authentication.getPrincipal() instanceof UserPrincipal userPrincipal) {
28+
&& authentication.getPrincipal() instanceof UserPrincipal userPrincipal) {
3129
return userPrincipal;
3230
}
3331
throw new RuntimeException("E401001: 로그인이 필요합니다.");
@@ -60,4 +58,8 @@ public static AdminPrincipal getCurrentAdminPrincipal() {
6058
}
6159
throw new RuntimeException("E401001: 로그인이 필요합니다.");
6260
}
61+
62+
public static Authentication getAuthentication() {
63+
return SecurityContextHolder.getContext().getAuthentication();
64+
}
6365
}

0 commit comments

Comments
 (0)