Skip to content

Commit 20abe97

Browse files
committed
feat(UserInfo): 엑세스 토큰으로 만들어진 SCH의 Authentication으로부터 userId와 role 조회
1 parent 2b4eb7a commit 20abe97

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.somemore.auth.controller;
2+
3+
import com.somemore.auth.dto.UserInfoResponseDto;
4+
import com.somemore.global.common.response.ApiResponse;
5+
import com.somemore.global.exception.BadRequestException;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.security.core.Authentication;
8+
import org.springframework.security.core.GrantedAuthority;
9+
import org.springframework.security.core.context.SecurityContextHolder;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import static com.somemore.global.exception.ExceptionMessage.INVALID_TOKEN;
15+
16+
@RestController
17+
@RequiredArgsConstructor
18+
@RequestMapping("/api/token")
19+
public class UserInfoQueryController {
20+
21+
@GetMapping("/userinfo")
22+
public ApiResponse<UserInfoResponseDto> getUserInfoBySCH() {
23+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
24+
25+
String userId = authentication.getPrincipal().toString();
26+
String role = authentication.getAuthorities().stream()
27+
.findFirst()
28+
.map(GrantedAuthority::getAuthority)
29+
.orElseThrow(() -> new BadRequestException(INVALID_TOKEN));
30+
31+
return ApiResponse.ok(200,
32+
new UserInfoResponseDto(userId, role),
33+
"유저 정보 응답 성공");
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.somemore.auth.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
6+
@Schema(description = "유저 정보 DTO")
7+
public record UserInfoResponseDto(
8+
@JsonProperty("USER_ID")
9+
@Schema(description = "유저 ID")
10+
String userId,
11+
12+
@JsonProperty("ROLE")
13+
@Schema(description = "유저 ROLE")
14+
String role
15+
) {
16+
}

src/main/java/com/somemore/global/exception/ExceptionMessage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@Getter
99
public enum ExceptionMessage {
1010

11+
INVALID_TOKEN("잘못된 엑세스 토큰입니다"),
1112
NOT_EXISTS_CENTER("존재하지 않는 기관입니다."),
1213
NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글입니다."),
1314
UNAUTHORIZED_COMMUNITY_BOARD("해당 게시글에 권한이 없습니다."),

0 commit comments

Comments
 (0)