[블로그] 일반적인 로그인 방식_


//SELECT * From user WHERE username = ? AND password = ?
	User findByUsernameAndPassword(String username,String password);
	
//@Query(value = "SELECT * FROM user WHERE username = ?1 AND password = ?2", nativeQuery = true)
//	User login(String username, String password);
}










	@Transactional(readOnly = true)
	public User 로그인(User user ) {
		return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());
	}
}















	
	//, HttpSession session 빈등록 가능
	@PostMapping("/api/user/login")
	public ResponseDto<Integer> login(@RequestBody User user) {
		System.out.println("login 호출");
		user.setRole(RoleType.USER);
		 User principal = userService.로그인(user);
		 
		 if(principal != null) {
			 session.setAttribute("principal", principal);
		 }
		return new ResponseDto<Integer>(HttpStatus.OK.value(),1); 
		// jackson을 통해 자동으로 json 으로 전달
	}
}

















<c:choose>
  
  	<c:when test = "${empty sessionScope.principal }">
  	
  	 <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="/loginForm">로그인</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/joinForm">회원가입</a>
      </li>
    </ul>
    
  	</c:when>
  	
  	<c:otherwise>
  	
  	<ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="/board/writeForm">글쓰기</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/user/userForm">회원정보</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/user/logout">로그아웃</a>
      </li>
    </ul>
    
  	</c:otherwise>
  
  </c:choose>
  

좋은 웹페이지 즐겨찾기