inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링과 JPA 기반 웹 애플리케이션 개발

스터디 설정 - 경로 및 이름 수정

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

345

optional

작성한 질문수 3

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으로 다중처리하는 걸까요?

 

spring spring-boot java JPA thymeleaf

답변 0

Study 개설하는 로직에 대해서 궁금점이 있습니다.

0

72

1

앱 재시작 후 회원가입

0

121

1

app.host 관련 질문이 있습니다

0

106

1

강의 버전 정보

0

136

1

event, study 참조

0

238

2

비밀번호 변경 로직 질문있습니다.

0

145

1

프로필 수정 처리 merge 질문입니다.

0

117

1

회원가입 성공 후 redirect이동시 권한 질문

0

516

3

HtmlEmailService 개발하다 생긴 의문입니다

0

260

2

postgreSql 연결하여 JPA 를 통해 테이블 생성시 ZONE 테이블 생성에서 에러가 납니다

0

440

2

수업질문 [긴급] 로그인안되는 문제 말씀해주시는 부분 반영해서 최종 질문드립니다

0

272

2

[긴급-재업로드]수업질문 로그인 안 되는 문제

0

246

1

[긴급] 로그인해도 네비게이션 바가 안 바뀌고 있습니다!! 로그인이 안 됩니다 도와주세요

0

285

1

cropper 오류 문제로 질문드립니다..

0

295

2

authentication관련 질문...

0

504

2

모임참가 취소 할때 로직 질문

0

363

3

안녕하세요 기선님 질문이있습니다..

0

237

1

HTML코드 및 강의 중간자료들

0

689

3

springSecurity

0

532

2

버전 질문입니다.

0

311

1

부트스트랩, css

0

362

2

영속성 컨텍스트 질문

0

257

2

다시 강의를 보니 드는생각..

0

359

2

5:50에 나오는 HTML코드는 어디서 찾을 수 있나여?

0

320

1