Skip to content

๐ŸŽž๐Ÿ“ ์˜ํ™” ์ •๋ณด์™€ ๋ฆฌ๋ทฐ์— ๊ด€ํ•œ ์›นํŽ˜์ด์ง€(Back-end)

Notifications You must be signed in to change notification settings

movielog/movielog-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

MovieLog Build Status

์•ˆ๋…•ํ•˜์„ธ์š”. MovieLog์˜ Back-endํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค!

  • ์˜ํ™”์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Spring Security์™€ JWT๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆํ•˜๊ณ  ์‹๋ณ„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋กœ๊ทธ์ธ ํ•œ ์‚ฌ์šฉ์ž๋Š” ์˜ํ™”์— ๊ด€ํ•œ ๋ฆฌ๋ทฐ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์œผ๋ฉด ์ž‘์„ฑ๋œ ๋ฆฌ๋ทฐ๋งŒ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฉ”์ธ ํŽ˜์ด์ง€

์‹œ์Šคํ…œ ๊ตฌ์„ฑ

ERD

๊ธฐ์ˆ  ์Šคํƒ

  • Server
    • Java
    • SpringBoot
    • JPA
    • Spring Data JPA
    • JWT
    • Amazon EC2
    • Junit5
  • Database
    • H2(local)
    • Maria DB
    • Amazon RDS
  • CI
    • Travis CI
    • CodeDeploy
    • Amazon S3

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

  • ํ”„๋กœ์ ํŠธ๋Š” ํ”„๋ก ํŠธ์—”๋“œ 1์ธ, ๋ฐฑ์—”๋“œ 1์ธ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ํ”„๋ก ํŠธ์—”๋“œ๋Š” react๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ํ”„๋ก ํŠธ์—”ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐฑ์—”๋“œ ํ”„๋กœ์ ํŠธ ์•ˆ์— ํฌํ•จํ•ด ๋ฐฐํฌํ•˜์˜€๊ณ , ์•„๋ž˜ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ”„๋ก ํŠธ์—”๋“œ ์ €์žฅ์†Œ readme์—์„œ๋Š” ๊ตฌํ˜„ ๋™์ž‘์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Backend ํŒจํ‚ค์ง€ ๊ตฌ์กฐ ํ™•์ธ
โญ๏ธmovielog-serverโญ๏ธ
โ””โ”€โ”€ src
    โ”œโ”€โ”€ main
    โ”‚ย ย  โ”œโ”€โ”€ java
    โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ com
    โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ web
    โ”‚ย ย  โ”‚ย ย          โ””โ”€โ”€ movielog
    โ”‚ย ย  โ”‚ย ย              โ”œโ”€โ”€ configure
    โ”‚ย ย  โ”‚ย ย              โ”œโ”€โ”€ controller
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ movie
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ order
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ profile
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ review
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ””โ”€โ”€ user
    โ”‚ย ย  โ”‚ย ย              โ”œโ”€โ”€ model
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ movie
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ order
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ review
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ””โ”€โ”€ user
    โ”‚ย ย  โ”‚ย ย              โ”œโ”€โ”€ repository
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ movie
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ order
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ”œโ”€โ”€ review
    โ”‚ย ย  โ”‚ย ย              โ”‚ย ย  โ””โ”€โ”€ user
    โ”‚ย ย  โ”‚ย ย              โ”œโ”€โ”€ security
    โ”‚ย ย  โ”‚ย ย              โ””โ”€โ”€ service
    โ”‚ย ย  โ”œโ”€โ”€ โญ๏ธmovielog-clientโญ๏ธ
    โ”‚ย ย  โ””โ”€โ”€ resources
    โ””โ”€โ”€ test
        โ”œโ”€โ”€ java
        โ”‚ย ย  โ””โ”€โ”€ com
        โ”‚ย ย      โ””โ”€โ”€ example
        โ”‚ย ย          โ””โ”€โ”€ movielog
        โ”‚ย ย              โ”œโ”€โ”€ controller
        โ”‚ย ย              โ”œโ”€โ”€ repository
        โ”‚ย ย              โ””โ”€โ”€ service
        โ””โ”€โ”€ resources
