Skip to content

Commit ea8e7ee

Browse files
committed
Merge branch 'main' of https://github.com/prgrms-web-devcourse-final-project/WEB1_1_Bongdari_BE into bug/152-community-paging
2 parents f16c7fb + 4837aa7 commit ea8e7ee

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
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/auth/jwt/filter/JwtAuthFilter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ protected void doFilterInternal(HttpServletRequest request,
5555

5656
private EncodedToken getAccessToken(HttpServletRequest request) {
5757
String accessToken = request.getHeader("Authorization");
58+
if (accessToken == null || accessToken.isEmpty()) {
59+
throw new JwtException(JwtErrorType.MISSING_TOKEN);
60+
}
5861

5962
String tokenPrefix = "Bearer ";
6063
if (accessToken.startsWith(tokenPrefix)) {
6164
return new EncodedToken(accessToken.substring(tokenPrefix.length()));
6265
}
6366

64-
throw new JwtException(JwtErrorType.MISSING_TOKEN);
67+
return new EncodedToken(accessToken);
6568
}
6669

6770
private JwtAuthenticationToken createAuthenticationToken(Claims claims,

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)