-
Notifications
You must be signed in to change notification settings - Fork 0
Login main 오류 수정 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Login main 오류 수정 #10
Changes from all commits
4b7bc16
38594ae
62ba67b
a832c2d
82dcade
88ee598
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.example.redunm.exception; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
|
|
||
| @ControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
| public ResponseEntity<?> handleMethodNotSupported(HttpRequestMethodNotSupportedException ex) { | ||
| return ResponseEntity | ||
| .status(HttpStatus.METHOD_NOT_ALLOWED) | ||
| .body("지원되지 않는 HTTP 메서드입니다. " + ex.getMethod() + "이 엔드포인트에서 지원되지 않습니다."); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.example.redunm.login; | ||
|
|
||
| public class LoginRequest { | ||
| private String email; | ||
| private String password; | ||
|
|
||
| // Getters and Setters | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public void setEmail(String email) { | ||
| this.email = email; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public void setPassword(String password) { | ||
| this.password = password; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,32 @@ | ||
| package com.example.redunm.modellist; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import com.example.redunm.entity.User; | ||
| import jakarta.servlet.http.HttpSession; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Controller | ||
| @RestController | ||
| @RequestMapping("/api/models") | ||
| public class DataModelController { | ||
|
|
||
| @Autowired | ||
| private DataModelRepository DataModelRepository; | ||
| private DataModelRepository dataModelRepository; | ||
|
|
||
| @GetMapping("/models") | ||
| public String getAllModels(Model model) { | ||
| // MongoDB에서 모든 모델 데이터를 조회 | ||
| List<DataModel> models = DataModelRepository.findAll(); | ||
| model.addAttribute("models", models); // 데이터를 뷰로 전달 | ||
| return "modellist"; // modellist.html 렌더링 | ||
| @GetMapping | ||
| public ResponseEntity<?> getAllModels(HttpSession session) { | ||
| User loggedInUser = (User) session.getAttribute("loggedInUser"); | ||
| if (loggedInUser == null) { | ||
| // 로그인이 안돼있으면 401 오류 뜨게하기 | ||
| return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | ||
| .body("로그인이 필요합니다."); | ||
| } | ||
|
|
||
| // 로그인 된 경우 mongoDB에서 model 조회 | ||
| List<DataModel> models = dataModelRepository.findAll(); | ||
| return ResponseEntity.ok(models); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||||||||||||
| package com.example.redunm.modellist; | ||||||||||||||||
|
|
||||||||||||||||
| import com.example.redunm.modellist.DataModel; | ||||||||||||||||
| import org.springframework.stereotype.Service; | ||||||||||||||||
|
|
||||||||||||||||
| import java.util.*; | ||||||||||||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * 사용자별 장바구니(Cart)를 | ||||||||||||||||
| * 서버 메모리 내부의 Map에 저장해두는 임시 서비스 | ||||||||||||||||
| */ | ||||||||||||||||
| @Service | ||||||||||||||||
| public class ModelCartService { | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * 사용자별 장바구니 목록을 저장할 Map | ||||||||||||||||
| * key: 사용자 ID (문자열) | ||||||||||||||||
| * value: 해당 사용자가 장바구니에 담은 DataModel 리스트 | ||||||||||||||||
| * | ||||||||||||||||
| * - ConcurrentHashMap: 멀티쓰레드 환경에서 안전성 보장(간단 예시) | ||||||||||||||||
| * - 실무에서는 Redis나 DB에 저장하는 방식을 권장 | ||||||||||||||||
| */ | ||||||||||||||||
| private final Map<String, List<DataModel>> userCartMap = new ConcurrentHashMap<>(); | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * 특정 사용자(userId)의 장바구니 목록 조회 | ||||||||||||||||
| */ | ||||||||||||||||
| public List<DataModel> getCart(String userId) { | ||||||||||||||||
| return userCartMap.getOrDefault(userId, Collections.emptyList()); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Returning a mutable list -public List<DataModel> getCart(String userId) {
- return userCartMap.getOrDefault(userId, Collections.emptyList());
+public List<DataModel> getCart(String userId) {
+ List<DataModel> cart = userCartMap.getOrDefault(userId, Collections.emptyList());
+ return List.copyOf(cart); // returns an unmodifiable copy
}📝 Committable suggestion
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * 장바구니에 모델 추가 | ||||||||||||||||
| * @return 추가 후의 장바구니 전체 목록 | ||||||||||||||||
| */ | ||||||||||||||||
| public List<DataModel> addToCart(String userId, DataModel model) { | ||||||||||||||||
| // userId가 가진 장바구니 목록 가져오기 (없으면 새 리스트) | ||||||||||||||||
| List<DataModel> cartList = userCartMap.getOrDefault(userId, new ArrayList<>()); | ||||||||||||||||
| cartList.add(model); | ||||||||||||||||
|
|
||||||||||||||||
| // 갱신된 목록을 다시 put | ||||||||||||||||
| userCartMap.put(userId, cartList); | ||||||||||||||||
|
|
||||||||||||||||
| return cartList; | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+38
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Race condition risk in cart operations due to non-thread-safe ArrayList The verification confirms the concern. While the service uses Specific issues:
To fix this, consider:
🔗 Analysis chainConcurrent updates Let me check the implementation of ModelCartService to understand the concurrency handling better. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Searching for usage of `addToCart` in the codebase to confirm concurrency safety or synchronization approach
rg -A 5 "addToCart"
Length of output: 2071 Script: #!/bin/bash
# Check the ModelCartService class implementation for ConcurrentHashMap and any synchronization
rg -A 10 "class ModelCartService"
Length of output: 1094 |
||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * 장바구니 전체 삭제 (옵션) | ||||||||||||||||
| */ | ||||||||||||||||
| public void clearCart(String userId) { | ||||||||||||||||
| userCartMap.remove(userId); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * 장바구니에서 특정 모델 삭제 (옵션) | ||||||||||||||||
| */ | ||||||||||||||||
| public List<DataModel> removeModel(String userId, String modelId) { | ||||||||||||||||
| List<DataModel> cartList = userCartMap.get(userId); | ||||||||||||||||
| if (cartList == null) { | ||||||||||||||||
| return Collections.emptyList(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| cartList.removeIf(m -> m.getId().equals(modelId)); | ||||||||||||||||
| userCartMap.put(userId, cartList); | ||||||||||||||||
| return cartList; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security warning: Storing passwords in plain text
Consider securing passwords via hashing before storing or transmitting them.