Frontend ํŒจํ‚ค์ง€ ๊ตฌ์กฐ ํ™•์ธ
โญ๏ธmovielog-clientโญ๏ธ
โ””โ”€โ”€ src
    โ”œโ”€โ”€ api
    โ”œโ”€โ”€ components
    โ”‚ย ย  โ”œโ”€โ”€ Header
    โ”‚ย ย  โ”œโ”€โ”€ Join
    โ”‚ย ย  โ”œโ”€โ”€ Login
    โ”‚ย ย  โ”œโ”€โ”€ Main
    โ”‚ย ย  โ”œโ”€โ”€ MovieDetail
    โ”‚ย ย  โ”œโ”€โ”€ MovieOrder
    โ”‚ย ย  โ”œโ”€โ”€ My
    โ”‚ย ย  โ”œโ”€โ”€ MyOrder
    โ”‚ย ย  โ”œโ”€โ”€ MyReview
    โ”‚ย ย  โ”œโ”€โ”€ OrderInfo
    โ”‚ย ย  โ”œโ”€โ”€ Profile
    โ”‚ย ย  โ”œโ”€โ”€ ReviewBoard
    โ”‚ย ย  โ”œโ”€โ”€ ReviewEdit
    โ”‚ย ย  โ””โ”€โ”€ ReviewWrite
    โ””โ”€โ”€ hooks

์ž‘์—… ๋‚ด์šฉ

  • MOVIE API
    • ๋ฉ”์ธ ํ™”๋ฉด (์ „์ฒด ์กฐํšŒ)
      • GET /movie
    • ์˜ํ™” ์ƒ์„ธ (๊ฐœ๋ณ„ ์กฐํšŒ)
      • GET /movie/{movieId}
  • USER API
  • REVIEW API
    • ๋ฆฌ๋ทฐ ์ž‘์„ฑ
      • GET /review/write/{movieId} (front - ์ž‘์„ฑ ํŽ˜์ด์ง€)
      • POST /review/wirte/{movieId}
    • ์ „์ฒด ๋ฆฌ๋ทฐ ๋ชฉ๋ก ์กฐํšŒ
      • GET /review
    • ๋‚˜์˜ ๋ฆฌ๋ทฐ ๋ชฉ๋ก ์กฐํšŒ (USER ROLE ํ•„์š” ํŽ˜์ด์ง€)
  • ORDERS API
    • ์˜ํ™” ์ฃผ๋ฌธ
      • GET /order/{movieId} (front - ๊ตฌ๋งค ํŽ˜์ด์ง€)
      • POST /order/{movieId}
    • ๋‚˜์˜ ์ฃผ๋ฌธ ๋ชฉ๋ก ์ „์ฒด ์กฐํšŒ (USER ROLE ํ•„์š” ํŽ˜์ด์ง€)
    • ๋‚˜์˜ ์ฃผ๋ฌธ ๋ชฉ๋ก ๊ฐœ๋ณ„ ์กฐํšŒ (USER ROLE ํ•„์š” ํŽ˜์ด์ง€)
      • GET /my/order/{orderId}
  • AWS EC2, RDS์™€ ์—ฐ๋™ํ•˜์—ฌ ๋ฐฐํฌ ์ž‘์—… ์ง„ํ–‰
  • ๋„๋ฉ”์ธ ์—ฐ๊ฒฐ
  • HTTPS ์ ์šฉ

์ฃผ์š” ๊ฐœ๋ฐœ ๋‚ด์šฉ

Spring Security์™€ JWT

Spring Security

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.
Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements. ์ฐธ๊ณ  ํŽ˜์ด์ง€

  • Spring ๊ธฐ๋ฐ˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ‘œ์ค€ ๋ณด์•ˆ ํ”„๋ ˆ์ž„์›Œํฌ
  • ์‚ฌ์šฉ์ž ์ธ์ฆ, ์ธ๊ฐ€ ๋ฐ ๋ณด์•ˆ์„ ์ œ๊ณต
  • ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์นœ ํ›„์— ์ธ๊ฐ€ ์ ˆ์ฐจ๋ฅผ ์ง„ํ–‰ํ•จ
    • ์ธ์ฆ(Authentication) : ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€, ํ•ด๋‹น ์‚ฌ์šฉ์ž๊ฐ€ ๋ณธ์ธ์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ
    • ์ธ๊ฐ€(Authorization) : ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•œ ์ž์›์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์ ˆ์ฐจ

ํ”„๋กœ์ ํŠธ์— Spring Security๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์˜์กด์„ฑ ์ถ”๊ฐ€

implementation 'org.springframework.boot:spring-boot-starter-security'

