Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.somemore.auth.controller;

import com.somemore.auth.dto.UserInfoResponseDto;
import com.somemore.global.common.response.ApiResponse;
import com.somemore.global.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.somemore.global.exception.ExceptionMessage.INVALID_TOKEN;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/token")
public class UserInfoQueryController {

@GetMapping("/userinfo")
public ApiResponse<UserInfoResponseDto> getUserInfoBySCH() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

String userId = authentication.getPrincipal().toString();
String role = authentication.getAuthorities().stream()
.findFirst()
.map(GrantedAuthority::getAuthority)
.orElseThrow(() -> new BadRequestException(INVALID_TOKEN));

return ApiResponse.ok(200,
new UserInfoResponseDto(userId, role),
"유저 정보 응답 성공");
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/somemore/auth/dto/UserInfoResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.somemore.auth.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "유저 정보 DTO")
public record UserInfoResponseDto(
@JsonProperty("USER_ID")
@Schema(description = "유저 ID")
String userId,

@JsonProperty("ROLE")
@Schema(description = "유저 ROLE")
String role
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ protected void doFilterInternal(HttpServletRequest request,

private EncodedToken getAccessToken(HttpServletRequest request) {
String accessToken = request.getHeader("Authorization");
if (accessToken == null || accessToken.isEmpty()) {
throw new JwtException(JwtErrorType.MISSING_TOKEN);
}

String tokenPrefix = "Bearer ";
if (accessToken.startsWith(tokenPrefix)) {
return new EncodedToken(accessToken.substring(tokenPrefix.length()));
}

throw new JwtException(JwtErrorType.MISSING_TOKEN);
return new EncodedToken(accessToken);
}

private JwtAuthenticationToken createAuthenticationToken(Claims claims,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Getter
public enum ExceptionMessage {

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