Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

부산대 BE_이풍헌 6주차 과제(step3) #260

Open
wants to merge 24 commits into
base: canyos
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ plugins {
id 'org.springframework.boot' version '3.3.1'
id 'io.spring.dependency-management' version '1.1.5'
id "org.asciidoctor.jvm.convert" version "3.3.2"

}

group = 'camp.nextstep.edu'
Expand All @@ -29,9 +28,9 @@ dependencies {

implementation 'com.google.code.gson:gson:2.8.9'

implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
implementation 'io.jsonwebtoken:jjwt-impl:0.12.3'
implementation 'io.jsonwebtoken:jjwt-jackson:0.12.3'
implementation 'io.jsonwebtoken:jjwt-api:0.12.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.5'
implementation 'org.springframework.security:spring-security-crypto:5.7.1'

implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'
Expand All @@ -52,6 +51,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

ext {
set('snippetsDir', file("build/generated-snippets"))
}
Expand All @@ -74,4 +74,4 @@ bootJar {
from "${asciidoctor.outputDir}"
into 'src/main/resources/static/docs' // /static/docs로 복사!
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/gift/Util/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gift.Util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Collections;

@Configuration
public class CorsConfig {

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials(true);
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.setAllowedOriginPatterns(Collections.singletonList("*"));

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
19 changes: 19 additions & 0 deletions src/main/java/gift/Util/JWTConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gift.Util;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class JWTConfig {
@Autowired
private Environment env;

@PostConstruct
public void init() {
String jwtSecret = env.getProperty("jwt.secretKey");
int jwtExpirationMs = env.getProperty("jwt.expiredMs", Integer.class, 0);
JWTUtil.init(jwtSecret, jwtExpirationMs);
}
}
46 changes: 20 additions & 26 deletions src/main/java/gift/Util/JWTUtil.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
package gift.Util;

import gift.entity.User;

import io.jsonwebtoken.*;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.time.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Component
public class JWTUtil {
private static SecretKey key;
private static int expirationMs;

private final SecretKey key;
private final int expirationMs;

public JWTUtil(@Value("${jwt.secretKey}") String jwtSecret,
@Value("${jwt.expiredMs}") int jwtExpirationMs) {
this.key = Keys.hmacShaKeyFor(jwtSecret.getBytes());
this.expirationMs = jwtExpirationMs;
public static void init(String jwtSecret, int jwtExpirationMs) {
key = Keys.hmacShaKeyFor(jwtSecret.getBytes());
expirationMs = jwtExpirationMs;
}

public String generateToken(User user, String kakaoToken) {
public static String generateToken(int userId, String kakaoToken) {
LocalDateTime now = LocalDateTime.now();
Instant nowInstant = now.atZone(ZoneId.systemDefault()).toInstant();
Instant expireInstant = nowInstant.plus(Duration.ofMillis(expirationMs));
Date issuedAt = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
Date expiryDate = Date.from(now.plus(Duration.ofMillis(expirationMs)).atZone(ZoneId.systemDefault()).toInstant());

return Jwts.builder()
.subject(Integer.toString(user.getId()))
.subject(Integer.toString(userId))
.claim("kakaoToken", kakaoToken)
.issuedAt(Date.from(nowInstant))
.expiration(Date.from(expireInstant))
.issuedAt(issuedAt)
.expiration(expiryDate)
.signWith(key)
.compact();
}

public boolean validateToken(String token) throws JwtException {
public static boolean validateToken(String token) throws JwtException {
try {
Jwts.parser().verifyWith(key).build().parseSignedClaims(token);
} catch (JwtException e) {
Expand All @@ -49,7 +43,7 @@ public boolean validateToken(String token) throws JwtException {
return true;
}

public Integer getUserIdFromToken(String token) {
public static Integer getUserIdFromToken(String token) {
return Integer.parseInt(Jwts.parser()
.verifyWith(key)
.build()
Expand All @@ -58,7 +52,7 @@ public Integer getUserIdFromToken(String token) {
.getSubject());
}

public String getKakaoTokenFromToken(String token) {
public static String getKakaoTokenFromToken(String token) {
Claims claims = Jwts.parser()
.verifyWith(key)
.build()
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/gift/controller/CategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import gift.dto.category.SaveCategoryDTO;
import gift.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/gift/controller/OptionController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package gift.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import gift.dto.option.*;
import gift.dto.option.OptionQuantityDTO;
import gift.dto.option.OptionResponseDTO;
import gift.dto.option.SaveOptionDTO;
import gift.service.OptionService;
import gift.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/gift/controller/OrderController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package gift.controller;

import gift.dto.order.OrderPageDTO;
import gift.dto.order.OrderResponseDTO;
import gift.dto.order.OrderRequestDTO;
import gift.dto.order.*;
import gift.service.OrderService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -39,4 +36,9 @@ public OrderPageDTO getOrders(@RequestHeader("Authorization") String token,
return orderService.getOrders(token, pageable);
}

@GetMapping("/api/orders/price")
public PriceResponseDTO getPrice(@RequestBody PriceRequestDTO priceRequestDTO) {
return orderService.getPrice(priceRequestDTO);
}

}
9 changes: 0 additions & 9 deletions src/main/java/gift/controller/ProductAdminController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package gift.controller;


import gift.dto.product.ResponseProductDTO;
import gift.service.ProductService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/gift/controller/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@


import gift.dto.product.ProductPageDTO;
import gift.dto.product.SaveProductDTO;
import gift.dto.product.ResponseProductDTO;
import gift.dto.product.SaveProductDTO;
import gift.service.ProductService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/gift/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


import gift.dto.user.LoginDTO;
import gift.dto.user.PointDTO;
import gift.dto.user.SignUpDTO;

import gift.dto.user.UserResponseDTO;
import gift.service.UserService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -46,4 +46,10 @@ public UserResponseDTO kakakLogin() {
return userService.kakaoLogin();
}

@GetMapping("/api/members/point")
@ResponseBody
public PointDTO getPoint(@RequestHeader("Authorization") String token) {
return userService.getPoint(token);
}

}
3 changes: 1 addition & 2 deletions src/main/java/gift/controller/WishListController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package gift.controller;

import gift.dto.wish.WishPageDTO;
import gift.dto.wish.ResponseWishDTO;
import gift.dto.wish.SaveWishlistDTO;
import gift.dto.wish.WishPageDTO;
import gift.service.WishListService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

Expand Down
37 changes: 36 additions & 1 deletion src/main/java/gift/dto/order/OrderPageDTO.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
package gift.dto.order;

import org.springframework.data.domain.Page;

import java.util.List;

public record OrderPageDTO(List<OrderResponseDTO> content, int number, int totalElements, int size, boolean last) {
public class OrderPageDTO {
private List<OrderResponseDTO> content;
private int number;
private int totalElements;
private int size;
private boolean last;

public List<OrderResponseDTO> getContent() {
return content;
}

public int getNumber() {
return number;
}

public int getTotalElements() {
return totalElements;
}

public int getSize() {
return size;
}

public boolean isLast() {
return last;
}

public OrderPageDTO(Page<OrderResponseDTO> orders) {
this.content = orders.getContent();
this.number = orders.getNumber();
this.totalElements = (int) orders.getTotalElements();
this.size = orders.getSize();
this.last = orders.isLast();
}

}
4 changes: 3 additions & 1 deletion src/main/java/gift/dto/order/OrderRequestDTO.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package gift.dto.order;

public record OrderRequestDTO(int optionId, String message, int quantity) {
public record OrderRequestDTO(int optionId, String message,
int quantity, int productId, int point,
String phone, boolean receipt) {
}
13 changes: 12 additions & 1 deletion src/main/java/gift/dto/order/OrderResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@

import java.time.LocalDateTime;

public record OrderResponseDTO(int optionId, int quantity, LocalDateTime orderDateTime, String message) {
public record OrderResponseDTO(
int id,
int productId,
String name,
String imageUrl,
int optionId,
int count,
int price,
LocalDateTime orderDateTime,
String message,
boolean success) {

}
5 changes: 5 additions & 0 deletions src/main/java/gift/dto/order/PriceRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gift.dto.order;

public record PriceRequestDTO(int productId, int optionId, int quantity) {

}
4 changes: 4 additions & 0 deletions src/main/java/gift/dto/order/PriceResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package gift.dto.order;

public record PriceResponseDTO(int price) {
}
4 changes: 4 additions & 0 deletions src/main/java/gift/dto/user/PointDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package gift.dto.user;

public record PointDTO(int point) {
}
1 change: 1 addition & 0 deletions src/main/java/gift/dto/wish/ResponseWishDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
import gift.dto.product.ResponseProductDTO;

public record ResponseWishDTO(int id, ResponseProductDTO product) {

}
Loading