JWT(Json Web Token) : Json ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์•ˆ์ „ํ•˜๊ฒŒ ์ •๋ณด๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋Š” ์›น ํ‘œ์ค€

ํ† ํฐ ๊ธฐ๋ฐ˜์˜ ์ธ์ฆ ์‹œ์Šคํ…œ์—์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์ธ์ฆ ๋ฐฉ์‹ ์ค‘ ํ•˜๋‚˜

JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties. ์ฐธ๊ณ  ํŽ˜์ด์ง€

JWT ๊ตฌ์กฐ

  • . ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š” ์„ธ ๊ฐ€์ง€ ๋ฌธ์ž์—ด์˜ ์กฐํ•ฉ
  • Header, Payload, Signature๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Œ
    • Header : ํ† ํฐ์˜ ํƒ€์ž…, ์•”ํ˜ธ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ตฌ์„ฑ
    • Payload : ์‹ค์ œ๋กœ ํšŒ์›์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด(Claim) ํฌํ•จ (๋ฏผ๊ฐ ์ •๋ณด[ex. ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ]๋Š” ํฌํ•จํ•˜๋ฉด ์•ˆ ๋จ)
    • Signature : Header์—์„œ ์ •์˜ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฐฉ์‹ ํ™œ์šฉ, Secret Key๋ฅผ ํฌํ•จํ•˜์—ฌ ์•”ํ˜ธํ™”
image

์‚ฌ์šฉ์ž ์‹๋ณ„ ๊ณผ์ •

  1. ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ๋˜๋ฉด ์„œ๋ฒ„์—์„œ๋Š” JWT๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌ
  2. ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” ๊ทธ JWT๋ฅผ ๋ฐ›์•„์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€, ์„œ๋ฒ„์— API ์š”์ฒญ ์‹œ JWT๋ฅผ HTTP Header์— ๋‹ด์•„์„œ ๋ณด๋ƒ„
  3. ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ „๋‹ฌํ•œ JWT๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์‹๋ณ„

์ด ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ JWT๊ฐ€ ์ƒ์„ฑ

ex) eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJoaUBnbWFpbC5jb20iLCJyb2xlcyI6W10sImlhdCI6MTY2NDI1Njc4OSwiZXhwIjoxNjY0MjU4NTg5fQ.ySIGyzKAJscUnYRtbAepRUotaohWtNpl2ja-REYMOmI

  • JWT๋Š” Statelessํ•˜๊ฒŒ ์„œ๋ฒ„์—์„œ ์‚ฌ์šฉ์ž๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•จ
  • ๋˜ํ•œ JWT๋Š” ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„์˜ ๋ถ€๋‹ด์„ ๋œ ์ˆ˜ ์žˆ์Œ

์‹ค์ œ ๊ฐœ๋ฐœ ๊ณผ์ •

JWT ์˜์กด์„ฑ ์ถ”๊ฐ€ ์„ค์ •

implementation 'io.jsonwebtoken:jjwt:0.9.1'

๋กœ๊ทธ์ธ API

์„œ๋ฒ„์— ๋กœ๊ทธ์ธํ•˜์—ฌ JWT๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •

  • ๋กœ๊ทธ์ธ ์š”์ฒญ์ด ์˜ค๋ฉด Controller๋ฅผ ํ†ตํ•ด UserService.java์˜ login ์‹คํ–‰

    // Userservice.java์—์„œ JWT ๋ฆฌํ„ด
    return jwtTokenProvider.createToken(user.getUsername(), user.getRoles());
  • ํด๋ผ์ด์–ธํŠธ์— ์ž…๋ ฅํ•œ id & password๊ฐ€ ์ผ์น˜ํ•˜๋ฉด JWT ์ƒ์„ฑ

    public String createToken(String userPk, List<String> roles) {
      Claims claims = Jwts.claims().setSubject(userPk); // Payload์— ์ €์žฅ๋˜๋Š” ์ •๋ณด ๋‹จ์œ„
      claims.put("roles", roles);
      Date now = new Date(); 
      
      return Jwts.builder()
              .setClaims(claims) 
              .setIssuedAt(now) // ํ† ํฐ ๋ฐœํ–‰ ์‹œ๊ฐ„ ์„ค์ •
              .setExpiration(new Date(now.getTime() + tokenValidTime)) // ํ† ํฐ ๋งŒ๋ฃŒ ์‹œ๊ฐ„ ์„ค์ •
              .signWith(SignatureAlgorithm.HS256, secretKey) // Signature์— ์‚ฌ์šฉํ•  ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜, secret ๊ฐ’ ์„ค์ •
              .compact();
    }
  • Postman์œผ๋กœ ํ…Œ์ŠคํŠธ

    • POST: /login ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ํ† ํฐ์„ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•˜๋ฉฐ
    • ์ด ํ† ํฐ์„ ํ”„๋ก ํŠธ์—”๋“œ์— ์ €์žฅํ•˜๊ฒŒ ๋จ
      image

