학습 목표
- 폴더 구조 잡기
- UserController 생성
- UserDTO 생성
- UserService 생성
- UserRepository 생성
- UserEntity 생성
- scr/main/resources/user.xml 생성
user.xml
http://mybatis.org/dtd/mybatis-3-mapper.dtd>">
INSERT INTO users (username, email, password) VALUES (#{username}, #{email}, #{password})
SELECT userId, username, email
FROM users
WHERE email = #{email} AND password = #{password}
package com.tencoding.todo.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.tencoding.todo.dto.UserDTO;
import com.tencoding.todo.repository.entity.UserEntity;
import com.tencoding.todo.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
// 주소 설계
// 회원 가입 요청 -- form, HTTP Message body
@PostMapping("/sign-up")
public ResponseEntity<?> signUp(@RequestBody UserDTO userDTO) {
// 데이터 유효성 검사- 생략
int result = userService.signUp(userDTO); // 201 , 200, 404
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}
// 로그인 요청 --> 보안상 이유
@PostMapping("/sign-in")
public ResponseEntity<?> signin(@RequestBody UserDTO userDTO) {
UserEntity user = userService.signin(userDTO);
// 세션 처리 ---> JWT
if(user != null) {
return new ResponseEntity<>(user, HttpStatus.OK);
} else {
return new ResponseEntity<>("로그인 실패", HttpStatus.UNAUTHORIZED);
}
}
}
'SpringBoot' 카테고리의 다른 글
kakao 로그인 - 3 (0) | 2023.09.27 |
---|---|
CUGGI (쇼핑몰 프로젝트) (0) | 2023.09.26 |
kakao 로그인 - 2 (0) | 2023.09.26 |
kakao 로그인 - 1 (0) | 2023.09.26 |
BankApp - 암호화 처리 (0) | 2023.09.22 |