JSCODE Logo
프로그래밍 과외블로그후기멘토진
회사명 : JSCODE대표 : 박재성사업자 등록번호 : 244-22-01557통신판매업 : 제 2023-인천미추홀-0381 호
학원 명칭 : 제이에스코드(JSCODE)원격학원학원설립ㆍ운영 등록번호 : 제6063호

서울특별시 구로구 경인로 20가길 11(오류동, 아델리아)

Copyright ⓒ 2025 JSCODE - 최상위 현업 개발자들의 프로그래밍 교육 All rights reserved.

이용약관개인정보처리방침
← 블로그 목록으로 돌아가기

[실습] 사용자 서비스에 로그인 API 구현하기

JSCODE 박재성
JSCODE 박재성
2025-12-06
author
JSCODE 박재성
category
MSA
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 MSA 입문/실전
slug
practice-implement-login-api
type
post
updatedAt
Dec 6, 2025 05:47 AM

✅ 사용자 서비스에 로그인 API 구현하기

  1. DTO 구현하기
    1. dto/LoginRequestDto
      public class LoginRequestDto { private String email; private String password; public String getEmail() { return email; } public String getPassword() { return password; } }
       
      dto/LoginResponseDto
      public class LoginResponseDto { private String token; public LoginResponseDto(String token) { this.token = token; } public String getToken() { return token; } }
       
  1. Controller 로직 구현하기
    1. controller/UserController
      @RestController @RequestMapping("/api/users") public class UserController { ... @PostMapping("login") public ResponseEntity<LoginResponseDto> login( @RequestBody LoginRequestDto loginRequestDto ) { LoginResponseDto loginResponseDto = userService.login(loginRequestDto); return ResponseEntity.ok(loginResponseDto); } }
       
  1. Service 로직 구현하기
    1. service/UserService
      @Service public class UserService { ... public LoginResponseDto login(LoginRequestDto loginRequestDto) { User user = userRepository.findByEmail(loginRequestDto.getEmail()) .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); if (!user.getPassword().equals(loginRequestDto.getPassword())) { throw new IllegalArgumentException("비밀번호가 일치하지 않습니다."); } // (JWT 로직) String token = null; return new LoginResponseDto( token ); } }
       
      domain/UserRepository
      public interface UserRepository extends JpaRepository<User, Long> { Optional<User> findByEmail(String email); }
       
  1. JWT 의존성 추가하기
    1. build.gradle
      ... dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.kafka:spring-kafka' implementation 'io.jsonwebtoken:jjwt-api:0.13.0' runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.13.0' runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.13.0' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.mysql:mysql-connector-j' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } ...
      notion image
       
  1. JWT 로직 추가하기
    1. application.yaml
      ... jwt: secret: jscode-secret-1234-1234-1234-1234
       
      service/UserService
      @Service public class UserService { private final UserRepository userRepository; private final PointClient pointClient; private final KafkaTemplate<String, String> kafkaTemplate; private final String jwtSecret; public UserService( UserRepository userRepository, PointClient pointClient, KafkaTemplate<String, String> kafkaTemplate, @Value("${jwt.secret}") String jwtSecret ) { this.userRepository = userRepository; this.pointClient = pointClient; this.kafkaTemplate = kafkaTemplate; this.jwtSecret = jwtSecret; } ... public LoginResponseDto login(LoginRequestDto loginRequestDto) { User user = userRepository.findByEmail(loginRequestDto.getEmail()) .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); if (!user.getPassword().equals(loginRequestDto.getPassword())) { throw new IllegalArgumentException("비밀번호가 일치하지 않습니다."); } // JWT를 만들 때 사용하는 Key 생성 (공식 문서 방식) SecretKey secretKey = Keys.hmacShaKeyFor( jwtSecret.getBytes(StandardCharsets.UTF_8) ); // JWT 토큰 만들기 String token = Jwts.builder() .subject(user.getUserId().toString()) .signWith(secretKey) .compact(); return new LoginResponseDto( token ); } }
      • 참고) 간단한 로직 구조를 위해 Spring Security를 사용하지 않았고, 디테일한 JWT 설정도 하지 않았고, 별도의 클래스로 분리하지도 않았다.
       
  1. 서버 다시 실행시키기
    1.  
  1. API 테스트 해보기
    1. notion image
       
      [API Gateway 주소로도 통신해보기]
      notion image
author
JSCODE 박재성
category
MSA
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 MSA 입문/실전
slug
type
series-footer
updatedAt
Dec 6, 2025 05:45 AM
📎
이 글은 비전공자도 이해할 수 있는 MSA 입문/실전 강의의 수업 자료 중 일부입니다.