๋‹‰๋„ค์ž„ ์ˆ˜์ • API

API ์š”์ฒญ ์‹œ ํ”„๋ก ํŠธ์—”๋“œ์—์„œ๋Š” Header์— JWT๋ฅผ ๋‹ด์•„ ๋ณด๋ƒ„

  • ๋‹‰๋„ค์ž„ ์ˆ˜์ •๊ณผ ๊ฐ™์€ ๊ถŒํ•œ์ด ํ•„์š”ํ•œ ์š”์ฒญ์ด ์˜ฌ ๋•Œ๋งˆ๋‹ค, ์„œ๋ฒ„๋Š” Header์˜ X-AUTH-TOKEN์— ๋‹ด๊ธด JWT์„ ๊ฐ€์ ธ์˜ด
    public String resolveToken(HttpServletRequest request) {
      return request.getHeader("X-AUTH-TOKEN");
    }
  • ๊ฐ€์ ธ์˜จ JWT์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ์ถ”์ถœ
    public String getUserPk(String token) {
      return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody().getSubject();
    }
  • JWT์—์„œ ์ถ”์ถœํ•œ ์‚ฌ์šฉ์ž ์ •๋ณด(email)๋ฅผ loadUserByUsername(username) ํ•จ์ˆ˜์˜ findByEmail์„ ํ†ตํ•ด ์‹ค์ œ DB์—์„œ ์œ ์ € ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ด
    • username : ์ด ํ”„๋กœ์ ํŠธ์—์„œ๋Š” email์— ํ•ด๋‹น
  • DB์—์„œ ๊ฐ€์ ธ์˜จ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ UserDetails ๊ฐ์ฒด์— ๋‹ด์•„ return ํ•จ
    • ์ด๋•Œ ์‚ฌ์šฉ์ž ์ •๋ณด์—์„œ USER ROLE ๊ถŒํ•œ์„ ํ™•์ธํ•˜์—ฌ ๋ฆฌ์†Œ์Šค ์ œ๊ณต
  • Postman์œผ๋กœ ํ…Œ์ŠคํŠธ
    • POST: /user/me ์š”์ฒญ ์‹œ Header์˜ X-AUTH-TOKEN์— (๋กœ๊ทธ์ธ ์‹œ ์ƒ์„ฑํ•œ) JWT๋ฅผ ํ•จ๊ป˜ ๋ณด๋ƒ„
      • Body์— ์ˆ˜์ •ํ•  ๋‹‰๋„ค์ž„ ์„ค์ •
    • JWT์„ ํ†ตํ•ด USER ROLE์„ ํ™•์ธํ•˜์—ฌ ๋‹‰๋„ค์ž„ ์ˆ˜์ • ์„ฑ๊ณต
      • nickname : user1 -> pizza ์ˆ˜์ •
      image image

์„œ๋น„์Šค ์ ‘๊ทผ ๊ถŒํ•œ ๊ด€๋ฆฌ

ํ•„์š”ํ•œ ๊ณณ์—๋งŒ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜์—ฌ ๋ณด์•ˆ์„ ์ ์šฉ

  • URL ๋ณ„ ๊ถŒํ•œ ์„ค์ • ๊ฐ€๋Šฅ
  • WebSecurityConfigure.java์˜ configure()์—์„œ ์„ค์ •
    • hasRole("USER")๋ฅผ ํ†ตํ•ด USER, MY ์„œ๋น„์Šค์— USER ROLE์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž๋งŒ ์ ‘๊ทผ ํ—ˆ์šฉ
      • USER, MY ์„œ๋น„์Šค : ๋‹‰๋„ค์ž„ ์ˆ˜์ •, ํšŒ์› ํƒˆํ‡ด, ๋‚˜์˜ ๋ฆฌ๋ทฐ ๋ชฉ๋ก, ๋‚˜์˜ ์ฃผ๋ฌธ ๋ชฉ๋ก
    • permitAll()์„ ํ†ตํ•ด ์ด์™ธ์˜ ์„œ๋น„์Šค๋Š” USER๊ฐ€ ์•„๋‹ˆ์–ด๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ
      @Override
      protected void configure(HttpSecurity httpSecurity) throws Exception{
        httpSecurity  
                ...
                .and()
                .authorizeRequests()
                .antMatchers("/api/user/**").hasRole("USER")
                .antMatchers("/api/my/**").hasRole("USER")
                .antMatchers("/api/**").permitAll()
                .antMatchers("/profile").permitAll()
                .and()
                ...
      }

