s00jin 님의 블로그

3-1. [로그인/에러] 쿠키 로그인 시 500 에러 (whitelabel error page / 500) 본문

프로젝트/하고 싶은거 다해보는 내 사이트

3-1. [로그인/에러] 쿠키 로그인 시 500 에러 (whitelabel error page / 500)

s00jin 2025. 7. 1. 21:02

첫번째 오류

cookie-login 접속 시 에러가 발생한다.

2025-04-02T10:18:04.023+09:00 ERROR 10505 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null] with root cause

 

null 값으로 인해 발생한 오류인 것 같다!!


 

수정 전 코드

// 쿠키 로그인 페이지
    @GetMapping(value = {"", "/"})
    // @CookieValue 어노테이션을 통해 쿠키값 받아오기 가능
    public String home(@CookieValue(name = "userId", required = false) Long userId, Model model){
        model.addAttribute("loginType", "cookie-login");
        model.addAttribute("pageName", "쿠키 로그인");

        // 로그인 구현 전 임시 추가
        //model.addAttribute("nickname", "kk");

        User loginUser = userService.getLoginUser(userId);

        if(loginUser != null) {
            model.addAttribute("nickname", loginUser.getNickname());
        }

        return "home";
    }

 


수정 후 코드

 @GetMapping(value = {"", "/"})
    // @CookieValue 어노테이션을 통해 쿠키값 받아오기 가능
    public String home(@CookieValue(name = "userId", required = false) Long userId, Model model){
        model.addAttribute("loginType", "cookie-login");
        model.addAttribute("pageName", "쿠키 로그인");

        // 로그인 구현 전 임시 추가
        //model.addAttribute("nickname", "kk");
        if (userId != null) {
            User loginUser = userService.getLoginUser(userId);
            model.addAttribute("nickname", loginUser.getNickname());

        }

 


수정된 코드를 실행하면 화면이 정상적으로 보인다.

 


두번째 오류

 

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'loginRequest' available as request attribut
2025-04-02T16:37:08.458+09:00 ERROR 11576 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringInputGeneralFieldTagProcessor' (template: "login" - line 14, col 26)] with root cause

 

이번 오류는 단순 오타로 인한 문제다...ㅎㅎ


 

model.addAttribute("loginResult", new LoginRequest());

 

 

위 코드를 아래 코드로 수정해주면 된다..ㅎ

 

model.addAttribute("loginRequest", new LoginRequest());

 

 



세번째 오류

2025-04-02T17:30:25.085+09:00 ERROR 12121 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: User not found] with root cause

 


수정 전 코드

@Getter
@NoArgsConstructor
public class LoginRequest {
    private String loginId;
    private String password;
}

 

수정 후 코드

@Getter
@Setter
@NoArgsConstructor
public class LoginRequest {
    private String loginId;
    private String password;
}

 

더보기

왜 setter 애너테이션이 있어야 돌아갈까?

🔍 문제 원인 상세 분석 (GPT의 답변)
Spring에서 @ModelAttribute는 요청 데이터를 객체로 변환할 때 리플렉션(Reflection)과 Setter 메서드를 사용합니다.
하지만 LoginRequest 클래스에 Setter 메서드가 없었기 때문에 필드 값을 채울 수 없었습니다.
이로 인해 LoginRequest 객체가 null 값이거나, loginId와 password가 null 상태로 유지되었고,
userRepository.findByLoginId(request.getLoginId())에서 null 값을 조회하려 하다 보니 "User not found" 예외가 발생한 것입니다.