• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

/study/path 검증실패시 리다이렉트 처리에 대한 궁금증

22.10.17 22:50 작성 조회수 215

0

강의와 같은 뷰 템플릿에서 보내는 post요청입니다.

 

기존 Post /study/path 의 경우

   @PostMapping("/study/path")
    public String updateStudyPath(@CurrentUser Account account, @PathVariable String path, @RequestParam String newPath,
                                  Model model, RedirectAttributes attributes) {
        Study study = studyService.getStudyToUpdateStatus(account, path);

        if (!studyService.isValidPath(newPath)) {
            model.addAttribute(account);
            model.addAttribute(study);
            model.addAttribute("studyPathError", "해당 스터디 경로는 사용할 수 없습니다. 다른 값을 입력하세요.");
            return "study/settings/study";
        }

        studyService.updateStudyPath(study, newPath);
        attributes.addFlashAttribute("message", "스터디 경로를 수정했습니다");
        return "redirect:/study/" + getPath(newPath) + "/settings/study";

    }

 

같은 이름의 경로 요청시, 변경이 불가한 메세지를 나타내고, 페이지는 /study//settings/study를 나타내지만 주소창은 post요청의 영향으로 추가적으로 path가 더 붙는 것을 확인했습니다.

이상태에서 의도적일순 있으나 URL자체로 요청을 보내는 경우 /path가 붙은 Get요청을 하게되면 아래와 같은 화면을 보게 됩니다.

따라서 해당 문제점을 해결하기 위해 아래 처럼 변경하였습니다.

    @PostMapping("/study/path")
    public String updateStudyPath(@CurrentUser Account account, @PathVariable String path, @RequestParam String newPath,
                                  Model model, RedirectAttributes attributes) {
        Study study = studyService.getStudyToUpdateStatus(account, path);

        if (!studyService.isValidPath(newPath)) {
            model.addAttribute(account);
            model.addAttribute(study);
            attributes.addFlashAttribute("studyPathError", "해당 스터디 경로는 사용할 수 없습니다. 다른 값을 입력하세요.");
            return "redirect:/study/" + getPath(newPath) + "/settings/study";
        }

        studyService.updateStudyPath(study, newPath);
        attributes.addFlashAttribute("message", "스터디 경로를 수정했습니다");
        return "redirect:/study/" + getPath(newPath) + "/settings/study";

    }

기존의 model에 추가한 attribute는 리다이렉트 요청시 없어지기 때문에 메세지 출력에 필요한 studyPathError는 FlashAttirbute로 변경해주었습니다.

 

잘못된 경로변경 요청시 결과입니다.

redirect 결과로 /path가 붙어 나오지 않음

 

이 경험으로 위와 같은 상황에서는 Post요청으로 Redirect가 더 안전(?)하다고 생각했습니다.

하지만 매번 Redirect시킬수는 없을 것 같은데 존재하지 않는 API의 경우에 기준 URL로 리다이렉트 시키나요?

예를 들자면

/study/path/settings/study/path 의 경우에

/study/path/settings처럼

예상되는 url들의 경우 getMapping으로 다중처리하는 걸까요?

 

답변 0

답변을 작성해보세요.

답변을 기다리고 있는 질문이에요.
첫번째 답변을 남겨보세요!