Spring Data JPA

Spring Data JPA๋Š” ์Šคํ”„๋ง์—์„œ JPA๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›

๊ธฐ๋ณธ CRUD ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ œ๊ณตํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต ๊ฐœ๋ฐœ ์‹œ ๋ฐ˜๋ณตํ•ด ์ž‘์„ฑํ•  ํ•„์š” ์—†์Œ

  • CRUD ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค(JpaRepository ์ƒ์†) ์ œ๊ณต
  • ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋™์ ์œผ๋กœ ๊ตฌํ˜„์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ฃผ์ž…ํ•ด์คŒ
  • ๋”ฐ๋ผ์„œ ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์ž‘์„ฑํ•ด๋„ ๊ฐœ๋ฐœ์„ ์™„๋ฃŒํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›

๊ณตํ†ต ๋ฉ”์†Œ๋“œ ์—๋Š” count, delete, deleteAll, findById.. ๋“ฑ์ด ์žˆ์œผ๋ฉฐ, ๊ณตํ†ต ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹ˆ์–ด๋„ ๋„ค์ด๋ฐ ๋ฃฐ์„ ์ด์šฉํ•˜์—ฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฐ€๋Šฅ

JpaRepository ์ƒ์†

  • CRUD ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค
  • ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ•ด๋‹น ์—”ํ‹ฐํ‹ฐ์— ๋Œ€ํ•ด CRUD๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • ์‹ค์ œ ๊ตฌํ˜„์ฒด๋Š” SimpleJpaRepository์ด๋ฉฐ ์ด ํด๋ž˜์Šค๊ฐ€ ๊ตฌํ˜„
public interface MovieRepository extends JpaRepository<Movie, Long> {

}

๋ฉ”์†Œ๋“œ ์ด๋ฆ„์œผ๋กœ ์ฟผ๋ฆฌ ์‹คํ–‰

  • JPA๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ฟผ๋ฆฌ ๋ฉ”์†Œ๋“œ ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜
  • ๋ฉ”์†Œ๋“œ์˜ ์ด๋ฆ„์„ ์„ค์ •ํ•˜์—ฌ ํ•„์š”ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ
  • ์ธํ„ฐํŽ˜์ด์Šค์— ๋ฉ”์†Œ๋“œ ์„ ์–ธ๋งŒ ํ•˜๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

findByEmail

  • ์ ์šฉ1)
    • ์ด๋ฉ”์ผ(email)๋กœ ํšŒ์›(user)์„ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด UserRepository์— ๋ณ„๋„์˜ ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ
      • ์•ž์„œ ์–ธ๊ธ‰ํ•œ loadUserByUsername(username)ํ•จ์ˆ˜์—์„œ username์œผ๋กœ ๊ณ„์ • ์ •๋ณด ์กฐํšŒ ์‹œ ์‚ฌ์šฉ
      public interface UserRepository extends JpaRepository<User, Long> {
        Optional<User> findByEmail(String email);
      }
    • query = select u from User u where u.email = ?1

findAllByUser (review)

  • ์ ์šฉ2) [MY - ๋‚ด ๋ฆฌ๋ทฐ]์˜ ์œ ์ €๋ณ„ ๋ฆฌ๋ทฐ ๋ชฉ๋ก
    • ํŠน์ • ์œ ์ €(user)๊ฐ€ ์ž‘์„ฑํ•œ ๋ฆฌ๋ทฐ(review)๋ฅผ ์กฐํšŒํ•˜๊ธฐ ReviewRepository์— ๋ณ„๋„์˜ ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ
      public interface ReviewRepository extends JpaRepository<Review, Long> {
        List<Review> findAllByUser(User user);
      }
    • query = select r from Review r where r.user = :user

