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
Expand Up @@ -18,6 +18,7 @@ public class CustomerResponseDto {
private String phone;
private Gender gender;
private String univ;
private String role;

// 정적 메서드: Entity → DTO 변환
public static CustomerResponseDto fromEntity(Customer customer) {
Expand All @@ -28,6 +29,7 @@ public static CustomerResponseDto fromEntity(Customer customer) {
.phone(customer.getPhone())
.gender(customer.getGender())
.univ(customer.getMyUniv().getName())
.role(customer.getRole().getAuthority())
.build();
}
}
28 changes: 22 additions & 6 deletions src/main/java/ceos/phototoground/global/jwt/LoginFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
Expand Down Expand Up @@ -57,15 +59,17 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authentication) throws IOException {
// 인증된 사용자 정보 가져오기
String username = authentication.getName(); //username 대신 email 사용중
String username = authentication.getName(); // username 대신 email 사용 중
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
String role = authorities.iterator().next().getAuthority(); // 첫 번째 권한 추출

// JWT 토큰 생성 (Access Token: 10분, Refresh Token: 24시간)
// 단일 권한 추출
String role = authorities.iterator().next().getAuthority();

// JWT 토큰 생성 (Access Token: 1시간, Refresh Token: 24시간)
String accessToken = jwtUtil.createJwt("access", username, role, 3600000L); // 1시간
String refreshToken = jwtUtil.createJwt("refresh", username, role, 86400000L); // 24시간

//Refresh 토큰 저장
// Refresh 토큰 저장
addRefreshEntity(username, refreshToken, 86400000L);

// Access Token을 헤더에 추가
Expand All @@ -75,11 +79,23 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
Cookie refreshCookie = createCookie("refresh", refreshToken);
response.addCookie(refreshCookie);

// 응답 바디에 성공 메시지 추가
// 응답 바디에 성공 메시지와 사용자 정보 (권한 포함) 추가
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

SuccessResponseDto<String> successResponse = SuccessResponseDto.successMessage("로그인 성공");
// 사용자 데이터 구성
Map<String, Object> userData = new HashMap<>();
userData.put("username", username);
userData.put("role", role); // 단일 권한 추가

// 성공 응답 생성
SuccessResponseDto<Map<String, Object>> successResponse = SuccessResponseDto.success(
200,
"로그인 성공",
userData
);

// 응답 반환
response.getWriter().write(new ObjectMapper().writeValueAsString(successResponse));
response.getWriter().flush();
}
Expand Down
Loading