diff --git a/.DS_Store b/.DS_Store index 656d0cc3..a7fff75a 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/docker-compose.yaml b/docker-compose.yaml index 03025d8b..21af7561 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -27,7 +27,7 @@ services: - web-net ports: - "6379:6379" - + networks: web-net: external: true \ No newline at end of file diff --git a/postgres-data/base/1/1259 b/postgres-data/base/1/1259 new file mode 100644 index 00000000..b0743d38 Binary files /dev/null and b/postgres-data/base/1/1259 differ diff --git a/postgres-data/base/1/2619 b/postgres-data/base/1/2619 new file mode 100644 index 00000000..e3f14000 Binary files /dev/null and b/postgres-data/base/1/2619 differ diff --git a/postgres-data/base/1/2619_fsm b/postgres-data/base/1/2619_fsm new file mode 100644 index 00000000..047f30ec Binary files /dev/null and b/postgres-data/base/1/2619_fsm differ diff --git a/postgres-data/base/1/2619_vm b/postgres-data/base/1/2619_vm new file mode 100644 index 00000000..adf76f71 Binary files /dev/null and b/postgres-data/base/1/2619_vm differ diff --git a/postgres-data/base/1/2696 b/postgres-data/base/1/2696 new file mode 100644 index 00000000..416c1193 Binary files /dev/null and b/postgres-data/base/1/2696 differ diff --git a/postgres-data/base/1/2840 b/postgres-data/base/1/2840 new file mode 100644 index 00000000..cbe06ae5 Binary files /dev/null and b/postgres-data/base/1/2840 differ diff --git a/postgres-data/base/1/2841 b/postgres-data/base/1/2841 new file mode 100644 index 00000000..0812c765 Binary files /dev/null and b/postgres-data/base/1/2841 differ diff --git a/postgres-data/base/1/pg_internal.init b/postgres-data/base/1/pg_internal.init new file mode 100644 index 00000000..6a2fb9dd Binary files /dev/null and b/postgres-data/base/1/pg_internal.init differ diff --git a/postgres-data/base/16384/1247 b/postgres-data/base/16384/1247 new file mode 100644 index 00000000..16143e60 Binary files /dev/null and b/postgres-data/base/16384/1247 differ diff --git a/postgres-data/base/16384/1247_vm b/postgres-data/base/16384/1247_vm new file mode 100644 index 00000000..809b37e4 Binary files /dev/null and b/postgres-data/base/16384/1247_vm differ diff --git a/postgres-data/base/16384/1249 b/postgres-data/base/16384/1249 new file mode 100644 index 00000000..8a6601bf Binary files /dev/null and b/postgres-data/base/16384/1249 differ diff --git a/postgres-data/base/16384/1249_fsm b/postgres-data/base/16384/1249_fsm new file mode 100644 index 00000000..abf10d4a Binary files /dev/null and b/postgres-data/base/16384/1249_fsm differ diff --git a/postgres-data/base/16384/1249_vm b/postgres-data/base/16384/1249_vm new file mode 100644 index 00000000..80b666ad Binary files /dev/null and b/postgres-data/base/16384/1249_vm differ diff --git a/postgres-data/base/16384/1259 b/postgres-data/base/16384/1259 new file mode 100644 index 00000000..912b697d Binary files /dev/null and b/postgres-data/base/16384/1259 differ diff --git a/postgres-data/base/16384/1259_vm b/postgres-data/base/16384/1259_vm new file mode 100644 index 00000000..72bbc858 Binary files /dev/null and b/postgres-data/base/16384/1259_vm differ diff --git a/postgres-data/base/16384/2224 b/postgres-data/base/16384/2224 new file mode 100644 index 00000000..e69de29b diff --git a/postgres-data/base/16384/2579 b/postgres-data/base/16384/2579 new file mode 100644 index 00000000..2ff12b20 Binary files /dev/null and b/postgres-data/base/16384/2579 differ diff --git a/postgres-data/base/16384/2604 b/postgres-data/base/16384/2604 new file mode 100644 index 00000000..e69de29b diff --git a/postgres-data/base/16384/2606 b/postgres-data/base/16384/2606 new file mode 100644 index 00000000..56124c4a Binary files /dev/null and b/postgres-data/base/16384/2606 differ diff --git a/postgres-data/base/16384/2606_vm b/postgres-data/base/16384/2606_vm new file mode 100644 index 00000000..a0360502 Binary files /dev/null and b/postgres-data/base/16384/2606_vm differ diff --git a/postgres-data/base/16384/2608 b/postgres-data/base/16384/2608 new file mode 100644 index 00000000..f7265aca Binary files /dev/null and b/postgres-data/base/16384/2608 differ diff --git a/postgres-data/base/16384/2608_fsm b/postgres-data/base/16384/2608_fsm new file mode 100644 index 00000000..a3bf2006 Binary files /dev/null and b/postgres-data/base/16384/2608_fsm differ diff --git a/postgres-data/base/16384/2608_vm b/postgres-data/base/16384/2608_vm new file mode 100644 index 00000000..35fb86c3 Binary files /dev/null and b/postgres-data/base/16384/2608_vm differ diff --git a/postgres-data/base/16384/2610 b/postgres-data/base/16384/2610 new file mode 100644 index 00000000..cc22b9e4 Binary files /dev/null and b/postgres-data/base/16384/2610 differ diff --git a/postgres-data/base/16384/2610_vm b/postgres-data/base/16384/2610_vm new file mode 100644 index 00000000..6263e9df Binary files /dev/null and b/postgres-data/base/16384/2610_vm differ diff --git a/postgres-data/base/16384/2656 b/postgres-data/base/16384/2656 new file mode 100644 index 00000000..78b92d2e Binary files /dev/null and b/postgres-data/base/16384/2656 differ diff --git a/postgres-data/base/16384/2657 b/postgres-data/base/16384/2657 new file mode 100644 index 00000000..23cb71ef Binary files /dev/null and b/postgres-data/base/16384/2657 differ diff --git a/postgres-data/base/16384/2658 b/postgres-data/base/16384/2658 new file mode 100644 index 00000000..70a52dcf Binary files /dev/null and b/postgres-data/base/16384/2658 differ diff --git a/postgres-data/base/16384/2659 b/postgres-data/base/16384/2659 new file mode 100644 index 00000000..124f612c Binary files /dev/null and b/postgres-data/base/16384/2659 differ diff --git a/postgres-data/base/16384/2662 b/postgres-data/base/16384/2662 new file mode 100644 index 00000000..6568fb1c Binary files /dev/null and b/postgres-data/base/16384/2662 differ diff --git a/postgres-data/base/16384/2663 b/postgres-data/base/16384/2663 new file mode 100644 index 00000000..1f0c6a31 Binary files /dev/null and b/postgres-data/base/16384/2663 differ diff --git a/postgres-data/base/16384/2664 b/postgres-data/base/16384/2664 new file mode 100644 index 00000000..fa9dd803 Binary files /dev/null and b/postgres-data/base/16384/2664 differ diff --git a/postgres-data/base/16384/2665 b/postgres-data/base/16384/2665 new file mode 100644 index 00000000..1c2a6bb4 Binary files /dev/null and b/postgres-data/base/16384/2665 differ diff --git a/postgres-data/base/16384/2666 b/postgres-data/base/16384/2666 new file mode 100644 index 00000000..235fb63c Binary files /dev/null and b/postgres-data/base/16384/2666 differ diff --git a/postgres-data/base/16384/2667 b/postgres-data/base/16384/2667 new file mode 100644 index 00000000..6a831bb3 Binary files /dev/null and b/postgres-data/base/16384/2667 differ diff --git a/postgres-data/base/16384/2673 b/postgres-data/base/16384/2673 new file mode 100644 index 00000000..1f2f1c97 Binary files /dev/null and b/postgres-data/base/16384/2673 differ diff --git a/postgres-data/base/16384/2674 b/postgres-data/base/16384/2674 new file mode 100644 index 00000000..bb6c6251 Binary files /dev/null and b/postgres-data/base/16384/2674 differ diff --git a/postgres-data/base/16384/2678 b/postgres-data/base/16384/2678 new file mode 100644 index 00000000..bb401861 Binary files /dev/null and b/postgres-data/base/16384/2678 differ diff --git a/postgres-data/base/16384/2679 b/postgres-data/base/16384/2679 new file mode 100644 index 00000000..fd5dc6df Binary files /dev/null and b/postgres-data/base/16384/2679 differ diff --git a/postgres-data/base/16384/2703 b/postgres-data/base/16384/2703 new file mode 100644 index 00000000..6ce2b0c4 Binary files /dev/null and b/postgres-data/base/16384/2703 differ diff --git a/postgres-data/base/16384/2704 b/postgres-data/base/16384/2704 new file mode 100644 index 00000000..ddc89efb Binary files /dev/null and b/postgres-data/base/16384/2704 differ diff --git a/postgres-data/base/16384/3455 b/postgres-data/base/16384/3455 new file mode 100644 index 00000000..aa0e8c41 Binary files /dev/null and b/postgres-data/base/16384/3455 differ diff --git a/postgres-data/base/16384/5002 b/postgres-data/base/16384/5002 new file mode 100644 index 00000000..f1142884 Binary files /dev/null and b/postgres-data/base/16384/5002 differ diff --git a/postgres-data/base/5/pg_internal.init b/postgres-data/base/5/pg_internal.init new file mode 100644 index 00000000..9968738b Binary files /dev/null and b/postgres-data/base/5/pg_internal.init differ diff --git a/postgres-data/global/1260 b/postgres-data/global/1260 new file mode 100644 index 00000000..a8525cd8 Binary files /dev/null and b/postgres-data/global/1260 differ diff --git a/postgres-data/global/pg_control b/postgres-data/global/pg_control new file mode 100644 index 00000000..6991ae25 Binary files /dev/null and b/postgres-data/global/pg_control differ diff --git a/postgres-data/global/pg_internal.init b/postgres-data/global/pg_internal.init new file mode 100644 index 00000000..7e146b82 Binary files /dev/null and b/postgres-data/global/pg_internal.init differ diff --git a/postgres-data/pg_stat/pgstat.stat b/postgres-data/pg_stat/pgstat.stat new file mode 100644 index 00000000..8ae215de Binary files /dev/null and b/postgres-data/pg_stat/pgstat.stat differ diff --git a/postgres-data/pg_wal/000000010000000000000001 b/postgres-data/pg_wal/000000010000000000000001 new file mode 100644 index 00000000..8fc78473 Binary files /dev/null and b/postgres-data/pg_wal/000000010000000000000001 differ diff --git a/postgres-data/pg_xact/0000 b/postgres-data/pg_xact/0000 new file mode 100644 index 00000000..51787831 Binary files /dev/null and b/postgres-data/pg_xact/0000 differ diff --git a/src/main/java/hello/cluebackend/domain/classroom/domain/ClassRoom.java b/src/main/java/hello/cluebackend/domain/classroom/domain/ClassRoom.java new file mode 100644 index 00000000..03aeb93a --- /dev/null +++ b/src/main/java/hello/cluebackend/domain/classroom/domain/ClassRoom.java @@ -0,0 +1,55 @@ +package hello.cluebackend.domain.classroom.domain; + +import hello.cluebackend.domain.classroom.presentation.dto.ClassRoomDTO; +import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser; +import jakarta.persistence.*; +import lombok.*; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@Entity +@Table(name="class_room") +@Getter +@Setter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class ClassRoom { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(nullable = false) + private Long classRoomId; + + @Column(nullable = false) + private String name; + + @Column(nullable = false) + private String description; + + @Column(nullable = false) + private String code; + + @Column(nullable = false) + private LocalDateTime createdAt; + + @PrePersist + protected void onCreate() { + createdAt = LocalDateTime.now(); + } + + @OneToMany(mappedBy = "classRoom", cascade = CascadeType.ALL) + private List classRoomUserList = new ArrayList<>(); + + public ClassRoomDTO toDTO() { + return ClassRoomDTO.builder() + .classRoomId(classRoomId) + .name(name) + .description(description) + .code(code) + .createdAt(createdAt) + .build(); + } +} diff --git a/src/main/java/hello/cluebackend/domain/classroom/domain/repository/ClassRoomRepository.java b/src/main/java/hello/cluebackend/domain/classroom/domain/repository/ClassRoomRepository.java new file mode 100644 index 00000000..7bc50d40 --- /dev/null +++ b/src/main/java/hello/cluebackend/domain/classroom/domain/repository/ClassRoomRepository.java @@ -0,0 +1,9 @@ +package hello.cluebackend.domain.classroom.domain.repository; + +import hello.cluebackend.domain.classroom.domain.ClassRoom; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ClassRoomRepository extends JpaRepository { +} diff --git a/src/main/java/hello/cluebackend/domain/classroom/presentation/ClassRoomController.java b/src/main/java/hello/cluebackend/domain/classroom/presentation/ClassRoomController.java new file mode 100644 index 00000000..b8275ee6 --- /dev/null +++ b/src/main/java/hello/cluebackend/domain/classroom/presentation/ClassRoomController.java @@ -0,0 +1,57 @@ +package hello.cluebackend.domain.classroom.presentation; + +import hello.cluebackend.domain.classroom.presentation.dto.ClassRoomDTO; +import hello.cluebackend.domain.classroom.service.ClassRoomService; +import hello.cluebackend.domain.user.domain.Role; +import hello.cluebackend.global.config.JWTUtil; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; + +@RestController +@RequestMapping("/api/class") +public class ClassRoomController { + + private final JWTUtil jwtUtil; + private final ClassRoomService classRoomService; + + public ClassRoomController(JWTUtil jwtUtil, ClassRoomService classRoomService) { + this.jwtUtil = jwtUtil; + this.classRoomService = classRoomService; + } + + @GetMapping("/request") + public List getClassRoom(HttpServletRequest request) { + String authHeader = request.getHeader("Authorization"); + String token = authHeader.substring(7); + Long userId = jwtUtil.getUserId(token); + return classRoomService.findMyClassRoomById(userId); + } + + @PostMapping("/create-room") + public ResponseEntity> createClassRoom(@RequestBody ClassRoomDTO classRoomDTO, HttpServletRequest request) { + String authHeader = request.getHeader("Authorization"); + String token = authHeader.substring(7); + Role role = jwtUtil.getRole(token); + Long userId = jwtUtil.getUserId(token); + if(!role.name().equals("teacher")) { + return new ResponseEntity<>(HttpStatus.FORBIDDEN); + } + try { + classRoomService.createClassRoom(classRoomDTO, userId); + return new ResponseEntity<>(HttpStatus.CREATED); + } catch (RuntimeException e){ + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + } + + @GetMapping("/{classid}") + public ClassRoomDTO getClassRoom(@PathVariable Long classid, HttpServletRequest request) { + return classRoomService.getClassRoomByClassId(classid); + } + +} \ No newline at end of file diff --git a/src/main/java/hello/cluebackend/domain/classroom/presentation/dto/ClassRoomDTO.java b/src/main/java/hello/cluebackend/domain/classroom/presentation/dto/ClassRoomDTO.java new file mode 100644 index 00000000..d2a10669 --- /dev/null +++ b/src/main/java/hello/cluebackend/domain/classroom/presentation/dto/ClassRoomDTO.java @@ -0,0 +1,41 @@ +package hello.cluebackend.domain.classroom.presentation.dto; + +import hello.cluebackend.domain.classroom.domain.ClassRoom; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; +import java.util.Random; + +@Getter +@Setter +@Builder +public class ClassRoomDTO { + + private Long classRoomId; + private String name; + private String description; + private String code; + private LocalDateTime createdAt; + + public void generateCode() { + int length = 6; + StringBuilder randomStringBuilder = new StringBuilder(); + Random random = new Random(); + String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + for (int i = 0; i < length; i++) { + int randomIndex = random.nextInt(characters.length()); + randomStringBuilder.append(characters.charAt(randomIndex)); + } + this.code = randomStringBuilder.toString(); + } + + public ClassRoom toEntity() { + return ClassRoom.builder() + .name(name) + .description(description) + .code(code).build(); + } +} diff --git a/src/main/java/hello/cluebackend/domain/classroom/service/ClassRoomService.java b/src/main/java/hello/cluebackend/domain/classroom/service/ClassRoomService.java new file mode 100644 index 00000000..b56aa98b --- /dev/null +++ b/src/main/java/hello/cluebackend/domain/classroom/service/ClassRoomService.java @@ -0,0 +1,54 @@ +package hello.cluebackend.domain.classroom.service; + +import hello.cluebackend.domain.classroom.domain.ClassRoom; +import hello.cluebackend.domain.classroom.domain.repository.ClassRoomRepository; +import hello.cluebackend.domain.classroom.presentation.dto.ClassRoomDTO; +import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser; +import hello.cluebackend.domain.classroomuser.domain.repository.ClassRoomUserRepository; +import hello.cluebackend.domain.user.domain.UserEntity; +import hello.cluebackend.domain.user.domain.repository.UserRepository; +import jakarta.transaction.Transactional; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ClassRoomService { + + private final ClassRoomUserRepository classRoomUserRepository; + private final ClassRoomRepository classRoomRepository; + private final UserRepository userRepository; + public ClassRoomService(ClassRoomUserRepository classRoomUserRepository, ClassRoomRepository classRoomRepository, UserRepository userRepository) { + this.classRoomUserRepository = classRoomUserRepository; + this.classRoomRepository = classRoomRepository; + this.userRepository = userRepository; + } + + public List findMyClassRoomById(Long id) { + List classRoomUsers = classRoomUserRepository.findByUser_UserId(id); + return classRoomUsers.stream() + .map(ClassRoomUser::getClassRoom) + .map(ClassRoom::toDTO) + .toList(); + } + + @Transactional + public void createClassRoom(ClassRoomDTO classRoomDTO, Long userId) { + classRoomDTO.generateCode(); + ClassRoom classRoom = classRoomDTO.toEntity(); + classRoomRepository.save(classRoom); + + UserEntity user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("user not found")); + + ClassRoomUser classRoomUser = ClassRoomUser.builder() + .classRoom(classRoom) + .user(user) + .build(); + classRoomUserRepository.save(classRoomUser); + } + + public ClassRoomDTO getClassRoomByClassId(Long classid) { + ClassRoom classRoom = classRoomRepository.findById(classid).orElseThrow(() -> new RuntimeException("classroom not found")); + return classRoom.toDTO(); + } +} diff --git a/src/main/java/hello/cluebackend/domain/classroomuser/domain/ClassRoomUser.java b/src/main/java/hello/cluebackend/domain/classroomuser/domain/ClassRoomUser.java new file mode 100644 index 00000000..d9f779a3 --- /dev/null +++ b/src/main/java/hello/cluebackend/domain/classroomuser/domain/ClassRoomUser.java @@ -0,0 +1,29 @@ +package hello.cluebackend.domain.classroomuser.domain; + +import hello.cluebackend.domain.classroom.domain.ClassRoom; +import hello.cluebackend.domain.user.domain.UserEntity; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Table(name="class_room_user") +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ClassRoomUser { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(nullable = false) + private Long classRoomUserId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private UserEntity user; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "class_room_id", nullable = false) + private ClassRoom classRoom; +} diff --git a/src/main/java/hello/cluebackend/domain/classroomuser/domain/repository/ClassRoomUserRepository.java b/src/main/java/hello/cluebackend/domain/classroomuser/domain/repository/ClassRoomUserRepository.java new file mode 100644 index 00000000..7944fa4a --- /dev/null +++ b/src/main/java/hello/cluebackend/domain/classroomuser/domain/repository/ClassRoomUserRepository.java @@ -0,0 +1,14 @@ +package hello.cluebackend.domain.classroomuser.domain.repository; + +import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ClassRoomUserRepository extends JpaRepository { + + List findByUser_UserId(Long userId); + +} \ No newline at end of file diff --git a/src/main/java/hello/cluebackend/domain/user/domain/UserEntity.java b/src/main/java/hello/cluebackend/domain/user/domain/UserEntity.java index fcc5aae3..a5c305fc 100644 --- a/src/main/java/hello/cluebackend/domain/user/domain/UserEntity.java +++ b/src/main/java/hello/cluebackend/domain/user/domain/UserEntity.java @@ -1,5 +1,6 @@ package hello.cluebackend.domain.user.domain; +import hello.cluebackend.domain.classroomuser.domain.ClassRoomUser; import hello.cluebackend.domain.user.presentation.dto.UserDTO; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -10,13 +11,15 @@ import org.springframework.data.annotation.CreatedDate; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; @Entity @Getter @Setter @AllArgsConstructor @NoArgsConstructor -@Table(name = "user") +@Table(name = "user_entity") public class UserEntity { @Id @@ -44,6 +47,15 @@ public class UserEntity { @Column(nullable = false) private LocalDateTime createdAt; + @PrePersist + protected void onCreate() { + createdAt = LocalDateTime.now(); + } + + @OneToMany(mappedBy = "userEntity", cascade = CascadeType.ALL) + private List classRoomUserList = new ArrayList<>(); + + public UserEntity(int studentId, String username, String addition, String email, Role role) { this.studentId = studentId; this.username = username; @@ -53,6 +65,6 @@ public UserEntity(int studentId, String username, String addition, String email, } public UserDTO toUserDTO() { - return new UserDTO(email, role, username, studentId, addition); + return new UserDTO(userId, email, role, username, studentId, addition); } } diff --git a/src/main/java/hello/cluebackend/domain/user/presentation/dto/CustomOAuth2User.java b/src/main/java/hello/cluebackend/domain/user/presentation/dto/CustomOAuth2User.java index 21f540c6..47783781 100644 --- a/src/main/java/hello/cluebackend/domain/user/presentation/dto/CustomOAuth2User.java +++ b/src/main/java/hello/cluebackend/domain/user/presentation/dto/CustomOAuth2User.java @@ -47,4 +47,12 @@ public String getName() { public String getUsername() { return userDTO.getUsername(); } + + public Long getUserId() { + return userDTO.getUserId(); + } + + public int getStudentId() { + return userDTO.getStudentId(); + } } diff --git a/src/main/java/hello/cluebackend/domain/user/presentation/dto/UserDTO.java b/src/main/java/hello/cluebackend/domain/user/presentation/dto/UserDTO.java index 278d9ee2..a888b80c 100644 --- a/src/main/java/hello/cluebackend/domain/user/presentation/dto/UserDTO.java +++ b/src/main/java/hello/cluebackend/domain/user/presentation/dto/UserDTO.java @@ -7,13 +7,15 @@ @Getter @Setter public class UserDTO { + private Long userId; private String email; private Role role; private String username; private int studentId; private String addition; - public UserDTO(String email, Role role, String username, int studentId, String addition) { + public UserDTO(Long userId, String email, Role role, String username, int studentId, String addition) { + this.userId = userId; this.email = email; this.role = role; this.username = username; diff --git a/src/main/java/hello/cluebackend/global/config/CustomSuccessHandler.java b/src/main/java/hello/cluebackend/global/config/CustomSuccessHandler.java index ddc1468d..b6cf1ff2 100644 --- a/src/main/java/hello/cluebackend/global/config/CustomSuccessHandler.java +++ b/src/main/java/hello/cluebackend/global/config/CustomSuccessHandler.java @@ -37,7 +37,6 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo int studentId = userDTO.getStudentId(); if (studentId == -1) { request.getSession().setAttribute("firstUser", userDTO); - System.out.println(userDTO.getUsername() + "님 회원가입 성공"); getRedirectStrategy().sendRedirect( request, response, @@ -45,26 +44,24 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo ); } else { String username = customUserDetails.getUsername(); + Long userId = customUserDetails.getUserId(); Collection authorities = authentication.getAuthorities(); Iterator iterator = authorities.iterator(); GrantedAuthority auth = iterator.next(); String role = auth.getAuthority(); - String access = jwtUtil.createJwt("access", username, role, 60*60*1000L); - String refresh = jwtUtil.createJwt("refresh", username, role,7 * 24 * 60 * 60 * 1000L); + String access = jwtUtil.createJwt("access", userId, username, role, 60*60*1000L); + String refresh = jwtUtil.createJwt("refresh", userId, username, role,7 * 24 * 60 * 60 * 1000L); refreshTokenService.saveRefreshToken(refresh, username); response.setHeader("Authorization", "Bearer " + access); response.addCookie(createCookie("refresh_token", refresh)); - System.out.println(username + "님 로그인 성공"); response.setStatus(HttpStatus.OK.value()); response.sendRedirect("http://localhost:3000/"); } } - - private Cookie createCookie(String key, String value) { Cookie cookie = new Cookie(key, value); cookie.setMaxAge(7 * 24 * 60 * 60); diff --git a/src/main/java/hello/cluebackend/global/config/JWTUtil.java b/src/main/java/hello/cluebackend/global/config/JWTUtil.java index 22353cb5..0f468e63 100644 --- a/src/main/java/hello/cluebackend/global/config/JWTUtil.java +++ b/src/main/java/hello/cluebackend/global/config/JWTUtil.java @@ -19,6 +19,10 @@ public JWTUtil(@Value("${spring.jwt.secret}")String secret) { secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), Jwts.SIG.HS256.key().build().getAlgorithm()); } + public Long getUserId(String token) { + return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().get("userId", Long.class); + } + public String getUsername(String token) { return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().get("username", String.class); } @@ -43,10 +47,13 @@ public String getCategory(String token) { return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload().get("category", String.class); } - public String createJwt(String category, String username, String role, Long expiredMs) { + + + public String createJwt(String category, Long userId, String username, String role, Long expiredMs) { return Jwts.builder() .claim("category", category) + .claim("userId", userId) .claim("username", username) .claim("role", role) .issuedAt(new Date(System.currentTimeMillis())) diff --git a/src/main/java/hello/cluebackend/global/security/jwt/RefreshTokenService.java b/src/main/java/hello/cluebackend/global/security/jwt/RefreshTokenService.java index 7dbea682..d0a4afa9 100644 --- a/src/main/java/hello/cluebackend/global/security/jwt/RefreshTokenService.java +++ b/src/main/java/hello/cluebackend/global/security/jwt/RefreshTokenService.java @@ -31,7 +31,7 @@ public boolean existsByRefresh(String refreshToken) { } public void reissueRefreshToken(HttpServletRequest request, HttpServletResponse response) throws AuthenticationCredentialsNotFoundException { - System.out.println("reissueRefreshToken"); +// System.out.println("reissueRefreshToken"); String refreshToken = null; Cookie[] cookies = request.getCookies(); if (cookies == null) { @@ -65,9 +65,11 @@ public void reissueRefreshToken(HttpServletRequest request, HttpServletResponse String username = jwtUtil.getUsername(refreshToken); String role = jwtUtil.getRole(refreshToken).name(); + Long userId = jwtUtil.getUserId(refreshToken); - String newAccessToken = jwtUtil.createJwt("access", username, role, 60 * 10 * 1000L); - String newRefreshToken = jwtUtil.createJwt("refresh", username, role,24 * 60 * 60 * 1000L); + + String newAccessToken = jwtUtil.createJwt("access", userId, username, role, 60 * 10 * 1000L); + String newRefreshToken = jwtUtil.createJwt("refresh", userId, username, role,24 * 60 * 60 * 1000L); saveRefreshToken(newRefreshToken, username); deleteByRefresh(refreshToken); diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 0597ab40..678601c4 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -2,7 +2,7 @@ spring: application: name: CLUE-Backend datasource: - url: jdbc:postgresql://postgresql:5432/clue + url: jdbc:postgresql://my_postgres:5432/clue username: clue1234 password: clue52025 driver-class-name: org.postgresql.Driver @@ -34,12 +34,6 @@ spring: provider: google: authorization-uri: https://accounts.google.com/o/oauth2/v2/auth?prompt=consent - - neis: - api: - host: open.neis.go.kr/hub - key: ${spring.neis.api.key} - server: address: 0.0.0.0 port: 8080 \ No newline at end of file