findAllByUser (order)

  • ์ ์šฉ3) [MY - ๊ตฌ๋งค๋‚ด์—ญ]์˜ ์œ ์ €๋ณ„ ์ฃผ๋ฌธ ๋ชฉ๋ก

    • ํŠน์ • ์œ ์ €(user)๊ฐ€ ์ƒ์„ฑํ•œ ์ฃผ๋ฌธ(order)๋ฅผ ์กฐํšŒํ•˜๊ธฐ OrderRepository์— ๋ณ„๋„์˜ ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ
      public interface OrderRepository extends JpaRepository<Order, Long> {
        List<Order> findAllByUser(User user);
      }
    • query = select o from Orders o where o.user = :order
  • ์ฐธ๊ณ  : Spring Data JPA ๊ณต์‹ ๋ฌธ์„œ

EC2 ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ ํ˜„์ƒ

  • ๋ฌธ์ œ ํ™•์ธ
    • ๋ฐฐํฌ ์‹œ๋„ : ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•ด EC2์—์„œ -jar ๋ช…๋ น์–ด ์‹คํ–‰ ์‹œ ๊ฐ‘์ž๊ธฐ ์ •์ง€ํ•จ
    • ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์ด ๋Š๊ธฐ๊ณ  ์•„๋ฌด๊ฒƒ๋„ ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๋‹ค์‹œ ๋นŒ๋“œ ํ•ด๋„ ๊ฐ™์€ ํ˜„์ƒ ๋ฐœ์ƒ
    • AWS EC2 ์ธ์Šคํ„ด์Šค ๋ชจ๋‹ˆํ„ฐ๋ง(Cloud watch) ํ™•์ธํ•˜๋‹ˆ CPU ์‚ฌ์šฉ๋ฅ ์ด ๊ธ‰์ƒ์Šนํ•˜๋‹ค๊ฐ€ ํ•˜๋ฝํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธ
      image
  • ์›์ธ
    • ์ด ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” t2.micro(ํ”„๋ฆฌํ‹ฐ์–ด)๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ 1GB
    • ๋นŒ๋“œ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ด๋Ÿฌํ•œ ํ˜„์ƒ์ด ๋‚˜ํƒ€๋‚จ
  • ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•
    • ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ์ธ์Šคํ„ด์Šค ์—…๊ทธ๋ ˆ์ด๋“œ์ด์ง€๋งŒ(๋น„์šฉ ๋ถ€๊ณผ) Swap ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์ฐพ์Œ

    • ์Šค์™€ํ•‘(Swapping) : ๋ฆฌ๋ˆ…์Šค๋Š” ํ•˜๋“œ ๋””์Šคํฌ๋ฅผ ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ๋กœ ์ „ํ™˜ํ•ด ์‚ฌ์šฉ

      • ์Šค์™‘ ๊ณต๊ฐ„ ์ฟ ๊ธฐ ๊ณ„์‚ฐ
        • ๋ฌผ๋ฆฌ์  RAM ํฌ๊ธฐ๊ฐ€ 2GB ์ดํ•˜์ธ ๊ฒฝ์šฐ, ๊ถŒ์žฅ ์Šค์™‘ ๋ฉ”๋ชจ๋ฆฌ๋Š” RAM ์šฉ๋Ÿ‰์˜ 2๋ฐฐ
        • ์ด ํ”„๋กœ์ ํŠธ์—์„œ๋Š” 1GB*2 = ์•ฝ 2GB๋ฅผ ์Šค์™‘ํ•  ์ˆ˜ ์žˆ์Œ
      • ์ˆœ์„œ
        1. ๋ฃจํŠธ ํŒŒ์ผ ์‹œ์Šคํ…œ์— ์Šค์™‘ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค sudo dd if=/dev/zero of=/mnt/swapfile bs=128M count=16
          1. 128M * 16๊ฐœ = ์•ฝ 2GB
        2. ์Šค์™‘ ํŒŒ์ผ์˜ ์ฝ๊ธฐ, ์“ฐ๊ธฐ ๊ถŒํ•œ ์—…๋ฐ์ดํŠธ sudo chmod 600 /swapfile
        3. Linux ์Šค์™‘ ์˜์—ญ ์„ค์ • sudo mkswap /swapfile
        4. ์Šค์™‘ ๊ณต๊ฐ„์— ์Šค์™‘ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์Šค์™‘ ํŒŒ์ผ ์ฆ‰์‹œ ์‚ฌ์šฉ sudo swap /swapfile
        5. ์„ฑ๊ณตํ–ˆ๋Š”์ง€ ํ™•์ธ sudo swapon -s
        6. /etc/fstab ํŒŒ์ผ์„ ํŽธ์ง‘ํ•˜์—ฌ ๋ถ€ํŒ… ์‹œ ์Šค์™‘ ํŒŒ์ผ ์‹œ์ž‘ sudo vi /etc/fstab
          1. ํŒŒ์ผ ๋์— /swapfile swap swap defaults 0 0 ์ถ”๊ฐ€
          2. ์ €์žฅ ํ›„ ์ข…๋ฃŒ
        7. free๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ ํ™•์ธ
    • swap ๊ณต๊ฐ„ ํ™•์žฅ! ๋ฉ”๋ชจ๋ฆฌ ์•ฝ 3GB ์‚ฌ์šฉ ๊ฐ€๋Šฅ

      image
  • ์ฐธ๊ณ  : AWS ํ™ˆํŽ˜์ด์ง€

