Skip to content
Open
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
Expand Up @@ -6,4 +6,8 @@ public interface PrincipalProvider {
Long userId();
Set<Role> roles();
Set<CouncilDepartment> councilDepartments();

// 지금 요청이 들고 온 JWT 원문. 이 사용자 권한으로 다른 서버가 학생 API를 대신 부를 때만 쓴다(AI 서버 중계).
// 로그·응답에 남기지 않는다
String accessToken();
}
1 change: 1 addition & 0 deletions docs/conventions/config-and-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public interface PrincipalProvider {
Long userId();
Set<Role> roles();
Set<Department> departments(); // STUDENT면 빈 Set
String accessToken(); // 요청의 JWT 원문. AI 서버 중계처럼 학생 권한으로 대신 부를 때만 쓴다
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public Set<CouncilDepartment> councilDepartments() {
return currentAuthentication().councilDepartments();
}

@Override
public String accessToken() {
return currentAuthentication().accessToken();
}

private UserAuthentication currentAuthentication() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof UserAuthentication userAuthentication)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected void doFilterInternal(
) throws ServletException, IOException {
String token = resolveToken(request);
if (token != null) {
SecurityContextHolder.getContext().setAuthentication(UserAuthentication.from(jwtProvider.parse(token)));
SecurityContextHolder.getContext().setAuthentication(UserAuthentication.of(jwtProvider.parse(token), token));
}
filterChain.doFilter(request, response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ public class UserAuthentication extends AbstractAuthenticationToken {
private final Long userId;
private final Set<Role> roles;
private final Set<CouncilDepartment> councilDepartments;
// 검증을 마친 JWT 원문. Bearer 파싱은 JwtAuthFilter 한 곳에서만 하고, 다른 모듈은 PrincipalProvider로 꺼낸다
private final String accessToken;

private UserAuthentication(JwtPayload payload) {
private UserAuthentication(JwtPayload payload, String accessToken) {
super(toAuthorities(payload));
this.userId = payload.userId();
this.roles = payload.roles();
this.councilDepartments = payload.councilDepartments();
this.accessToken = accessToken;
setAuthenticated(true);
}

public static UserAuthentication from(JwtPayload payload) {
return new UserAuthentication(payload);
public static UserAuthentication of(JwtPayload payload, String accessToken) {
return new UserAuthentication(payload, accessToken);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.ac.kookmin.stream.client.chat.streamai;

import java.util.List;
import kr.ac.kookmin.stream.common.PrincipalProvider;
import kr.ac.kookmin.stream.welfare.domain.chat.client.ChatClient;
import kr.ac.kookmin.stream.welfare.domain.chat.client.ChatEventListener;
import kr.ac.kookmin.stream.welfare.domain.chat.domain.ChatTurn;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class StreamAiChatClient implements ChatClient {

private final StreamAiProperties properties;
private final ObjectMapper objectMapper;
private final PrincipalProvider principalProvider;

@Override
public void stream(Long memberId, List<ChatTurn> history, ChatEventListener listener) {
Expand All @@ -51,6 +53,10 @@ public void stream(Long memberId, List<ChatTurn> history, ChatEventListener list
.uri(properties.chatPath())
.header("X-Internal-Token", properties.internalToken())
.header("X-User-Id", String.valueOf(memberId))
// AI 서버가 이 학생 권한으로 기존 학생 API를 조회(GET)한다. 예: 빌릴게 재고, 내 대여.
// 토큰은 인자로 받지 않는다 — 받으면 ChatClient(도메인)가 인증 개념을 알아야 한다(config-and-auth.md 4-4절).
// PrincipalProvider는 요청 스레드 기준이라, 중계를 비동기로 바꾸면 토큰을 꺼낼 수 없다. 토큰 원문은 로그에 남기지 않는다
.header("X-User-Token", principalProvider.accessToken())
.accept(MediaType.TEXT_EVENT_STREAM)
.body(StreamAiChatRequest.from(history))
.exchange((request, response) -> {
Expand Down