๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

[error] localhost์—์„œ ๋ฆฌ๋””๋ ‰์…˜ํ•œ ํšŸ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์Šต๋‹ˆ๋‹ค ๋ณธ๋ฌธ

Back_End/Issue

[error] localhost์—์„œ ๋ฆฌ๋””๋ ‰์…˜ํ•œ ํšŸ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์Šต๋‹ˆ๋‹ค

๊ธฐ๋Œ 2024. 4. 16. 15:48

๐Ÿ”’ ERROR  ๋ฌธ์ œ ์ƒํ™ฉ

 

 ๊ธฐ์กด์ฝ”๋“œ

    @GetMapping("/mypage")
    public String myPage(Model model , Principal principal) {

        if (principal == null || principal.getName() == null ) {
            return "redirect:/user/login ";
        } else {
            model.addAttribute("id",principal.getName());
            return "redirect:/user/mypage";
        }
    }

 

@PreAuthorize("isAuthenticated()") 


์• ๋…ธํ…Œ์ด์…˜์ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋Š” ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž๋งŒ์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์‚ฌ์šฉ์ž์˜ ID๋ฅผ ๋ชจ๋ธ์— ์ถ”๊ฐ€ํ•œ ํ›„์— "/user/mypage"๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ ํ•ญ์ƒ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์œ„ ์–ด๋…ธํ…Œ์ด์…˜์€ ์ธ์ฆ๋˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋ฉ”์„œ๋“œ ๋ณธ๋ฌธ์—์„œ๋Š” ์‚ฌ์šฉ์ž์˜ ID๋ฅผ ๋ชจ๋ธ์— ์ถ”๊ฐ€ํ•œ ํ›„์— "/user/mypage"๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ฅผ ํ•˜๋Š”๋ฐ
์ด๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ ํ•ญ์ƒ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ, ์‚ฌ์šฉ์ž๊ฐ€ "/mypage"์— ์ ‘๊ทผํ•  ๋•Œ๋งˆ๋‹ค ํ•ญ์ƒ ์ด๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ "/mypage"์— ์ ‘๊ทผํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋„ ๋ฃจํ”„๋กœ ์ธํ•ด ํ•ญ์ƒ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋˜๋Š” 
๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 @PreAuthorize("isAuthenticated()")
    @GetMapping("/mypage")
    public String myPage(Model model , Principal principal) {
    
            model.addAttribute("id",principal.getName());
            return "redirect:/user/mypage";
        }
    }

 

๐Ÿ“Œ ํ•ด๊ฒฐ

   @PreAuthorize("isAuthenticated()")
    @GetMapping("/mypage")
    public String myPage(Model model , Principal principal) {

        model.addAttribute("id",principal.getName());
        return "/user/mypage";
    }

 

 

728x90