๋„๋ฉ”์ธ ์—ฐ๊ฒฐ

  1. GoDaddy์—์„œ ๋„๋ฉ”์ธ ๊ตฌ๋งค
  2. AWS Route 53์— ํ˜ธ์ŠคํŒ… ์˜์—ญ ์ƒ์„ฑ
  3. ํ˜ธ์ŠคํŒ… ์˜์—ญ์ด ์ƒ์„ฑ๋˜๋ฉด ์œ ํ˜• NS์˜ ๊ฐ’ 4๊ฐœ๋ฅผ GoDaddy์˜ ๋„ค์ž„ ์„œ๋ฒ„์— ์„ค์ •
  4. AWS Route 53์—์„œ ๋ ˆ์ฝ”๋“œ ์œ ํ˜• A์— EC2 ํƒ„๋ ฅ์  IP ์—ฐ๊ฒฐ
  5. SSL(Secure Sockets Layer) ๋ณด์•ˆ์ด ์ ์šฉ๋˜์ง€ ์•Š์€ HTTP ๋„๋ฉ”์ธ ์—ฐ๊ฒฐ ์™„๋ฃŒ

HTTPS ์ ์šฉ

๊ธฐ์กด HTTP์— SSL ๋ณด์•ˆ ์ ์šฉ

  • ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๊ฐ€ ์ฃผ๊ณ ๋ฐ›๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์•”ํ˜ธํ™”
  • ๋ฐ์ดํ„ฐ ์•”ํ˜ธํ™”๋ฅผ ์œ„ํ•ด SSL์„ ์‚ฌ์šฉํ•จ
  1. AWS Certificate Manager ํ†ตํ•ด ACM ์ธ์ฆ์„œ ๋ฐœ๊ธ‰
    • ๋„๋ฉ”์ธ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์—ฌ ์ธ์ฆ์„œ ์š”์ฒญ ํ›„ ๋ ˆ์ฝ”๋“œ ๊ฒ€์ฆ ์™„๋ฃŒ
  2. ๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ ์„ค์ •
    1. ํƒ€๊ฒŸ ๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ
    2. ๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ ์ƒ์„ฑ - Application Load Balancer
      1. ํฌํŠธ ์ถ”๊ฐ€ํ•  ๋•Œ ํƒ€๊ฒŸ ๊ทธ๋ฃน ์ ์šฉ
      2. SSL ๋ณด์•ˆ : listener์— ACM ์ธ์ฆ์„œ ์ ์šฉ
  3. Route 53์—์„œ ๋‹จ์ˆœ ๋ผ์šฐํŒ… ์„ค์ •
    1. ๋ ˆ์ฝ”๋“œ ์œ ํ˜• A๋ฅผ ์„ ํƒ
    2. ๊ฐ’/ํŠธ๋ž˜ํ”ฝ ๋ผ์šฐํŒ… ๋Œ€์ƒ์— ์ด์ „์— ๋งŒ๋“  ๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ ์ ์šฉ
    3. ๋ ˆ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด HTTPS ์—ฐ๊ฒฐ ์™„๋ฃŒ

About

๐ŸŽž๐Ÿ“ ์˜ํ™” ์ •๋ณด์™€ ๋ฆฌ๋ทฐ์— ๊ด€ํ•œ ์›นํŽ˜์ด์ง€(Back-end)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published