From 9c193090ce24a23044a7e104028952359b6f69df Mon Sep 17 00:00:00 2001 From: wook Date: Tue, 11 Jul 2023 16:44:10 +0900 Subject: [PATCH 01/20] chore : add jpa, mysql-connector --- build.gradle | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index acd405c..7be13d6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ plugins { - id 'java' - id 'org.springframework.boot' version '2.7.12' - id 'io.spring.dependency-management' version '1.0.15.RELEASE' + id 'java' + id 'org.springframework.boot' version '2.7.12' + id 'io.spring.dependency-management' version '1.0.15.RELEASE' } group = 'com.kumofactory' @@ -9,27 +9,30 @@ version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' configurations { - compileOnly { - extendsFrom annotationProcessor - } + compileOnly { + extendsFrom annotationProcessor + } } repositories { - mavenCentral() + mavenCentral() } dependencies { - developmentOnly 'org.springframework.boot:spring-boot-devtools' - implementation 'org.springframework.boot:spring-boot-starter-web' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - compileOnly 'org.projectlombok:lombok:1.18.24' - annotationProcessor 'org.projectlombok:lombok:1.18.24' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + implementation 'org.springframework.boot:spring-boot-starter-web' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + compileOnly 'org.projectlombok:lombok:1.18.24' + annotationProcessor 'org.projectlombok:lombok:1.18.24' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.1.1' + implementation 'mysql:mysql-connector-java:8.0.33' + implementation 'org.springframework.boot:spring-boot-starter-validation' - implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2' - implementation group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2' - implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2' + implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2' + implementation group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2' + implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2' } tasks.named('test') { - useJUnitPlatform() + useJUnitPlatform() } From c71af7eb0862a84bdf2ca9cdd670eac35a746534 Mon Sep 17 00:00:00 2001 From: wook Date: Tue, 11 Jul 2023 16:46:34 +0900 Subject: [PATCH 02/20] feat : add domain related with BluePrint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 향후 확장성을 위해 AwsComponent 도메인을 따로 생성 - PointLink 는 Component 간의 연결 관계를 나타낼 때 필요한데, 연관관계를 주지 없이 Component 들의 id 값만 저장 --- .../cloud/blueprint/domain/BluePrint.java | 37 +++++++++++ .../blueprint/domain/ComponentPoint.java | 56 ++++++++++++++++ .../cloud/blueprint/domain/PointLink.java | 44 +++++++++++++ .../blueprint/domain/aws/AwsComponent.java | 64 +++++++++++++++++++ .../domain/aws/AwsComponentType.java | 5 ++ .../cloud/member/domain/Member.java | 22 +++++++ 6 files changed, 228 insertions(+) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/BluePrint.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java create mode 100644 src/main/java/com/kumofactory/cloud/member/domain/Member.java diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/BluePrint.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/BluePrint.java new file mode 100644 index 0000000..1c1e94f --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/BluePrint.java @@ -0,0 +1,37 @@ +package com.kumofactory.cloud.blueprint.domain; + +import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; +import com.kumofactory.cloud.member.domain.Member; +import java.util.Date; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +@Entity +public class BluePrint { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + private String name; // 블루프린트 이름 + + @ManyToOne + private Member member; + + @OneToMany(mappedBy = "bluePrint", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY) + private List cspComponents; +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java new file mode 100644 index 0000000..4240aba --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java @@ -0,0 +1,56 @@ +package com.kumofactory.cloud.blueprint.domain; + +import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; +import com.kumofactory.cloud.blueprint.dto.ComponentPointDto; +import java.util.Date; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +@Entity +@Getter +@Setter +@NoArgsConstructor +public class ComponentPoint { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + @Column(unique = true) + private String ui_id; // Client 에서 생성하는 uuid + + // Component 의 찍힌 point 위치 + private Integer position_x; + private Integer position_y; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "csp_component_id") + private AwsComponent cspComponent; + + // ============== 생성함수 ================= // + public static ComponentPoint createComponentPoint(ComponentPointDto pointLinkDto, + AwsComponent cspComponent) { + ComponentPoint componentPoint = new ComponentPoint(); + componentPoint.setUi_id(pointLinkDto.getId()); + componentPoint.setPosition_x(pointLinkDto.getX()); + componentPoint.setPosition_y(pointLinkDto.getY()); + componentPoint.setCspComponent(cspComponent); + return componentPoint; + } +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java new file mode 100644 index 0000000..6df76b0 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java @@ -0,0 +1,44 @@ +package com.kumofactory.cloud.blueprint.domain; + +import com.kumofactory.cloud.blueprint.dto.PointLinkDto; +import java.util.Date; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +@Entity +@Getter +@Setter +public class PointLink { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + @Column(unique = true) + private String ui_id; // Client 에서 생성하는 uuid + + private String source_id; + private String destination_id; + + // ============== 생성함수 ================= // + public static PointLink createPointLink(PointLinkDto pointLinkDto) { + PointLink pointLink = new PointLink(); + pointLink.setUi_id(pointLinkDto.getId()); + pointLink.setSource_id(pointLinkDto.getSource()); + pointLink.setDestination_id(pointLinkDto.getTarget()); + return pointLink; + } +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java new file mode 100644 index 0000000..a27d6c3 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java @@ -0,0 +1,64 @@ +package com.kumofactory.cloud.blueprint.domain.aws; + +import com.kumofactory.cloud.blueprint.domain.BluePrint; +import com.kumofactory.cloud.blueprint.domain.ComponentPoint; +import com.kumofactory.cloud.blueprint.dto.aws.AwsComponentDto; +import java.util.Date; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +@Entity +@Getter +@Setter +@NoArgsConstructor +public class AwsComponent { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + @Column(unique = true) + private String ui_id; // Client 에서 생성하는 uuid + + @Enumerated(EnumType.STRING) + private AwsComponentType type; // Component 타입 (vm, vpc, subnet, ...) + + // Component 의 좌측 상단 좌표 + private Integer position_x; + private Integer position_y; + + @ManyToOne + private BluePrint bluePrint; + + @OneToMany(mappedBy = "cspComponent", cascade = CascadeType.REMOVE) + private List componentPoint; + + // ============== 생성함수 ================= // + public static AwsComponent createAwsComponent(AwsComponentDto awsComponentDto) { + AwsComponent awsComponent = new AwsComponent(); + awsComponent.setUi_id(awsComponentDto.getId()); + awsComponent.setPosition_x(awsComponentDto.getX()); + awsComponent.setPosition_y(awsComponentDto.getY()); + awsComponent.setType(awsComponentDto.getType()); + return awsComponent; + } +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java new file mode 100644 index 0000000..9fb122c --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java @@ -0,0 +1,5 @@ +package com.kumofactory.cloud.blueprint.domain.aws; + +public enum AwsComponentType { + VPC, SUBNET, ELB, EC2, EFS, WAF, ROUTE53, NAT_GATEWAY, RDS +} diff --git a/src/main/java/com/kumofactory/cloud/member/domain/Member.java b/src/main/java/com/kumofactory/cloud/member/domain/Member.java new file mode 100644 index 0000000..ab5c0df --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/member/domain/Member.java @@ -0,0 +1,22 @@ +package com.kumofactory.cloud.member.domain; + +import com.kumofactory.cloud.blueprint.domain.BluePrint; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class Member { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private Long social_id; + + @OneToMany(mappedBy = "member") + private List bluePrints; +} From 9366b6d910e3ef3781a86bdfcf46cc27b47dfca6 Mon Sep 17 00:00:00 2001 From: wook Date: Tue, 11 Jul 2023 16:47:06 +0900 Subject: [PATCH 03/20] feat : add dto related with blueprint --- .../blueprint/dto/ComponentPointDto.java | 15 +++++++++++ .../cloud/blueprint/dto/PointLinkDto.java | 16 +++++++++++ .../blueprint/dto/aws/AwsBluePrintDto.java | 12 +++++++++ .../blueprint/dto/aws/AwsComponentDto.java | 27 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java new file mode 100644 index 0000000..3794968 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java @@ -0,0 +1,15 @@ +package com.kumofactory.cloud.blueprint.dto; + +import javax.validation.constraints.NotNull; +import lombok.Getter; + +@Getter +public class ComponentPointDto { + + @NotNull + private String id; + @NotNull + private Integer x; + @NotNull + private Integer y; +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java new file mode 100644 index 0000000..c0f80e7 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java @@ -0,0 +1,16 @@ +package com.kumofactory.cloud.blueprint.dto; + +import javax.validation.constraints.NotNull; +import lombok.Getter; + +@Getter +public class PointLinkDto { + + @NotNull + private String id; + // Component uuid 값 + @NotNull + private String source; + @NotNull + private String target; +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java new file mode 100644 index 0000000..8fb8eb1 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -0,0 +1,12 @@ +package com.kumofactory.cloud.blueprint.dto.aws; + +import com.kumofactory.cloud.blueprint.dto.PointLinkDto; +import java.util.List; +import lombok.Getter; + +@Getter +public class AwsBluePrintDto { + + private List components; + private List links; +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java new file mode 100644 index 0000000..d66aa87 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java @@ -0,0 +1,27 @@ +package com.kumofactory.cloud.blueprint.dto.aws; + +import com.kumofactory.cloud.blueprint.domain.aws.AwsComponentType; +import com.kumofactory.cloud.blueprint.dto.ComponentPointDto; +import java.util.List; +import javax.validation.constraints.NotNull; +import lombok.Getter; +import org.springframework.lang.Nullable; + +@Getter +public class AwsComponentDto { + + @NotNull + private String id; + + @NotNull + private Integer x; + + @NotNull + private Integer y; + + @NotNull + private AwsComponentType type; + + @Nullable + private List points; +} From 7828b94b301e57b86fc405d0dec64b076c9d2e5d Mon Sep 17 00:00:00 2001 From: wook Date: Tue, 11 Jul 2023 16:47:29 +0900 Subject: [PATCH 04/20] feat : add Repository related with blueprint entity --- .../blueprint/repository/AwsComponentRepository.java | 8 ++++++++ .../cloud/blueprint/repository/BluePrintRepository.java | 9 +++++++++ .../blueprint/repository/ComponentPointRepository.java | 8 ++++++++ .../cloud/blueprint/repository/PointLinkRepository.java | 8 ++++++++ 4 files changed, 33 insertions(+) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/AwsComponentRepository.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/AwsComponentRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/AwsComponentRepository.java new file mode 100644 index 0000000..037e52b --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/AwsComponentRepository.java @@ -0,0 +1,8 @@ +package com.kumofactory.cloud.blueprint.repository; + +import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AwsComponentRepository extends JpaRepository { + +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java new file mode 100644 index 0000000..8a05f6c --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java @@ -0,0 +1,9 @@ +package com.kumofactory.cloud.blueprint.repository; + +import com.kumofactory.cloud.blueprint.domain.BluePrint; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +public interface BluePrintRepository extends JpaRepository { + +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java new file mode 100644 index 0000000..bda1d16 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java @@ -0,0 +1,8 @@ +package com.kumofactory.cloud.blueprint.repository; + +import com.kumofactory.cloud.blueprint.domain.ComponentPoint; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ComponentPointRepository extends JpaRepository { + +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java new file mode 100644 index 0000000..9f375a6 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java @@ -0,0 +1,8 @@ +package com.kumofactory.cloud.blueprint.repository; + +import com.kumofactory.cloud.blueprint.domain.PointLink; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PointLinkRepository extends JpaRepository{ + +} From 1c58ee830119d3ac4e2644a1806a1885738fac74 Mon Sep 17 00:00:00 2001 From: wook Date: Wed, 12 Jul 2023 17:31:44 +0900 Subject: [PATCH 05/20] =?UTF-8?q?feat=20:=20blueprint=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 6 +- .../kumofactory/cloud/CloudApplication.java | 6 +- .../cloud/blueprint/BlueprintController.java | 23 +++++ .../cloud/blueprint/domain/PointLink.java | 14 ++- .../{BluePrint.java => aws/AwsBluePrint.java} | 13 ++- .../blueprint/domain/aws/AwsComponent.java | 11 ++- .../blueprint/dto/aws/AwsBluePrintDto.java | 7 +- .../blueprint/dto/aws/AwsComponentDto.java | 1 - .../repository/BluePrintRepository.java | 9 -- .../repository/ComponentPointRepository.java | 2 + .../repository/PointLinkRepository.java | 6 +- .../aws/AwsBluePrintRepository.java | 10 +++ .../{ => aws}/AwsComponentRepository.java | 4 +- .../service/AwsBlueprintService.java | 9 ++ .../service/AwsBlueprintServiceImpl.java | 87 +++++++++++++++++++ .../cloud/member/MemberRepository.java | 10 +++ .../cloud/member/domain/Member.java | 12 ++- 17 files changed, 202 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java rename src/main/java/com/kumofactory/cloud/blueprint/domain/{BluePrint.java => aws/AwsBluePrint.java} (82%) delete mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java rename src/main/java/com/kumofactory/cloud/blueprint/repository/{ => aws}/AwsComponentRepository.java (64%) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java create mode 100644 src/main/java/com/kumofactory/cloud/member/MemberRepository.java diff --git a/build.gradle b/build.gradle index 7be13d6..cf8c093 100644 --- a/build.gradle +++ b/build.gradle @@ -19,14 +19,14 @@ repositories { } dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' developmentOnly 'org.springframework.boot:spring-boot-devtools' implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' - compileOnly 'org.projectlombok:lombok:1.18.24' - annotationProcessor 'org.projectlombok:lombok:1.18.24' - implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.1.1' implementation 'mysql:mysql-connector-java:8.0.33' implementation 'org.springframework.boot:spring-boot-starter-validation' + compileOnly 'org.projectlombok:lombok:1.18.24' + annotationProcessor 'org.projectlombok:lombok:1.18.24' implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2' implementation group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2' diff --git a/src/main/java/com/kumofactory/cloud/CloudApplication.java b/src/main/java/com/kumofactory/cloud/CloudApplication.java index 046f5fa..c494869 100644 --- a/src/main/java/com/kumofactory/cloud/CloudApplication.java +++ b/src/main/java/com/kumofactory/cloud/CloudApplication.java @@ -2,10 +2,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication public class CloudApplication { - public static void main(String[] args) { SpringApplication.run(CloudApplication.class, args); } + public static void main(String[] args) { + SpringApplication.run(CloudApplication.class, args); + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java new file mode 100644 index 0000000..f6aab6a --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java @@ -0,0 +1,23 @@ +package com.kumofactory.cloud.blueprint; + +import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; +import com.kumofactory.cloud.blueprint.service.AwsBlueprintService; +import lombok.RequiredArgsConstructor; +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.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/blueprint") +public class BlueprintController { + + private final AwsBlueprintService awsBlueprintService; + + @PostMapping("/aws") + public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { + awsBlueprintService.store(awsBluePrintDto); + return "hello-world"; + } +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java index 6df76b0..1b304cf 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java @@ -1,5 +1,6 @@ package com.kumofactory.cloud.blueprint.domain; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.PointLinkDto; import java.util.Date; import javax.persistence.Column; @@ -7,8 +8,11 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.ManyToOne; import javax.persistence.OneToOne; +import javax.persistence.Table; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; @@ -16,6 +20,8 @@ @Entity @Getter @Setter +@NoArgsConstructor +@Table public class PointLink { @Id @@ -29,16 +35,20 @@ public class PointLink { @Column(unique = true) private String ui_id; // Client 에서 생성하는 uuid - + private String source_id; private String destination_id; + @ManyToOne + private AwsBluePrint bluePrint; + // ============== 생성함수 ================= // - public static PointLink createPointLink(PointLinkDto pointLinkDto) { + public static PointLink createPointLink(PointLinkDto pointLinkDto, AwsBluePrint bluePrint) { PointLink pointLink = new PointLink(); pointLink.setUi_id(pointLinkDto.getId()); pointLink.setSource_id(pointLinkDto.getSource()); pointLink.setDestination_id(pointLinkDto.getTarget()); + pointLink.setBluePrint(bluePrint); return pointLink; } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/BluePrint.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsBluePrint.java similarity index 82% rename from src/main/java/com/kumofactory/cloud/blueprint/domain/BluePrint.java rename to src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsBluePrint.java index 1c1e94f..4d87a0f 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/BluePrint.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsBluePrint.java @@ -1,6 +1,5 @@ -package com.kumofactory.cloud.blueprint.domain; +package com.kumofactory.cloud.blueprint.domain.aws; -import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; import com.kumofactory.cloud.member.domain.Member; import java.util.Date; import java.util.List; @@ -12,11 +11,17 @@ import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; @Entity -public class BluePrint { +@Getter +@Setter +@NoArgsConstructor +public class AwsBluePrint { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -34,4 +39,6 @@ public class BluePrint { @OneToMany(mappedBy = "bluePrint", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY) private List cspComponents; + + } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java index a27d6c3..7e851f6 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java @@ -1,8 +1,8 @@ package com.kumofactory.cloud.blueprint.domain.aws; -import com.kumofactory.cloud.blueprint.domain.BluePrint; import com.kumofactory.cloud.blueprint.domain.ComponentPoint; import com.kumofactory.cloud.blueprint.dto.aws.AwsComponentDto; + import java.util.Date; import java.util.List; import javax.persistence.CascadeType; @@ -15,6 +15,7 @@ import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; + import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -47,18 +48,20 @@ public class AwsComponent { private Integer position_y; @ManyToOne - private BluePrint bluePrint; + private AwsBluePrint bluePrint; - @OneToMany(mappedBy = "cspComponent", cascade = CascadeType.REMOVE) + @OneToMany(mappedBy = "cspComponent", cascade = CascadeType.ALL) private List componentPoint; // ============== 생성함수 ================= // - public static AwsComponent createAwsComponent(AwsComponentDto awsComponentDto) { + public static AwsComponent createAwsComponent(AwsComponentDto awsComponentDto, + AwsBluePrint awsBluePrint) { AwsComponent awsComponent = new AwsComponent(); awsComponent.setUi_id(awsComponentDto.getId()); awsComponent.setPosition_x(awsComponentDto.getX()); awsComponent.setPosition_y(awsComponentDto.getY()); awsComponent.setType(awsComponentDto.getType()); + awsComponent.setBluePrint(awsBluePrint); return awsComponent; } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java index 8fb8eb1..59b1b6c 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -1,12 +1,17 @@ package com.kumofactory.cloud.blueprint.dto.aws; import com.kumofactory.cloud.blueprint.dto.PointLinkDto; + import java.util.List; + +import lombok.Data; import lombok.Getter; -@Getter + +@Data public class AwsBluePrintDto { + private String name; private List components; private List links; } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java index d66aa87..770e27e 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java @@ -22,6 +22,5 @@ public class AwsComponentDto { @NotNull private AwsComponentType type; - @Nullable private List points; } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java deleted file mode 100644 index 8a05f6c..0000000 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/BluePrintRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.kumofactory.cloud.blueprint.repository; - -import com.kumofactory.cloud.blueprint.domain.BluePrint; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -public interface BluePrintRepository extends JpaRepository { - -} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java index bda1d16..072fe45 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java @@ -2,7 +2,9 @@ import com.kumofactory.cloud.blueprint.domain.ComponentPoint; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +@Repository public interface ComponentPointRepository extends JpaRepository { } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java index 9f375a6..a5d191a 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java @@ -2,7 +2,11 @@ import com.kumofactory.cloud.blueprint.domain.PointLink; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; -public interface PointLinkRepository extends JpaRepository{ +@Repository +@Transactional +public interface PointLinkRepository extends JpaRepository { } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java new file mode 100644 index 0000000..683cc82 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java @@ -0,0 +1,10 @@ +package com.kumofactory.cloud.blueprint.repository.aws; + +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AwsBluePrintRepository extends JpaRepository { + +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/AwsComponentRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsComponentRepository.java similarity index 64% rename from src/main/java/com/kumofactory/cloud/blueprint/repository/AwsComponentRepository.java rename to src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsComponentRepository.java index 037e52b..a5e7696 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/AwsComponentRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsComponentRepository.java @@ -1,8 +1,10 @@ -package com.kumofactory.cloud.blueprint.repository; +package com.kumofactory.cloud.blueprint.repository.aws; import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +@Repository public interface AwsComponentRepository extends JpaRepository { } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java new file mode 100644 index 0000000..e11790f --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java @@ -0,0 +1,9 @@ +package com.kumofactory.cloud.blueprint.service; + + +import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; + +public interface AwsBlueprintService { + + void store(AwsBluePrintDto awsBluePrintDto); +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java new file mode 100644 index 0000000..411bc53 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java @@ -0,0 +1,87 @@ +package com.kumofactory.cloud.blueprint.service; + +import com.kumofactory.cloud.blueprint.domain.ComponentPoint; +import com.kumofactory.cloud.blueprint.domain.PointLink; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; +import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; +import com.kumofactory.cloud.blueprint.dto.ComponentPointDto; +import com.kumofactory.cloud.blueprint.dto.PointLinkDto; +import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; + +import com.kumofactory.cloud.blueprint.dto.aws.AwsComponentDto; +import com.kumofactory.cloud.blueprint.repository.ComponentPointRepository; +import com.kumofactory.cloud.blueprint.repository.PointLinkRepository; +import com.kumofactory.cloud.blueprint.repository.aws.AwsBluePrintRepository; +import com.kumofactory.cloud.blueprint.repository.aws.AwsComponentRepository; +import com.kumofactory.cloud.member.MemberRepository; +import com.kumofactory.cloud.member.domain.Member; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +@Slf4j +@Repository +public class AwsBlueprintServiceImpl implements AwsBlueprintService { + + private final MemberRepository memberRepository; + private final AwsBluePrintRepository awsBluePrintRepository; + private final AwsComponentRepository awsComponentRepository; + private final ComponentPointRepository componentPointRepository; + private final PointLinkRepository pointLinkRepository; + private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); + + + @Override + public void store(AwsBluePrintDto awsBluePrintDto) { +// Member member = memberRepository.findMemberById(1L); + + // BluePrint 저장 + AwsBluePrint awsBluePrint = new AwsBluePrint(); + awsBluePrint.setName(awsBluePrintDto.getName()); +// awsBluePrint.setMember(member); + AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); + logger.info("savedBlueprint: {}", savedBlueprint); + + List components = new ArrayList<>(); + List links = awsBluePrintDto.getLinks(); + + for (AwsComponentDto component : awsBluePrintDto.getComponents()) { + AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); + + List componentPoints = new ArrayList<>(); + List points = component.getPoints(); + if (points.size() != 0) { + for (ComponentPointDto point : points) { + ComponentPoint componentPoint = ComponentPoint.createComponentPoint(point, + awsComponent); + componentPoints.add(componentPoint); + } + } + awsComponent.setComponentPoint(componentPoints); + components.add(awsComponent); + } + awsComponentRepository.saveAll(components); + + // Component 저장 + if (links.size() != 0) { + // PointLink 저장 + List toSaveLink = new ArrayList<>(); + for (PointLinkDto link : links) { + PointLink pointLink = PointLink.createPointLink(link, savedBlueprint); + toSaveLink.add(pointLink); + } + + pointLinkRepository.saveAll(toSaveLink); + } + } +} diff --git a/src/main/java/com/kumofactory/cloud/member/MemberRepository.java b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java new file mode 100644 index 0000000..594b50e --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java @@ -0,0 +1,10 @@ +package com.kumofactory.cloud.member; + +import com.kumofactory.cloud.member.domain.Member; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MemberRepository extends JpaRepository { + Member findMemberById(Long id); +} diff --git a/src/main/java/com/kumofactory/cloud/member/domain/Member.java b/src/main/java/com/kumofactory/cloud/member/domain/Member.java index ab5c0df..f4bed73 100644 --- a/src/main/java/com/kumofactory/cloud/member/domain/Member.java +++ b/src/main/java/com/kumofactory/cloud/member/domain/Member.java @@ -1,6 +1,7 @@ package com.kumofactory.cloud.member.domain; -import com.kumofactory.cloud.blueprint.domain.BluePrint; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; + import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -8,7 +9,14 @@ import javax.persistence.Id; import javax.persistence.OneToMany; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + @Entity +@Getter +@Setter public class Member { @Id @@ -18,5 +26,5 @@ public class Member { private Long social_id; @OneToMany(mappedBy = "member") - private List bluePrints; + private List bluePrints; } From e56d9139eaf5537ecedf5bb39d40ce5f630c5fbf Mon Sep 17 00:00:00 2001 From: wook Date: Wed, 12 Jul 2023 23:24:27 +0900 Subject: [PATCH 06/20] feat : --- .../cloud/blueprint/BlueprintController.java | 10 ++++++---- .../cloud/blueprint/service/AwsBlueprintService.java | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java index f6aab6a..a526ab3 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java @@ -3,10 +3,7 @@ import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; import com.kumofactory.cloud.blueprint.service.AwsBlueprintService; import lombok.RequiredArgsConstructor; -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.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor @@ -15,6 +12,11 @@ public class BlueprintController { private final AwsBlueprintService awsBlueprintService; + @GetMapping("/aws") + public AwsBluePrintDto getAwsBlueprint() { + return "hello-world"; + } + @PostMapping("/aws") public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { awsBlueprintService.store(awsBluePrintDto); diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java index e11790f..184e862 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java @@ -5,5 +5,7 @@ public interface AwsBlueprintService { + void getAwsBlueprint(); + void store(AwsBluePrintDto awsBluePrintDto); } From bfe07f89418604a0fa721658b549024427111909 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Thu, 13 Jul 2023 01:06:25 +0900 Subject: [PATCH 07/20] feat : add mapping domain to dto in Dto class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 데이터 베이스에서 가져온 Entity 를 Dto 로 맵핑하는 함수 추가 - Mapper Class 를 따로 분리하지 않고 Dto 내부에 생성함수 추가 --- .../blueprint/dto/ComponentPointDto.java | 24 +++++++++--- .../blueprint/dto/aws/AwsBluePrintDto.java | 25 ++++++++++-- .../blueprint/dto/aws/AwsComponentDto.java | 39 ++++++++++++++----- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java index 3794968..b615e3e 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java @@ -1,15 +1,27 @@ package com.kumofactory.cloud.blueprint.dto; import javax.validation.constraints.NotNull; + +import com.kumofactory.cloud.blueprint.domain.ComponentPoint; import lombok.Getter; +import lombok.Setter; @Getter +@Setter public class ComponentPointDto { - @NotNull - private String id; - @NotNull - private Integer x; - @NotNull - private Integer y; + @NotNull + private String id; + @NotNull + private Integer x; + @NotNull + private Integer y; + + public static ComponentPointDto mapper(ComponentPoint componentPoint) { + ComponentPointDto componentPointDto = new ComponentPointDto(); + componentPointDto.setId(componentPoint.getUi_id()); + componentPointDto.setX(componentPoint.getPosition_x()); + componentPointDto.setY(componentPoint.getPosition_y()); + return componentPointDto; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java index 59b1b6c..f232d28 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -1,7 +1,10 @@ package com.kumofactory.cloud.blueprint.dto.aws; +import com.kumofactory.cloud.blueprint.domain.PointLink; +import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; import com.kumofactory.cloud.blueprint.dto.PointLinkDto; +import java.util.ArrayList; import java.util.List; import lombok.Data; @@ -11,7 +14,23 @@ @Data public class AwsBluePrintDto { - private String name; - private List components; - private List links; + private String name; + private List components; + private List links; + + public static List awsComponentDtosMapper(List awsComponentDtos) { + List awsComponentDtoList = new ArrayList<>(); + for (AwsComponent awsComponent : awsComponentDtos) { + awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); + } + return awsComponentDtoList; + } + + public static List pointLinkDtosMapper(List pointLinks) { + List pointLinkDtoList = new ArrayList<>(); + for (PointLink pointLink : pointLinks) { + pointLinkDtoList.add(PointLinkDto.mapper(pointLink)); + } + return pointLinkDtoList; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java index 770e27e..cb28f67 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java @@ -1,26 +1,47 @@ package com.kumofactory.cloud.blueprint.dto.aws; +import com.kumofactory.cloud.blueprint.domain.ComponentPoint; +import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; import com.kumofactory.cloud.blueprint.domain.aws.AwsComponentType; import com.kumofactory.cloud.blueprint.dto.ComponentPointDto; + +import java.util.ArrayList; import java.util.List; import javax.validation.constraints.NotNull; + import lombok.Getter; +import lombok.Setter; import org.springframework.lang.Nullable; @Getter +@Setter public class AwsComponentDto { - @NotNull - private String id; + @NotNull + private String id; + + @NotNull + private Integer x; - @NotNull - private Integer x; + @NotNull + private Integer y; - @NotNull - private Integer y; + @NotNull + private AwsComponentType type; - @NotNull - private AwsComponentType type; + private List points; - private List points; + public static AwsComponentDto mapper(AwsComponent awsComponent) { + AwsComponentDto awsComponentDto = new AwsComponentDto(); + awsComponentDto.setId(awsComponent.getUi_id()); + awsComponentDto.setX(awsComponent.getPosition_x()); + awsComponentDto.setY(awsComponent.getPosition_y()); + awsComponentDto.setType(awsComponent.getType()); + List points = new ArrayList<>(); + for (ComponentPoint componentPoint : awsComponent.getComponentPoint()) { + points.add(ComponentPointDto.mapper(componentPoint)); + } + awsComponentDto.setPoints(points); + return awsComponentDto; + } } From ac476aea018ca1c9b8036704575059fdb473a9f9 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Thu, 13 Jul 2023 01:06:46 +0900 Subject: [PATCH 08/20] feat : add mapping domain to dto in Dto class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 데이터 베이스에서 가져온 Entity 를 Dto 로 맵핑하는 함수 추가 - Mapper Class 를 따로 분리하지 않고 Dto 내부에 생성함수 추가 --- .../cloud/blueprint/dto/PointLinkDto.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java index c0f80e7..f3d8840 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java @@ -1,16 +1,28 @@ package com.kumofactory.cloud.blueprint.dto; import javax.validation.constraints.NotNull; + +import com.kumofactory.cloud.blueprint.domain.PointLink; import lombok.Getter; +import lombok.Setter; @Getter +@Setter public class PointLinkDto { - @NotNull - private String id; - // Component uuid 값 - @NotNull - private String source; - @NotNull - private String target; + @NotNull + private String id; + // Component uuid 값 + @NotNull + private String source; + @NotNull + private String target; + + public static PointLinkDto mapper(PointLink pointLink) { + PointLinkDto pointLinkDto = new PointLinkDto(); + pointLinkDto.setId(pointLink.getUi_id()); + pointLinkDto.setSource(pointLink.getSource_id()); + pointLinkDto.setTarget(pointLink.getDestination_id()); + return pointLinkDto; + } } From 7eb055a55b96b49f14b0a150e510587fd92963f8 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Thu, 13 Jul 2023 01:08:18 +0900 Subject: [PATCH 09/20] feat : get Blueprint api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 유저 정보로 부터 blueprint 가져오는 기능 추가 예정 --- .../cloud/blueprint/BlueprintController.java | 27 +++-- .../blueprint/domain/aws/AwsComponent.java | 84 +++++++-------- .../repository/PointLinkRepository.java | 5 +- .../aws/AwsBluePrintRepository.java | 2 +- .../aws/AwsComponentRepository.java | 5 +- .../service/AwsBlueprintService.java | 5 +- .../service/AwsBlueprintServiceImpl.java | 101 ++++++++++-------- 7 files changed, 126 insertions(+), 103 deletions(-) diff --git a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java index a526ab3..ce582d3 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java @@ -1,5 +1,6 @@ package com.kumofactory.cloud.blueprint; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; import com.kumofactory.cloud.blueprint.service.AwsBlueprintService; import lombok.RequiredArgsConstructor; @@ -10,16 +11,22 @@ @RequestMapping("/blueprint") public class BlueprintController { - private final AwsBlueprintService awsBlueprintService; + private final AwsBlueprintService awsBlueprintService; - @GetMapping("/aws") - public AwsBluePrintDto getAwsBlueprint() { - return "hello-world"; - } + // TODO : 토큰에서 유저 정보 가져오는 로직 추가 + @GetMapping("/aws") + public AwsBluePrintDto getAwsBlueprint() { + try { + AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(); + return awsBlueprint; + } catch (RuntimeException e) { + return null; + } + } - @PostMapping("/aws") - public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { - awsBlueprintService.store(awsBluePrintDto); - return "hello-world"; - } + @PostMapping("/aws") + public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { + awsBlueprintService.store(awsBluePrintDto); + return "hello-world"; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java index 7e851f6..0e43678 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java @@ -5,21 +5,13 @@ import java.util.Date; import java.util.List; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; +import javax.persistence.*; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.Fetch; import org.hibernate.annotations.UpdateTimestamp; @Entity @@ -28,40 +20,40 @@ @NoArgsConstructor public class AwsComponent { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @CreationTimestamp - private Date created_at; - @UpdateTimestamp - private Date updated_at; - - @Column(unique = true) - private String ui_id; // Client 에서 생성하는 uuid - - @Enumerated(EnumType.STRING) - private AwsComponentType type; // Component 타입 (vm, vpc, subnet, ...) - - // Component 의 좌측 상단 좌표 - private Integer position_x; - private Integer position_y; - - @ManyToOne - private AwsBluePrint bluePrint; - - @OneToMany(mappedBy = "cspComponent", cascade = CascadeType.ALL) - private List componentPoint; - - // ============== 생성함수 ================= // - public static AwsComponent createAwsComponent(AwsComponentDto awsComponentDto, - AwsBluePrint awsBluePrint) { - AwsComponent awsComponent = new AwsComponent(); - awsComponent.setUi_id(awsComponentDto.getId()); - awsComponent.setPosition_x(awsComponentDto.getX()); - awsComponent.setPosition_y(awsComponentDto.getY()); - awsComponent.setType(awsComponentDto.getType()); - awsComponent.setBluePrint(awsBluePrint); - return awsComponent; - } + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + @Column(unique = true) + private String ui_id; // Client 에서 생성하는 uuid + + @Enumerated(EnumType.STRING) + private AwsComponentType type; // Component 타입 (vm, vpc, subnet, ...) + + // Component 의 좌측 상단 좌표 + private Integer position_x; + private Integer position_y; + + @ManyToOne + private AwsBluePrint bluePrint; + + @OneToMany(mappedBy = "cspComponent", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private List componentPoint; + + // ============== 생성함수 ================= // + public static AwsComponent createAwsComponent(AwsComponentDto awsComponentDto, + AwsBluePrint awsBluePrint) { + AwsComponent awsComponent = new AwsComponent(); + awsComponent.setUi_id(awsComponentDto.getId()); + awsComponent.setPosition_x(awsComponentDto.getX()); + awsComponent.setPosition_y(awsComponentDto.getY()); + awsComponent.setType(awsComponentDto.getType()); + awsComponent.setBluePrint(awsBluePrint); + return awsComponent; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java index a5d191a..f516ae4 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java @@ -1,12 +1,15 @@ package com.kumofactory.cloud.blueprint.repository; import com.kumofactory.cloud.blueprint.domain.PointLink; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Repository @Transactional public interface PointLinkRepository extends JpaRepository { - + List findAllByBluePrint(AwsBluePrint bluePrint); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java index 683cc82..8bb149f 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java @@ -6,5 +6,5 @@ @Repository public interface AwsBluePrintRepository extends JpaRepository { - + AwsBluePrint findAwsBluePrintById(long id); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsComponentRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsComponentRepository.java index a5e7696..3cb2682 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsComponentRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsComponentRepository.java @@ -1,10 +1,13 @@ package com.kumofactory.cloud.blueprint.repository.aws; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AwsComponentRepository extends JpaRepository { - + List findAllByBluePrint(AwsBluePrint awsBluePrint); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java index 184e862..72bbff8 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java @@ -1,11 +1,12 @@ package com.kumofactory.cloud.blueprint.service; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; public interface AwsBlueprintService { - void getAwsBlueprint(); + AwsBluePrintDto getAwsBlueprint(); - void store(AwsBluePrintDto awsBluePrintDto); + void store(AwsBluePrintDto awsBluePrintDto); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java index 411bc53..5588258 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java @@ -33,55 +33,72 @@ @Repository public class AwsBlueprintServiceImpl implements AwsBlueprintService { - private final MemberRepository memberRepository; - private final AwsBluePrintRepository awsBluePrintRepository; - private final AwsComponentRepository awsComponentRepository; - private final ComponentPointRepository componentPointRepository; - private final PointLinkRepository pointLinkRepository; - private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); + private final MemberRepository memberRepository; + private final AwsBluePrintRepository awsBluePrintRepository; + private final AwsComponentRepository awsComponentRepository; + private final ComponentPointRepository componentPointRepository; + private final PointLinkRepository pointLinkRepository; + private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); - @Override - public void store(AwsBluePrintDto awsBluePrintDto) { + @Override + public AwsBluePrintDto getAwsBlueprint() { + AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintById(1L); + List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); + List pointLinks = pointLinkRepository.findAllByBluePrint(awsBluePrintById); + logger.info("awsBluePrintById: {}", awsBluePrintById); + if (awsBluePrintById == null) { + throw new RuntimeException("awsBluePrintById is null"); + } + AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); + awsBluePrintDto.setName(awsBluePrintById.getName()); + awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); + awsBluePrintDto.setLinks(AwsBluePrintDto.pointLinkDtosMapper(pointLinks)); + + return awsBluePrintDto; + } + + @Override + public void store(AwsBluePrintDto awsBluePrintDto) { // Member member = memberRepository.findMemberById(1L); - // BluePrint 저장 - AwsBluePrint awsBluePrint = new AwsBluePrint(); - awsBluePrint.setName(awsBluePrintDto.getName()); + // BluePrint 저장 + AwsBluePrint awsBluePrint = new AwsBluePrint(); + awsBluePrint.setName(awsBluePrintDto.getName()); // awsBluePrint.setMember(member); - AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); - logger.info("savedBlueprint: {}", savedBlueprint); + AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); + logger.info("savedBlueprint: {}", savedBlueprint); + + List components = new ArrayList<>(); + List links = awsBluePrintDto.getLinks(); - List components = new ArrayList<>(); - List links = awsBluePrintDto.getLinks(); + for (AwsComponentDto component : awsBluePrintDto.getComponents()) { + AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); - for (AwsComponentDto component : awsBluePrintDto.getComponents()) { - AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); + List componentPoints = new ArrayList<>(); + List points = component.getPoints(); + if (points.size() != 0) { + for (ComponentPointDto point : points) { + ComponentPoint componentPoint = ComponentPoint.createComponentPoint(point, + awsComponent); + componentPoints.add(componentPoint); + } + } + awsComponent.setComponentPoint(componentPoints); + components.add(awsComponent); + } + awsComponentRepository.saveAll(components); - List componentPoints = new ArrayList<>(); - List points = component.getPoints(); - if (points.size() != 0) { - for (ComponentPointDto point : points) { - ComponentPoint componentPoint = ComponentPoint.createComponentPoint(point, - awsComponent); - componentPoints.add(componentPoint); - } - } - awsComponent.setComponentPoint(componentPoints); - components.add(awsComponent); - } - awsComponentRepository.saveAll(components); - - // Component 저장 - if (links.size() != 0) { - // PointLink 저장 - List toSaveLink = new ArrayList<>(); - for (PointLinkDto link : links) { - PointLink pointLink = PointLink.createPointLink(link, savedBlueprint); - toSaveLink.add(pointLink); - } + // Component 저장 + if (links.size() != 0) { + // PointLink 저장 + List toSaveLink = new ArrayList<>(); + for (PointLinkDto link : links) { + PointLink pointLink = PointLink.createPointLink(link, savedBlueprint); + toSaveLink.add(pointLink); + } - pointLinkRepository.saveAll(toSaveLink); - } - } + pointLinkRepository.saveAll(toSaveLink); + } + } } From 3e56f9ee5bb5b190cdc0bbd0fc7c6518b18acf39 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Thu, 13 Jul 2023 01:08:42 +0900 Subject: [PATCH 10/20] Chore : add io.jsonwebtoken:jjwt-api:0.11.5 --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index cf8c093..91a416c 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,8 @@ dependencies { implementation 'mysql:mysql-connector-java:8.0.33' implementation 'org.springframework.boot:spring-boot-starter-validation' compileOnly 'org.projectlombok:lombok:1.18.24' + implementation 'io.jsonwebtoken:jjwt-api:0.11.5' + annotationProcessor 'org.projectlombok:lombok:1.18.24' implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2' From 384dede29711ba3dce7871206f0abaa66cbf30a8 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Thu, 13 Jul 2023 01:08:59 +0900 Subject: [PATCH 11/20] feat : add validateToken --- .../cloud/jwt/provider/JwtTokenProvider.java | 106 ++++++++++-------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java index bf67215..62684af 100644 --- a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java +++ b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java @@ -1,6 +1,8 @@ package com.kumofactory.cloud.jwt.provider; import com.kumofactory.cloud.jwt.dto.TokenDto; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jws; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.security.Keys; import lombok.RequiredArgsConstructor; @@ -13,54 +15,62 @@ @RequiredArgsConstructor public class JwtTokenProvider { - @Value("${jwt.secret}") - private String secret; - @Value("${jwt.token-validity-in-milliseconds}") - private long VALIDITY; - - public TokenDto create(String id) { - - Date now = new Date(); - - String accessToken = Jwts.builder() - .setSubject(id) - .setIssuedAt(now) - .setExpiration(new Date(now.getTime() + VALIDITY)) - .signWith(Keys.hmacShaKeyFor(secret.getBytes())) - .compact(); - - String refreshToken = Jwts.builder() - .setSubject(id) - .setIssuedAt(now) - .setExpiration(new Date(now.getTime() + (VALIDITY * 365))) - .signWith(Keys.hmacShaKeyFor(secret.getBytes())) - .compact(); - - return TokenDto.builder() - .userEmail(id) - .accessToken(accessToken) - .refreshToken(refreshToken) - .build(); - } - - public String getUserId(String token) { - return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody().getSubject(); - } - - public String validateAccessToken() { - - return null; - } - - public String validateRefreshToken() { - - return null; - } - - public String refreshAccessToken() { - - return null; - } + @Value("${jwt.secret}") + private String secret; + @Value("${jwt.token-validity-in-milliseconds}") + private long VALIDITY; + + public TokenDto create(String id) { + + Date now = new Date(); + + String accessToken = Jwts.builder() + .setSubject(id) + .setIssuedAt(now) + .setExpiration(new Date(now.getTime() + VALIDITY)) + .signWith(Keys.hmacShaKeyFor(secret.getBytes())) + .compact(); + + String refreshToken = Jwts.builder() + .setSubject(id) + .setIssuedAt(now) + .setExpiration(new Date(now.getTime() + (VALIDITY * 365))) + .signWith(Keys.hmacShaKeyFor(secret.getBytes())) + .compact(); + + return TokenDto.builder() + .userEmail(id) + .accessToken(accessToken) + .refreshToken(refreshToken) + .build(); + } + + public String getUserId(String token) { + return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody().getSubject(); + } + + public boolean validateAccessToken(String token) { + try { + Jws claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); + return !claims.getBody().getExpiration().before(new Date()); + } catch (Exception e) { + return false; + } + } + + public boolean validateRefreshToken(String token) { + try { + Jws claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); + return !claims.getBody().getExpiration().before(new Date()); + } catch (Exception e) { + return false; + } + } + + public String refreshAccessToken() { + + return null; + } } From a91f169b49a411090efcf12d70b63210301a552b Mon Sep 17 00:00:00 2001 From: wook Date: Thu, 13 Jul 2023 16:13:03 +0900 Subject: [PATCH 12/20] =?UTF-8?q?refactor=20:=20=ED=9A=A8=EA=B7=BC?= =?UTF-8?q?=EC=9D=B4=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloud/blueprint/BlueprintController.java | 40 +++--- .../cloud/blueprint/domain/ComponentDot.java | 43 +++++++ .../cloud/blueprint/domain/ComponentLine.java | 45 +++++++ .../blueprint/domain/ComponentPoint.java | 56 -------- .../cloud/blueprint/domain/PointLink.java | 54 -------- .../blueprint/domain/aws/AwsBluePrint.java | 5 + .../blueprint/domain/aws/AwsComponent.java | 71 +++++------ .../domain/aws/AwsComponentType.java | 2 +- .../cloud/blueprint/dto/ComponentDotDto.java | 21 +++ .../cloud/blueprint/dto/ComponentLineDto.java | 21 +++ .../blueprint/dto/ComponentPointDto.java | 27 ---- .../cloud/blueprint/dto/PointLinkDto.java | 28 ---- .../blueprint/dto/aws/AwsBluePrintDto.java | 43 +++---- .../blueprint/dto/aws/AwsComponentDto.java | 52 +++----- ...itory.java => ComponentDotRepository.java} | 5 +- .../repository/ComponentLineRepository.java | 13 ++ .../repository/PointLinkRepository.java | 15 --- .../service/AwsBlueprintService.java | 8 +- .../service/AwsBlueprintServiceImpl.java | 120 ++++++++---------- 19 files changed, 307 insertions(+), 362 deletions(-) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentLine.java delete mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java delete mode 100644 src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentDotDto.java create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentLineDto.java delete mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java delete mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java rename src/main/java/com/kumofactory/cloud/blueprint/repository/{ComponentPointRepository.java => ComponentDotRepository.java} (54%) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentLineRepository.java delete mode 100644 src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java diff --git a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java index ce582d3..9dafc31 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java @@ -4,29 +4,35 @@ import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; import com.kumofactory.cloud.blueprint.service.AwsBlueprintService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor @RequestMapping("/blueprint") +@Slf4j public class BlueprintController { + private final Logger logger = LoggerFactory.getLogger(BlueprintController.class); + private final AwsBlueprintService awsBlueprintService; - private final AwsBlueprintService awsBlueprintService; + // TODO : 토큰에서 유저 정보 가져오는 로직 추가 + // TODO : blueprint list 주는 api 추가 + // TODO : blueprint id 값으로 blueprint 가져오는 api 추가 + @GetMapping("/aws") + public AwsBluePrintDto getAwsBlueprint() { + try { + AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(); + return awsBlueprint; + } catch (RuntimeException e) { + return null; + } + } - // TODO : 토큰에서 유저 정보 가져오는 로직 추가 - @GetMapping("/aws") - public AwsBluePrintDto getAwsBlueprint() { - try { - AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(); - return awsBlueprint; - } catch (RuntimeException e) { - return null; - } - } - - @PostMapping("/aws") - public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { - awsBlueprintService.store(awsBluePrintDto); - return "hello-world"; - } + @PostMapping("/aws") + public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { + awsBlueprintService.store(awsBluePrintDto); + return "hello-world"; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java new file mode 100644 index 0000000..1e7f557 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java @@ -0,0 +1,43 @@ +package com.kumofactory.cloud.blueprint.domain; + +import com.kumofactory.cloud.blueprint.dto.ComponentDotDto; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Date; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class ComponentDot { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + private Integer x; // 점의 x 좌표 + private Integer y; // 점의 y 좌표 + private String componentId; // 점이 속한 컴포넌트의 id + + public static ComponentDot createComponentDot(ComponentDotDto componentDotDto) { + ComponentDot componentDot = new ComponentDot(); + componentDot.setComponentId(componentDotDto.getComponentId()); + componentDot.setX(componentDotDto.getX()); + componentDot.setY(componentDotDto.getY()); + return componentDot; + } + +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentLine.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentLine.java new file mode 100644 index 0000000..18f0e04 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentLine.java @@ -0,0 +1,45 @@ +package com.kumofactory.cloud.blueprint.domain; + +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; +import com.kumofactory.cloud.blueprint.dto.ComponentLineDto; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import javax.persistence.*; +import java.util.Date; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class ComponentLine { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + private String ui_id; // Client 에서 생성하는 uuid + @OneToOne(cascade = CascadeType.ALL) + private ComponentDot source; + @OneToOne(cascade = CascadeType.ALL) + private ComponentDot destination; + @ManyToOne + private AwsBluePrint bluePrint; + + public static ComponentLine createComponentLink(ComponentLineDto lineDto, AwsBluePrint bluePrint) { + ComponentLine componentLink = new ComponentLine(); + componentLink.setUi_id(lineDto.getId()); + componentLink.setSource(ComponentDot.createComponentDot(lineDto.getSrc())); + componentLink.setDestination(ComponentDot.createComponentDot(lineDto.getDst())); + componentLink.setBluePrint(bluePrint); + return componentLink; + } +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java deleted file mode 100644 index 4240aba..0000000 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentPoint.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.kumofactory.cloud.blueprint.domain; - -import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; -import com.kumofactory.cloud.blueprint.dto.ComponentPointDto; -import java.util.Date; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import org.hibernate.annotations.CreationTimestamp; -import org.hibernate.annotations.UpdateTimestamp; - -@Entity -@Getter -@Setter -@NoArgsConstructor -public class ComponentPoint { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @CreationTimestamp - private Date created_at; - @UpdateTimestamp - private Date updated_at; - - @Column(unique = true) - private String ui_id; // Client 에서 생성하는 uuid - - // Component 의 찍힌 point 위치 - private Integer position_x; - private Integer position_y; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "csp_component_id") - private AwsComponent cspComponent; - - // ============== 생성함수 ================= // - public static ComponentPoint createComponentPoint(ComponentPointDto pointLinkDto, - AwsComponent cspComponent) { - ComponentPoint componentPoint = new ComponentPoint(); - componentPoint.setUi_id(pointLinkDto.getId()); - componentPoint.setPosition_x(pointLinkDto.getX()); - componentPoint.setPosition_y(pointLinkDto.getY()); - componentPoint.setCspComponent(cspComponent); - return componentPoint; - } -} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java deleted file mode 100644 index 1b304cf..0000000 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/PointLink.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.kumofactory.cloud.blueprint.domain; - -import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; -import com.kumofactory.cloud.blueprint.dto.PointLinkDto; -import java.util.Date; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToOne; -import javax.persistence.OneToOne; -import javax.persistence.Table; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import org.hibernate.annotations.CreationTimestamp; -import org.hibernate.annotations.UpdateTimestamp; - -@Entity -@Getter -@Setter -@NoArgsConstructor -@Table -public class PointLink { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @CreationTimestamp - private Date created_at; - @UpdateTimestamp - private Date updated_at; - - @Column(unique = true) - private String ui_id; // Client 에서 생성하는 uuid - - private String source_id; - private String destination_id; - - @ManyToOne - private AwsBluePrint bluePrint; - - // ============== 생성함수 ================= // - public static PointLink createPointLink(PointLinkDto pointLinkDto, AwsBluePrint bluePrint) { - PointLink pointLink = new PointLink(); - pointLink.setUi_id(pointLinkDto.getId()); - pointLink.setSource_id(pointLinkDto.getSource()); - pointLink.setDestination_id(pointLinkDto.getTarget()); - pointLink.setBluePrint(bluePrint); - return pointLink; - } -} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsBluePrint.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsBluePrint.java index 4d87a0f..d520931 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsBluePrint.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsBluePrint.java @@ -1,6 +1,8 @@ package com.kumofactory.cloud.blueprint.domain.aws; +import com.kumofactory.cloud.blueprint.domain.ComponentLine; import com.kumofactory.cloud.member.domain.Member; + import java.util.Date; import java.util.List; import javax.persistence.CascadeType; @@ -11,6 +13,7 @@ import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; + import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -40,5 +43,7 @@ public class AwsBluePrint { @OneToMany(mappedBy = "bluePrint", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY) private List cspComponents; + @OneToMany(mappedBy = "bluePrint", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY) + private List lines; } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java index 0e43678..f242204 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponent.java @@ -1,6 +1,5 @@ package com.kumofactory.cloud.blueprint.domain.aws; -import com.kumofactory.cloud.blueprint.domain.ComponentPoint; import com.kumofactory.cloud.blueprint.dto.aws.AwsComponentDto; import java.util.Date; @@ -11,7 +10,6 @@ import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.CreationTimestamp; -import org.hibernate.annotations.Fetch; import org.hibernate.annotations.UpdateTimestamp; @Entity @@ -20,40 +18,37 @@ @NoArgsConstructor public class AwsComponent { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @CreationTimestamp - private Date created_at; - @UpdateTimestamp - private Date updated_at; - - @Column(unique = true) - private String ui_id; // Client 에서 생성하는 uuid - - @Enumerated(EnumType.STRING) - private AwsComponentType type; // Component 타입 (vm, vpc, subnet, ...) - - // Component 의 좌측 상단 좌표 - private Integer position_x; - private Integer position_y; - - @ManyToOne - private AwsBluePrint bluePrint; - - @OneToMany(mappedBy = "cspComponent", cascade = CascadeType.ALL, fetch = FetchType.EAGER) - private List componentPoint; - - // ============== 생성함수 ================= // - public static AwsComponent createAwsComponent(AwsComponentDto awsComponentDto, - AwsBluePrint awsBluePrint) { - AwsComponent awsComponent = new AwsComponent(); - awsComponent.setUi_id(awsComponentDto.getId()); - awsComponent.setPosition_x(awsComponentDto.getX()); - awsComponent.setPosition_y(awsComponentDto.getY()); - awsComponent.setType(awsComponentDto.getType()); - awsComponent.setBluePrint(awsBluePrint); - return awsComponent; - } + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; + + @Column(unique = true) + private String ui_id; // Client 에서 생성하는 uuid + + @Enumerated(EnumType.STRING) + private AwsComponentType type; // Component 타입 (vm, vpc, subnet, ...) + + // Component 의 좌측 상단 좌표 + private Integer position_x; + private Integer position_y; + + @ManyToOne + private AwsBluePrint bluePrint; + + // ============== 생성함수 ================= // + public static AwsComponent createAwsComponent(AwsComponentDto awsComponentDto, + AwsBluePrint awsBluePrint) { + AwsComponent awsComponent = new AwsComponent(); + awsComponent.setUi_id(awsComponentDto.getId()); + awsComponent.setPosition_x(awsComponentDto.getX()); + awsComponent.setPosition_y(awsComponentDto.getY()); + awsComponent.setType(awsComponentDto.getType()); + awsComponent.setBluePrint(awsBluePrint); + return awsComponent; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java index 9fb122c..e5221ad 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java @@ -1,5 +1,5 @@ package com.kumofactory.cloud.blueprint.domain.aws; public enum AwsComponentType { - VPC, SUBNET, ELB, EC2, EFS, WAF, ROUTE53, NAT_GATEWAY, RDS + VPC, SUBNET, ELB, EC2, EFS, WAF, ROUTE53, NAT_GATEWAY, RDS, } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentDotDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentDotDto.java new file mode 100644 index 0000000..c9cb93e --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentDotDto.java @@ -0,0 +1,21 @@ +package com.kumofactory.cloud.blueprint.dto; + +import com.kumofactory.cloud.blueprint.domain.ComponentDot; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ComponentDotDto { + private Integer x; + private Integer y; + private String componentId; + + public static ComponentDotDto mapper(ComponentDot componentDot) { + ComponentDotDto componentDotDto = new ComponentDotDto(); + componentDotDto.setX(componentDot.getX()); + componentDotDto.setY(componentDot.getY()); + componentDotDto.setComponentId(componentDot.getComponentId()); + return componentDotDto; + } +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentLineDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentLineDto.java new file mode 100644 index 0000000..7c5f3f0 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentLineDto.java @@ -0,0 +1,21 @@ +package com.kumofactory.cloud.blueprint.dto; + +import com.kumofactory.cloud.blueprint.domain.ComponentLine; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ComponentLineDto { + private String id; // Client 에서 생성하는 uuid + private ComponentDotDto src; + private ComponentDotDto dst; + + public static ComponentLineDto mapper(ComponentLine componentLink) { + ComponentLineDto componentLinkDto = new ComponentLineDto(); + componentLinkDto.setId(componentLink.getUi_id()); + componentLinkDto.setSrc(ComponentDotDto.mapper(componentLink.getSource())); + componentLinkDto.setDst(ComponentDotDto.mapper(componentLink.getDestination())); + return componentLinkDto; + } +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java deleted file mode 100644 index b615e3e..0000000 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/ComponentPointDto.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.kumofactory.cloud.blueprint.dto; - -import javax.validation.constraints.NotNull; - -import com.kumofactory.cloud.blueprint.domain.ComponentPoint; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class ComponentPointDto { - - @NotNull - private String id; - @NotNull - private Integer x; - @NotNull - private Integer y; - - public static ComponentPointDto mapper(ComponentPoint componentPoint) { - ComponentPointDto componentPointDto = new ComponentPointDto(); - componentPointDto.setId(componentPoint.getUi_id()); - componentPointDto.setX(componentPoint.getPosition_x()); - componentPointDto.setY(componentPoint.getPosition_y()); - return componentPointDto; - } -} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java deleted file mode 100644 index f3d8840..0000000 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/PointLinkDto.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.kumofactory.cloud.blueprint.dto; - -import javax.validation.constraints.NotNull; - -import com.kumofactory.cloud.blueprint.domain.PointLink; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class PointLinkDto { - - @NotNull - private String id; - // Component uuid 값 - @NotNull - private String source; - @NotNull - private String target; - - public static PointLinkDto mapper(PointLink pointLink) { - PointLinkDto pointLinkDto = new PointLinkDto(); - pointLinkDto.setId(pointLink.getUi_id()); - pointLinkDto.setSource(pointLink.getSource_id()); - pointLinkDto.setTarget(pointLink.getDestination_id()); - return pointLinkDto; - } -} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java index f232d28..91d662e 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -1,36 +1,35 @@ package com.kumofactory.cloud.blueprint.dto.aws; -import com.kumofactory.cloud.blueprint.domain.PointLink; +import com.kumofactory.cloud.blueprint.domain.ComponentLine; import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; -import com.kumofactory.cloud.blueprint.dto.PointLinkDto; +import com.kumofactory.cloud.blueprint.dto.ComponentLineDto; import java.util.ArrayList; import java.util.List; import lombok.Data; -import lombok.Getter; @Data public class AwsBluePrintDto { - private String name; - private List components; - private List links; - - public static List awsComponentDtosMapper(List awsComponentDtos) { - List awsComponentDtoList = new ArrayList<>(); - for (AwsComponent awsComponent : awsComponentDtos) { - awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); - } - return awsComponentDtoList; - } - - public static List pointLinkDtosMapper(List pointLinks) { - List pointLinkDtoList = new ArrayList<>(); - for (PointLink pointLink : pointLinks) { - pointLinkDtoList.add(PointLinkDto.mapper(pointLink)); - } - return pointLinkDtoList; - } + private String name; + private List components; + private List links; + + public static List awsComponentDtosMapper(List awsComponentDtos) { + List awsComponentDtoList = new ArrayList<>(); + for (AwsComponent awsComponent : awsComponentDtos) { + awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); + } + return awsComponentDtoList; + } + + public static List componentLinkDtoListMapper(List componentLinks) { + List componentLinkDtoList = new ArrayList<>(); + for (ComponentLine pointLink : componentLinks) { + componentLinkDtoList.add(ComponentLineDto.mapper(pointLink)); + } + return componentLinkDtoList; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java index cb28f67..8905048 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsComponentDto.java @@ -1,47 +1,35 @@ package com.kumofactory.cloud.blueprint.dto.aws; -import com.kumofactory.cloud.blueprint.domain.ComponentPoint; import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; import com.kumofactory.cloud.blueprint.domain.aws.AwsComponentType; -import com.kumofactory.cloud.blueprint.dto.ComponentPointDto; -import java.util.ArrayList; -import java.util.List; import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; -import org.springframework.lang.Nullable; @Getter @Setter public class AwsComponentDto { - @NotNull - private String id; - - @NotNull - private Integer x; - - @NotNull - private Integer y; - - @NotNull - private AwsComponentType type; - - private List points; - - public static AwsComponentDto mapper(AwsComponent awsComponent) { - AwsComponentDto awsComponentDto = new AwsComponentDto(); - awsComponentDto.setId(awsComponent.getUi_id()); - awsComponentDto.setX(awsComponent.getPosition_x()); - awsComponentDto.setY(awsComponent.getPosition_y()); - awsComponentDto.setType(awsComponent.getType()); - List points = new ArrayList<>(); - for (ComponentPoint componentPoint : awsComponent.getComponentPoint()) { - points.add(ComponentPointDto.mapper(componentPoint)); - } - awsComponentDto.setPoints(points); - return awsComponentDto; - } + @NotNull + private String id; + + @NotNull + private Integer x; + + @NotNull + private Integer y; + + @NotNull + private AwsComponentType type; + + public static AwsComponentDto mapper(AwsComponent awsComponent) { + AwsComponentDto awsComponentDto = new AwsComponentDto(); + awsComponentDto.setId(awsComponent.getUi_id()); + awsComponentDto.setX(awsComponent.getPosition_x()); + awsComponentDto.setY(awsComponent.getPosition_y()); + awsComponentDto.setType(awsComponent.getType()); + return awsComponentDto; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentDotRepository.java similarity index 54% rename from src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java rename to src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentDotRepository.java index 072fe45..fdfbb19 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentPointRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentDotRepository.java @@ -1,10 +1,9 @@ package com.kumofactory.cloud.blueprint.repository; -import com.kumofactory.cloud.blueprint.domain.ComponentPoint; +import com.kumofactory.cloud.blueprint.domain.ComponentDot; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface ComponentPointRepository extends JpaRepository { - +public interface ComponentDotRepository extends JpaRepository { } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentLineRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentLineRepository.java new file mode 100644 index 0000000..94fccd1 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/ComponentLineRepository.java @@ -0,0 +1,13 @@ +package com.kumofactory.cloud.blueprint.repository; + +import com.kumofactory.cloud.blueprint.domain.ComponentLine; +import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ComponentLineRepository extends JpaRepository { + List findAllByBluePrint(AwsBluePrint bluePrint); +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java deleted file mode 100644 index f516ae4..0000000 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/PointLinkRepository.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.kumofactory.cloud.blueprint.repository; - -import com.kumofactory.cloud.blueprint.domain.PointLink; -import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; - -@Repository -@Transactional -public interface PointLinkRepository extends JpaRepository { - List findAllByBluePrint(AwsBluePrint bluePrint); -} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java index 72bbff8..3354151 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java @@ -4,9 +4,13 @@ import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; +import java.util.List; + public interface AwsBlueprintService { - AwsBluePrintDto getAwsBlueprint(); + AwsBluePrintDto getAwsBlueprint(); + + List getMyAwsBlueprints(); - void store(AwsBluePrintDto awsBluePrintDto); + void store(AwsBluePrintDto awsBluePrintDto); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java index 5588258..e1f712b 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java @@ -1,26 +1,21 @@ package com.kumofactory.cloud.blueprint.service; -import com.kumofactory.cloud.blueprint.domain.ComponentPoint; -import com.kumofactory.cloud.blueprint.domain.PointLink; +import com.kumofactory.cloud.blueprint.domain.ComponentLine; import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.domain.aws.AwsComponent; -import com.kumofactory.cloud.blueprint.dto.ComponentPointDto; -import com.kumofactory.cloud.blueprint.dto.PointLinkDto; +import com.kumofactory.cloud.blueprint.dto.ComponentLineDto; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; import com.kumofactory.cloud.blueprint.dto.aws.AwsComponentDto; -import com.kumofactory.cloud.blueprint.repository.ComponentPointRepository; -import com.kumofactory.cloud.blueprint.repository.PointLinkRepository; +import com.kumofactory.cloud.blueprint.repository.ComponentDotRepository; +import com.kumofactory.cloud.blueprint.repository.ComponentLineRepository; import com.kumofactory.cloud.blueprint.repository.aws.AwsBluePrintRepository; import com.kumofactory.cloud.blueprint.repository.aws.AwsComponentRepository; import com.kumofactory.cloud.member.MemberRepository; -import com.kumofactory.cloud.member.domain.Member; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; @@ -33,72 +28,63 @@ @Repository public class AwsBlueprintServiceImpl implements AwsBlueprintService { - private final MemberRepository memberRepository; - private final AwsBluePrintRepository awsBluePrintRepository; - private final AwsComponentRepository awsComponentRepository; - private final ComponentPointRepository componentPointRepository; - private final PointLinkRepository pointLinkRepository; - private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); + private final MemberRepository memberRepository; + private final AwsBluePrintRepository awsBluePrintRepository; + private final AwsComponentRepository awsComponentRepository; + private final ComponentLineRepository componentLineRepository; + private final ComponentDotRepository componentDotRepository; + private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); - @Override - public AwsBluePrintDto getAwsBlueprint() { - AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintById(1L); - List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); - List pointLinks = pointLinkRepository.findAllByBluePrint(awsBluePrintById); - logger.info("awsBluePrintById: {}", awsBluePrintById); - if (awsBluePrintById == null) { - throw new RuntimeException("awsBluePrintById is null"); - } - AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); - awsBluePrintDto.setName(awsBluePrintById.getName()); - awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); - awsBluePrintDto.setLinks(AwsBluePrintDto.pointLinkDtosMapper(pointLinks)); + @Override + public AwsBluePrintDto getAwsBlueprint() { + AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintById(1L); + logger.info("awsBluePrintById: {}", awsBluePrintById); + if (awsBluePrintById == null) { + throw new RuntimeException("awsBluePrintById is null"); + } - return awsBluePrintDto; - } + List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); + List componentLines = componentLineRepository.findAllByBluePrint(awsBluePrintById); + AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); + awsBluePrintDto.setName(awsBluePrintById.getName()); + awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); + awsBluePrintDto.setLinks(AwsBluePrintDto.componentLinkDtoListMapper(componentLines)); + return awsBluePrintDto; + } - @Override - public void store(AwsBluePrintDto awsBluePrintDto) { + @Override + public List getMyAwsBlueprints() { + return null; + } + + @Override + public void store(AwsBluePrintDto awsBluePrintDto) { // Member member = memberRepository.findMemberById(1L); - // BluePrint 저장 - AwsBluePrint awsBluePrint = new AwsBluePrint(); - awsBluePrint.setName(awsBluePrintDto.getName()); + // BluePrint 저장 + AwsBluePrint awsBluePrint = new AwsBluePrint(); + awsBluePrint.setName(awsBluePrintDto.getName()); // awsBluePrint.setMember(member); - AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); - logger.info("savedBlueprint: {}", savedBlueprint); - - List components = new ArrayList<>(); - List links = awsBluePrintDto.getLinks(); - - for (AwsComponentDto component : awsBluePrintDto.getComponents()) { - AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); + AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); + logger.info("savedBlueprint: {}", savedBlueprint); - List componentPoints = new ArrayList<>(); - List points = component.getPoints(); - if (points.size() != 0) { - for (ComponentPointDto point : points) { - ComponentPoint componentPoint = ComponentPoint.createComponentPoint(point, - awsComponent); - componentPoints.add(componentPoint); - } - } - awsComponent.setComponentPoint(componentPoints); - components.add(awsComponent); - } - awsComponentRepository.saveAll(components); + List components = new ArrayList<>(); + List links = awsBluePrintDto.getLinks(); - // Component 저장 - if (links.size() != 0) { - // PointLink 저장 - List toSaveLink = new ArrayList<>(); - for (PointLinkDto link : links) { - PointLink pointLink = PointLink.createPointLink(link, savedBlueprint); - toSaveLink.add(pointLink); - } + // Components 저장 + for (AwsComponentDto component : awsBluePrintDto.getComponents()) { + AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); + components.add(awsComponent); + } + awsComponentRepository.saveAll(components); - pointLinkRepository.saveAll(toSaveLink); - } - } + // Lines 저장 + List componentLines = new ArrayList<>(); + for (ComponentLineDto link : links) { + ComponentLine componentLink = ComponentLine.createComponentLink(link, savedBlueprint); + componentLines.add(componentLink); + } + componentLineRepository.saveAll(componentLines); + } } From 035165d6f8711dd6d0bcdc1a8cdd996469d8b9c8 Mon Sep 17 00:00:00 2001 From: wook Date: Sun, 16 Jul 2023 15:39:50 +0900 Subject: [PATCH 13/20] feat: create authorization annotation --- .../middleware/AuthorizationFromToken.java | 4 + .../auth/AuthorizationFromToken.java | 11 ++ .../auth/AuthorizationFromTokenAspect.java | 63 +++++++++ .../infra/service/aws_cdk/Ec2Service.java | 8 ++ .../infra/service/aws_cdk/Ec2ServiceImpl.java | 43 ++++++ .../infra/service/aws_cdk/stack/Ec2Stack.java | 16 +++ .../cloud/jwt/provider/JwtTokenProvider.java | 131 ++++++++++-------- 7 files changed, 216 insertions(+), 60 deletions(-) create mode 100644 src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java create mode 100644 src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromToken.java create mode 100644 src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java create mode 100644 src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2Service.java create mode 100644 src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java create mode 100644 src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java b/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java new file mode 100644 index 0000000..37edd75 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java @@ -0,0 +1,4 @@ +package com.kumofactory.cloud.global.middleware; + +public class AuthorizationFromToken { +} diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromToken.java b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromToken.java new file mode 100644 index 0000000..7434ab6 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromToken.java @@ -0,0 +1,11 @@ +package com.kumofactory.cloud.global.middleware.auth; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +public @interface AuthorizationFromToken { +} diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java new file mode 100644 index 0000000..64f90a9 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java @@ -0,0 +1,63 @@ +package com.kumofactory.cloud.global.middleware.auth; + +import com.kumofactory.cloud.jwt.provider.JwtTokenProvider; +import lombok.RequiredArgsConstructor; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; + +@Aspect +@Component +@RequiredArgsConstructor +public class AuthorizationFromTokenAspect { + private final JwtTokenProvider jwtTokenProvider; + + @Around(value = "@annotation(AuthorizationFromToken)") + public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, AuthorizationFromToken AuthorizationFromToken) throws Throwable { + HttpServletRequest request = findHttpServletRequest(joinPoint.getArgs()); + HttpServletRequest header = findHttpServletRequest(joinPoint.getArgs()); + String accessToken = extractAccessTokenFromCookies(request.getCookies()); + String userId = getUserFromAccessToken(accessToken); + + // 추출된 사용자 정보를 매개변수로 전달 + Object[] args = joinPoint.getArgs(); + args[0] = userId; + + return joinPoint.proceed(args); + } + + private HttpServletRequest findHttpServletRequest(Object[] args) { + for (Object arg : args) { + if (arg instanceof HttpServletRequest) { + return (HttpServletRequest) arg; + } + } + throw new IllegalArgumentException("HttpServletRequest not found in method parameters"); + } + + private String extractAccessTokenFromCookies(Cookie[] cookies) { + if (cookies != null) { + return Arrays.stream(cookies) + .filter(cookie -> "access_token".equals(cookie.getName())) + .map(Cookie::getValue) + .findFirst() + .orElse(null); + } + return null; + } + + private String getUserFromAccessToken(String accessToken) { + boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); + if (isValidate) { + return jwtTokenProvider.getClaimsFormToken(accessToken).getSubject(); + } + return null; + } +} diff --git a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2Service.java b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2Service.java new file mode 100644 index 0000000..df46713 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2Service.java @@ -0,0 +1,8 @@ +package com.kumofactory.cloud.infra.service.aws_cdk; + +public interface Ec2Service { + + void createBastionHostLinux(); + + void createEc2Instance(); +} diff --git a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java new file mode 100644 index 0000000..e736e8c --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java @@ -0,0 +1,43 @@ +package com.kumofactory.cloud.infra.service.aws_cdk; + +import java.util.ArrayList; +import java.util.List; + +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import software.amazon.awscdk.services.ec2.BastionHostLinux; +import software.amazon.awscdk.services.ec2.BastionHostLinuxProps; +import software.amazon.awscdk.services.ec2.SubnetFilter; +import software.amazon.awscdk.services.ec2.SubnetSelection; +import software.amazon.awscdk.services.ec2.SubnetSelection.Builder; +import software.amazon.awscdk.services.ec2.Instance; + +/** + * AWS CDK를 이용한 EC2 서비스 + */ +@Service +@Slf4j +public class Ec2ServiceImpl implements Ec2Service { + + private final Logger logger = LoggerFactory.getLogger(Ec2ServiceImpl.class); + + @Override + public void createBastionHostLinux() { + logger.info("================ Create BastionHostLinux Instance ================"); + List subnetsId = new ArrayList<>(); + List subnetFilters = new ArrayList<>(); + subnetsId.add("subnet-043aec505f84e38ac"); + SubnetFilter subnetFilter = SubnetFilter.byIds(subnetsId); + subnetFilters.add(subnetFilter); + BastionHostLinuxProps.builder().subnetSelection(SubnetSelection.builder().subnetFilters(subnetFilters).build()); + } + + @Override + public void createEc2Instance() { + logger.info("================ Create EC2 Instance ================"); + + + } +} diff --git a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java new file mode 100644 index 0000000..1cdb2cd --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java @@ -0,0 +1,16 @@ +package com.kumofactory.cloud.infra.service.aws_cdk.stack; + +import software.amazon.awscdk.Stack; +import software.amazon.awscdk.services.ec2.ISubnet; +import software.amazon.awscdk.services.ec2.Subnet; +import software.amazon.awscdk.services.ec2.Vpc; +import software.amazon.awscdk.services.ec2.VpcLookupOptions; +import software.constructs.Construct; + +public class Ec2Stack extends Stack { + public Ec2Stack(final Construct scope, String id) { + super(scope, id); + Vpc vpc = (Vpc) Vpc.fromLookup(this, "KumofactoryVPC", VpcLookupOptions.builder().vpcId("vpc-0719a1184fa5ccdcd").build()); + ISubnet kumofactorySubnet = Subnet.fromSubnetId(this, "KumofactorySubnet", "subnet-043aec505f84e38ac"); + } +} diff --git a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java index 62684af..c29ffa5 100644 --- a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java +++ b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java @@ -1,11 +1,12 @@ package com.kumofactory.cloud.jwt.provider; import com.kumofactory.cloud.jwt.dto.TokenDto; -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.Jws; -import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.*; import io.jsonwebtoken.security.Keys; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -13,64 +14,74 @@ @Service @RequiredArgsConstructor +@Slf4j public class JwtTokenProvider { - - @Value("${jwt.secret}") - private String secret; - @Value("${jwt.token-validity-in-milliseconds}") - private long VALIDITY; - - public TokenDto create(String id) { - - Date now = new Date(); - - String accessToken = Jwts.builder() - .setSubject(id) - .setIssuedAt(now) - .setExpiration(new Date(now.getTime() + VALIDITY)) - .signWith(Keys.hmacShaKeyFor(secret.getBytes())) - .compact(); - - String refreshToken = Jwts.builder() - .setSubject(id) - .setIssuedAt(now) - .setExpiration(new Date(now.getTime() + (VALIDITY * 365))) - .signWith(Keys.hmacShaKeyFor(secret.getBytes())) - .compact(); - - return TokenDto.builder() - .userEmail(id) - .accessToken(accessToken) - .refreshToken(refreshToken) - .build(); - } - - public String getUserId(String token) { - return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody().getSubject(); - } - - public boolean validateAccessToken(String token) { - try { - Jws claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); - return !claims.getBody().getExpiration().before(new Date()); - } catch (Exception e) { - return false; - } - } - - public boolean validateRefreshToken(String token) { - try { - Jws claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); - return !claims.getBody().getExpiration().before(new Date()); - } catch (Exception e) { - return false; - } - } - - public String refreshAccessToken() { - - return null; - } + private final Logger logger = LoggerFactory.getLogger(JwtTokenProvider.class); + + @Value("${jwt.secret}") + private String secret; + @Value("${jwt.token-validity-in-milliseconds}") + private long VALIDITY; + + public TokenDto create(String id) { + + Date now = new Date(); + + String accessToken = Jwts.builder() + .setSubject(id) + .setIssuedAt(now) + .setExpiration(new Date(now.getTime() + VALIDITY)) + .signWith(Keys.hmacShaKeyFor(secret.getBytes())) + .compact(); + + String refreshToken = Jwts.builder() + .setSubject(id) + .setIssuedAt(now) + .setExpiration(new Date(now.getTime() + (VALIDITY * 365))) + .signWith(Keys.hmacShaKeyFor(secret.getBytes())) + .compact(); + + return TokenDto.builder() + .userEmail(id) + .accessToken(accessToken) + .refreshToken(refreshToken) + .build(); + } + + public boolean validateAccessToken(String token) { + try { + Claims claims = getClaimsFormToken(token); + return true; + } catch (ExpiredJwtException exception) { + logger.error("Token Expired"); + throw new ExpiredJwtException(exception.getHeader(), exception.getClaims(), exception.getMessage()); + } catch (JwtException exception) { + logger.error("Token Tampered"); + return new JwtException(exception.getMessage()).getMessage().equals(exception.getMessage()); + } catch (NullPointerException exception) { + logger.error("Token is null"); + return new NullPointerException().getMessage().equals(exception.getMessage()); + } + } + + public boolean validateRefreshToken(String token) { + try { + Jws claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); + return !claims.getBody().getExpiration().before(new Date()); + } catch (Exception e) { + return false; + } + } + + public String refreshAccessToken(Claims claims) { + String id = claims.getSubject(); + TokenDto tokenDto = create(id); + return null; + } + + public Claims getClaimsFormToken(String token) { + return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody(); + } } From 90363cebd85f2344c8ed726822f836811e2e7e1e Mon Sep 17 00:00:00 2001 From: wook Date: Sun, 16 Jul 2023 15:40:14 +0900 Subject: [PATCH 14/20] feat : add createMember function --- .../com/kumofactory/cloud/member/domain/Member.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/kumofactory/cloud/member/domain/Member.java b/src/main/java/com/kumofactory/cloud/member/domain/Member.java index f4bed73..ab53028 100644 --- a/src/main/java/com/kumofactory/cloud/member/domain/Member.java +++ b/src/main/java/com/kumofactory/cloud/member/domain/Member.java @@ -9,6 +9,7 @@ import javax.persistence.Id; import javax.persistence.OneToMany; +import com.kumofactory.cloud.oauth.dto.UserInfoDto; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -23,8 +24,18 @@ public class Member { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - private Long social_id; + private String oauth_id; + + private String provider; @OneToMany(mappedBy = "member") private List bluePrints; + + // =========== 생성함수 =========== // + public static Member createMember(UserInfoDto userInfoDto) { + Member member = new Member(); + member.setOauth_id(userInfoDto.id()); + member.setProvider(userInfoDto.provider()); + return member; + } } From 51fe8b39f667051d080adb1559eb36af64389ba7 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Sun, 16 Jul 2023 19:51:03 +0900 Subject: [PATCH 15/20] feat : add annotation authorizing user through access token --- .../middleware/AuthorizationFromToken.java | 4 - .../auth/AuthorizationFromTokenAspect.java | 74 +++++------- .../infra/service/aws_cdk/stack/Ec2Stack.java | 16 --- .../cloud/jwt/provider/JwtTokenProvider.java | 112 +++++++++--------- 4 files changed, 87 insertions(+), 119 deletions(-) delete mode 100644 src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java delete mode 100644 src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java b/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java deleted file mode 100644 index 37edd75..0000000 --- a/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.kumofactory.cloud.global.middleware; - -public class AuthorizationFromToken { -} diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java index 64f90a9..6030539 100644 --- a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java +++ b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java @@ -2,62 +2,50 @@ import com.kumofactory.cloud.jwt.provider.JwtTokenProvider; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; +import java.util.Objects; @Aspect @Component @RequiredArgsConstructor +@Slf4j public class AuthorizationFromTokenAspect { - private final JwtTokenProvider jwtTokenProvider; - - @Around(value = "@annotation(AuthorizationFromToken)") - public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, AuthorizationFromToken AuthorizationFromToken) throws Throwable { - HttpServletRequest request = findHttpServletRequest(joinPoint.getArgs()); - HttpServletRequest header = findHttpServletRequest(joinPoint.getArgs()); - String accessToken = extractAccessTokenFromCookies(request.getCookies()); - String userId = getUserFromAccessToken(accessToken); - - // 추출된 사용자 정보를 매개변수로 전달 - Object[] args = joinPoint.getArgs(); - args[0] = userId; - - return joinPoint.proceed(args); - } - - private HttpServletRequest findHttpServletRequest(Object[] args) { - for (Object arg : args) { - if (arg instanceof HttpServletRequest) { - return (HttpServletRequest) arg; - } - } - throw new IllegalArgumentException("HttpServletRequest not found in method parameters"); - } - - private String extractAccessTokenFromCookies(Cookie[] cookies) { - if (cookies != null) { - return Arrays.stream(cookies) - .filter(cookie -> "access_token".equals(cookie.getName())) - .map(Cookie::getValue) - .findFirst() - .orElse(null); - } - return null; - } - - private String getUserFromAccessToken(String accessToken) { - boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); - if (isValidate) { - return jwtTokenProvider.getClaimsFormToken(accessToken).getSubject(); - } - return null; - } + private final JwtTokenProvider jwtTokenProvider; + private final Logger logger = LoggerFactory.getLogger(AuthorizationFromTokenAspect.class); + + @Around(value = "@annotation(AuthorizationFromToken)") + public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, AuthorizationFromToken AuthorizationFromToken) throws Throwable { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); + String userId = getUserFromAccessToken(request.getHeader("Authorization")); + + // 추출된 사용자 정보를 매개변수로 전달 + Object[] args = joinPoint.getArgs(); + args[0] = userId; + + return joinPoint.proceed(args); + } + + private String getUserFromAccessToken(String token) { + String accessToken = token.split(" ")[1]; + boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); + if (isValidate) { + return jwtTokenProvider.getClaimsFormToken(accessToken).getSubject(); + } + return null; + } } diff --git a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java deleted file mode 100644 index 1cdb2cd..0000000 --- a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.kumofactory.cloud.infra.service.aws_cdk.stack; - -import software.amazon.awscdk.Stack; -import software.amazon.awscdk.services.ec2.ISubnet; -import software.amazon.awscdk.services.ec2.Subnet; -import software.amazon.awscdk.services.ec2.Vpc; -import software.amazon.awscdk.services.ec2.VpcLookupOptions; -import software.constructs.Construct; - -public class Ec2Stack extends Stack { - public Ec2Stack(final Construct scope, String id) { - super(scope, id); - Vpc vpc = (Vpc) Vpc.fromLookup(this, "KumofactoryVPC", VpcLookupOptions.builder().vpcId("vpc-0719a1184fa5ccdcd").build()); - ISubnet kumofactorySubnet = Subnet.fromSubnetId(this, "KumofactorySubnet", "subnet-043aec505f84e38ac"); - } -} diff --git a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java index c29ffa5..91a1484 100644 --- a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java +++ b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java @@ -16,72 +16,72 @@ @RequiredArgsConstructor @Slf4j public class JwtTokenProvider { - private final Logger logger = LoggerFactory.getLogger(JwtTokenProvider.class); + private final Logger logger = LoggerFactory.getLogger(JwtTokenProvider.class); - @Value("${jwt.secret}") - private String secret; - @Value("${jwt.token-validity-in-milliseconds}") - private long VALIDITY; + @Value("${jwt.secret}") + private String secret; + @Value("${jwt.token-validity-in-milliseconds}") + private long VALIDITY; - public TokenDto create(String id) { + public TokenDto create(String id) { - Date now = new Date(); + Date now = new Date(); - String accessToken = Jwts.builder() - .setSubject(id) - .setIssuedAt(now) - .setExpiration(new Date(now.getTime() + VALIDITY)) - .signWith(Keys.hmacShaKeyFor(secret.getBytes())) - .compact(); + String accessToken = Jwts.builder() + .setSubject(id) + .setIssuedAt(now) + .setExpiration(new Date(now.getTime() + VALIDITY)) + .signWith(Keys.hmacShaKeyFor(secret.getBytes())) + .compact(); - String refreshToken = Jwts.builder() - .setSubject(id) - .setIssuedAt(now) - .setExpiration(new Date(now.getTime() + (VALIDITY * 365))) - .signWith(Keys.hmacShaKeyFor(secret.getBytes())) - .compact(); + String refreshToken = Jwts.builder() + .setSubject(id) + .setIssuedAt(now) + .setExpiration(new Date(now.getTime() + (VALIDITY * 365))) + .signWith(Keys.hmacShaKeyFor(secret.getBytes())) + .compact(); - return TokenDto.builder() - .userEmail(id) - .accessToken(accessToken) - .refreshToken(refreshToken) - .build(); - } + return TokenDto.builder() + .userEmail(id) + .accessToken(accessToken) + .refreshToken(refreshToken) + .build(); + } - public boolean validateAccessToken(String token) { - try { - Claims claims = getClaimsFormToken(token); - return true; - } catch (ExpiredJwtException exception) { - logger.error("Token Expired"); - throw new ExpiredJwtException(exception.getHeader(), exception.getClaims(), exception.getMessage()); - } catch (JwtException exception) { - logger.error("Token Tampered"); - return new JwtException(exception.getMessage()).getMessage().equals(exception.getMessage()); - } catch (NullPointerException exception) { - logger.error("Token is null"); - return new NullPointerException().getMessage().equals(exception.getMessage()); - } - } + public boolean validateAccessToken(String token) { + try { + Claims claims = getClaimsFormToken(token); + return true; + } catch (ExpiredJwtException exception) { + logger.error("Token Expired"); + throw new ExpiredJwtException(exception.getHeader(), exception.getClaims(), exception.getMessage()); + } catch (JwtException exception) { + logger.error("Token Tampered"); + return new JwtException(exception.getMessage()).getMessage().equals(exception.getMessage()); + } catch (NullPointerException exception) { + logger.error("Token is null"); + return new NullPointerException().getMessage().equals(exception.getMessage()); + } + } - public boolean validateRefreshToken(String token) { - try { - Jws claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); - return !claims.getBody().getExpiration().before(new Date()); - } catch (Exception e) { - return false; - } - } + public boolean validateRefreshToken(String token) { + try { + Jws claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); + return !claims.getBody().getExpiration().before(new Date()); + } catch (Exception e) { + return false; + } + } - public String refreshAccessToken(Claims claims) { - String id = claims.getSubject(); - TokenDto tokenDto = create(id); - return null; - } + public String refreshAccessToken(Claims claims) { + String id = claims.getSubject(); + TokenDto tokenDto = create(id); + return null; + } - public Claims getClaimsFormToken(String token) { - return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody(); - } + public Claims getClaimsFormToken(String token) { + return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody(); + } } From a3b387950c59802e231da7429cbdfa83ca8794e7 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Sun, 16 Jul 2023 20:18:28 +0900 Subject: [PATCH 16/20] feat : add get blueprint from id, get blueprint list from userid --- .../cloud/blueprint/BlueprintController.java | 55 ++++++--- .../aws/AwsBluePrintRepository.java | 7 ++ .../service/AwsBlueprintService.java | 6 +- .../service/AwsBlueprintServiceImpl.java | 110 ++++++++++-------- 4 files changed, 107 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java index 9dafc31..ca906a3 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java @@ -3,36 +3,53 @@ import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; import com.kumofactory.cloud.blueprint.service.AwsBlueprintService; +import com.kumofactory.cloud.global.middleware.auth.AuthorizationFromToken; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequiredArgsConstructor @RequestMapping("/blueprint") @Slf4j public class BlueprintController { - private final Logger logger = LoggerFactory.getLogger(BlueprintController.class); - private final AwsBlueprintService awsBlueprintService; + private final Logger logger = LoggerFactory.getLogger(BlueprintController.class); + private final AwsBlueprintService awsBlueprintService; + + @GetMapping("/aws/{id}") + @AuthorizationFromToken + public AwsBluePrintDto getAwsBlueprint(@PathVariable("id") Long id) { + try { + logger.info("aws blue print id: {}", id); + AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(id); + return awsBlueprint; + } catch (RuntimeException e) { + return null; + } + } + + @GetMapping("/aws/list") + @AuthorizationFromToken + public List getAwsBlueprintList(String userId) { + logger.info("userId: {}", userId); + List awsBlueprintList = awsBlueprintService.getMyAwsBlueprints(userId); + return awsBlueprintList; + } - // TODO : 토큰에서 유저 정보 가져오는 로직 추가 - // TODO : blueprint list 주는 api 추가 - // TODO : blueprint id 값으로 blueprint 가져오는 api 추가 - @GetMapping("/aws") - public AwsBluePrintDto getAwsBlueprint() { - try { - AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(); - return awsBlueprint; - } catch (RuntimeException e) { - return null; - } - } + @PostMapping("/aws") + public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { + awsBlueprintService.store(awsBluePrintDto); + return "hello-world"; + } - @PostMapping("/aws") - public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { - awsBlueprintService.store(awsBluePrintDto); - return "hello-world"; - } + @GetMapping("/test") + @AuthorizationFromToken + public String testMiddleware(String userId) { + System.out.printf("userId: %s\n", userId); + return userId; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java index 8bb149f..c8ddacf 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java @@ -1,10 +1,17 @@ package com.kumofactory.cloud.blueprint.repository.aws; import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; +import com.kumofactory.cloud.member.domain.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AwsBluePrintRepository extends JpaRepository { AwsBluePrint findAwsBluePrintById(long id); + + AwsBluePrint findAwsBluePrintByMemberId(long memberId); + + List findAwsBluePrintsByMember(Member member); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java index 3354151..a736dc2 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java @@ -8,9 +8,9 @@ public interface AwsBlueprintService { - AwsBluePrintDto getAwsBlueprint(); + AwsBluePrintDto getAwsBlueprint(Long bluePrintId); - List getMyAwsBlueprints(); + List getMyAwsBlueprints(String userId); - void store(AwsBluePrintDto awsBluePrintDto); + void store(AwsBluePrintDto awsBluePrintDto); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java index e1f712b..271f8d5 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java @@ -12,6 +12,7 @@ import com.kumofactory.cloud.blueprint.repository.aws.AwsBluePrintRepository; import com.kumofactory.cloud.blueprint.repository.aws.AwsComponentRepository; import com.kumofactory.cloud.member.MemberRepository; +import com.kumofactory.cloud.member.domain.Member; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; @@ -28,63 +29,74 @@ @Repository public class AwsBlueprintServiceImpl implements AwsBlueprintService { - private final MemberRepository memberRepository; - private final AwsBluePrintRepository awsBluePrintRepository; - private final AwsComponentRepository awsComponentRepository; - private final ComponentLineRepository componentLineRepository; - private final ComponentDotRepository componentDotRepository; - private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); + private final MemberRepository memberRepository; + private final AwsBluePrintRepository awsBluePrintRepository; + private final AwsComponentRepository awsComponentRepository; + private final ComponentLineRepository componentLineRepository; + private final ComponentDotRepository componentDotRepository; + private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); - @Override - public AwsBluePrintDto getAwsBlueprint() { - AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintById(1L); - logger.info("awsBluePrintById: {}", awsBluePrintById); - if (awsBluePrintById == null) { - throw new RuntimeException("awsBluePrintById is null"); - } + @Override + public AwsBluePrintDto getAwsBlueprint(Long bluePrintId) { + AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintByMemberId(bluePrintId); + if (awsBluePrintById == null) { + throw new RuntimeException("awsBluePrintById is null"); + } - List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); - List componentLines = componentLineRepository.findAllByBluePrint(awsBluePrintById); - AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); - awsBluePrintDto.setName(awsBluePrintById.getName()); - awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); - awsBluePrintDto.setLinks(AwsBluePrintDto.componentLinkDtoListMapper(componentLines)); - return awsBluePrintDto; - } + List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); + List componentLines = componentLineRepository.findAllByBluePrint(awsBluePrintById); + AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); + awsBluePrintDto.setId(awsBluePrintById.getId()); + awsBluePrintDto.setName(awsBluePrintById.getName()); + awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); + awsBluePrintDto.setLinks(AwsBluePrintDto.componentLinkDtoListMapper(componentLines)); + return awsBluePrintDto; + } - @Override - public List getMyAwsBlueprints() { - return null; - } + @Override + public List getMyAwsBlueprints(String oauthId) { + Member member = memberRepository.findMemberByOauthId(oauthId); + if (member == null) { + throw new RuntimeException("member is null"); + } - @Override - public void store(AwsBluePrintDto awsBluePrintDto) { -// Member member = memberRepository.findMemberById(1L); + List awsBluePrints = awsBluePrintRepository.findAwsBluePrintsByMember(member); + List awsBluePrintDtos = new ArrayList<>(); + for (AwsBluePrint awsBluePrint : awsBluePrints) { + AwsBluePrintDto dto = new AwsBluePrintDto(); + dto.setId(awsBluePrint.getId()); + dto.setName(awsBluePrint.getName()); + awsBluePrintDtos.add(dto); + } + return awsBluePrintDtos; + } - // BluePrint 저장 - AwsBluePrint awsBluePrint = new AwsBluePrint(); - awsBluePrint.setName(awsBluePrintDto.getName()); + @Override + public void store(AwsBluePrintDto awsBluePrintDto) { + // BluePrint 저장 + AwsBluePrint awsBluePrint = new AwsBluePrint(); + awsBluePrint.setName(awsBluePrintDto.getName()); // awsBluePrint.setMember(member); - AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); - logger.info("savedBlueprint: {}", savedBlueprint); + AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); + logger.info("savedBlueprint: {}", savedBlueprint); - List components = new ArrayList<>(); - List links = awsBluePrintDto.getLinks(); + List components = new ArrayList<>(); + List links = awsBluePrintDto.getLinks(); - // Components 저장 - for (AwsComponentDto component : awsBluePrintDto.getComponents()) { - AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); - components.add(awsComponent); - } - awsComponentRepository.saveAll(components); + // Components 저장 + for (AwsComponentDto component : awsBluePrintDto.getComponents()) { + AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); + components.add(awsComponent); + } + awsComponentRepository.saveAll(components); - // Lines 저장 - List componentLines = new ArrayList<>(); - for (ComponentLineDto link : links) { - ComponentLine componentLink = ComponentLine.createComponentLink(link, savedBlueprint); - componentLines.add(componentLink); - } - componentLineRepository.saveAll(componentLines); - } + // Lines 저장 + List componentLines = new ArrayList<>(); + for (ComponentLineDto link : links) { + ComponentLine componentLink = ComponentLine.createComponentLink(link, savedBlueprint); + componentLines.add(componentLink); + } + componentLineRepository.saveAll(componentLines); + } } From 659d2507f9e4b9f975f2d80ed464c2f84cb8e224 Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Sun, 16 Jul 2023 20:19:10 +0900 Subject: [PATCH 17/20] =?UTF-8?q?feat=20:=20AuthorizationFromTokenAspect?= =?UTF-8?q?=20=EC=97=90=EB=9F=AC=20=ED=95=B8=EB=93=A4=EB=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blueprint/dto/aws/AwsBluePrintDto.java | 39 +++-- .../auth/AuthorizationFromTokenAspect.java | 7 +- .../cloud/member/MemberRepository.java | 4 +- .../cloud/member/domain/Member.java | 28 +-- .../cloud/oauth/service/OAuthService.java | 162 ++++++++++-------- 5 files changed, 129 insertions(+), 111 deletions(-) diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java index 91d662e..b4a9b87 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -13,23 +13,24 @@ @Data public class AwsBluePrintDto { - private String name; - private List components; - private List links; - - public static List awsComponentDtosMapper(List awsComponentDtos) { - List awsComponentDtoList = new ArrayList<>(); - for (AwsComponent awsComponent : awsComponentDtos) { - awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); - } - return awsComponentDtoList; - } - - public static List componentLinkDtoListMapper(List componentLinks) { - List componentLinkDtoList = new ArrayList<>(); - for (ComponentLine pointLink : componentLinks) { - componentLinkDtoList.add(ComponentLineDto.mapper(pointLink)); - } - return componentLinkDtoList; - } + private Long id; + private String name; + private List components; + private List links; + + public static List awsComponentDtosMapper(List awsComponentDtos) { + List awsComponentDtoList = new ArrayList<>(); + for (AwsComponent awsComponent : awsComponentDtos) { + awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); + } + return awsComponentDtoList; + } + + public static List componentLinkDtoListMapper(List componentLinks) { + List componentLinkDtoList = new ArrayList<>(); + for (ComponentLine pointLink : componentLinks) { + componentLinkDtoList.add(ComponentLineDto.mapper(pointLink)); + } + return componentLinkDtoList; + } } diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java index 6030539..dc45521 100644 --- a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java +++ b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java @@ -32,14 +32,17 @@ public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, Authori ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); String userId = getUserFromAccessToken(request.getHeader("Authorization")); - + if (userId == null) { + throw new RuntimeException("유효하지 않은 토큰입니다."); + } // 추출된 사용자 정보를 매개변수로 전달 Object[] args = joinPoint.getArgs(); args[0] = userId; return joinPoint.proceed(args); } - + + // user oauth id 추출 private String getUserFromAccessToken(String token) { String accessToken = token.split(" ")[1]; boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); diff --git a/src/main/java/com/kumofactory/cloud/member/MemberRepository.java b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java index 594b50e..9e0024f 100644 --- a/src/main/java/com/kumofactory/cloud/member/MemberRepository.java +++ b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java @@ -6,5 +6,7 @@ @Repository public interface MemberRepository extends JpaRepository { - Member findMemberById(Long id); + Member findMemberById(Long id); + + Member findMemberByOauthId(String oauth_id); } diff --git a/src/main/java/com/kumofactory/cloud/member/domain/Member.java b/src/main/java/com/kumofactory/cloud/member/domain/Member.java index ab53028..0055d94 100644 --- a/src/main/java/com/kumofactory/cloud/member/domain/Member.java +++ b/src/main/java/com/kumofactory/cloud/member/domain/Member.java @@ -20,22 +20,22 @@ @Setter public class Member { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - private String oauth_id; + private String oauthId; - private String provider; + private String provider; - @OneToMany(mappedBy = "member") - private List bluePrints; + @OneToMany(mappedBy = "member") + private List bluePrints; - // =========== 생성함수 =========== // - public static Member createMember(UserInfoDto userInfoDto) { - Member member = new Member(); - member.setOauth_id(userInfoDto.id()); - member.setProvider(userInfoDto.provider()); - return member; - } + // =========== 생성함수 =========== // + public static Member createMember(UserInfoDto userInfoDto) { + Member member = new Member(); + member.setOauthId(userInfoDto.id()); + member.setProvider(userInfoDto.provider()); + return member; + } } diff --git a/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java b/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java index ac3a01e..19dcd74 100644 --- a/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java +++ b/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java @@ -4,9 +4,12 @@ import com.kumofactory.cloud.config.OAuthProvider; import com.kumofactory.cloud.jwt.dto.TokenDto; import com.kumofactory.cloud.jwt.provider.JwtTokenProvider; +import com.kumofactory.cloud.member.MemberRepository; +import com.kumofactory.cloud.member.domain.Member; import com.kumofactory.cloud.oauth.dto.UserInfoDto; import com.kumofactory.cloud.oauth.service.github.GitHubService; import com.kumofactory.cloud.oauth.service.google.GoogleService; + import static com.kumofactory.cloud.oauth.dto.OAuthDto.*; import lombok.extern.slf4j.Slf4j; @@ -25,79 +28,88 @@ @Slf4j @RequiredArgsConstructor public class OAuthService { - private final GoogleService googleService; - private final GitHubService githubService; - - private final JwtTokenProvider jwtTokenProvider; - - private TokenDto token; - - public ResponseEntity request(OAuthProvider provider, String code) - throws JsonProcessingException { - - HttpHeaders responseHeaders = new HttpHeaders(); - - switch (provider) { - case GOOGLE:{ - token = getAccessTokenFromGoogle(code); - break; - } - case GITHUB:{ - token = getAccessTokenFromGitHub(code); - break; - } - default:{ - throw new IllegalArgumentException("Unknown type of social login."); - } - } - - responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json"); - - ResponseCookie refreshTokenCookie = ResponseCookie - .from("refreshToken", token.getRefreshToken()) - .httpOnly(true) - .path("/") - .maxAge(Duration.ofDays(365)) - .build(); - - responseHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); - - Map map = new HashMap<>(); - map.put("accessToken", token.getAccessToken()); - - return ResponseEntity.ok() - .headers(responseHeaders) - .body(new ObjectMapper().writeValueAsString(map)); - } - - // TODO : 중복된 코드 간소화 (getAccessTokenFromXXX) - private TokenDto getAccessTokenFromGoogle(String code) - throws JsonProcessingException{ - - GoogleToken accessTokenFromGoogle = googleService.requestAccessToken(code); - if(accessTokenFromGoogle != null) { - - UserInfoDto userInfo = googleService.requestUserInfo(accessTokenFromGoogle.accessToken()); - TokenDto token = jwtTokenProvider.create(userInfo.id()); - - return token; - } - assert accessTokenFromGoogle != null; - return null; - } - - private TokenDto getAccessTokenFromGitHub(String code) - throws JsonProcessingException{ - - GitHubToken accessTokenFromGithub = githubService.requestAccessToken(code); - if(accessTokenFromGithub != null) { - - UserInfoDto userInfo = githubService.requestUserInfo(accessTokenFromGithub.accessToken()); - TokenDto token = jwtTokenProvider.create(userInfo.id()); - - return token; - } - assert accessTokenFromGithub != null; - return null; - } + private final GoogleService googleService; + private final GitHubService githubService; + + private final JwtTokenProvider jwtTokenProvider; + private final MemberRepository memberRepository; + + private TokenDto token; + + public ResponseEntity request(OAuthProvider provider, String code) + throws JsonProcessingException { + + HttpHeaders responseHeaders = new HttpHeaders(); + + switch (provider) { + case GOOGLE: { + token = getAccessTokenFromGoogle(code); + break; + } + case GITHUB: { + token = getAccessTokenFromGitHub(code); + break; + } + default: { + throw new IllegalArgumentException("Unknown type of social login."); + } + } + + responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json"); + + ResponseCookie refreshTokenCookie = ResponseCookie + .from("refreshToken", token.getRefreshToken()) + .httpOnly(true) + .path("/") + .maxAge(Duration.ofDays(365)) + .build(); + + responseHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); + + Map map = new HashMap<>(); + map.put("accessToken", token.getAccessToken()); + + return ResponseEntity.ok() + .headers(responseHeaders) + .body(new ObjectMapper().writeValueAsString(map)); + } + + // TODO : 중복된 코드 간소화 (getAccessTokenFromXXX) + private TokenDto getAccessTokenFromGoogle(String code) + throws JsonProcessingException { + + GoogleToken accessTokenFromGoogle = googleService.requestAccessToken(code); + if (accessTokenFromGoogle != null) { + + UserInfoDto userInfo = googleService.requestUserInfo(accessTokenFromGoogle.accessToken()); + saveMember(userInfo); + + return jwtTokenProvider.create(userInfo.id()); + } + return null; + } + + private TokenDto getAccessTokenFromGitHub(String code) + throws JsonProcessingException { + + GitHubToken accessTokenFromGithub = githubService.requestAccessToken(code); + if (accessTokenFromGithub != null) { + + UserInfoDto userInfo = githubService.requestUserInfo(accessTokenFromGithub.accessToken()); + saveMember(userInfo); + + + return jwtTokenProvider.create(userInfo.id()); + } + return null; + } + + // 처음 가입한 member 일때만 저장 + private void saveMember(UserInfoDto userInfo) { + Member member = memberRepository.findMemberByOauthId(userInfo.id()); + if (member == null) { + member = Member.createMember(userInfo); + memberRepository.save(member); + } + } } From 49ee18733632f988c5710e8ce96e3e738554ea0c Mon Sep 17 00:00:00 2001 From: Hyeonuook Kim Date: Mon, 17 Jul 2023 00:34:21 +0900 Subject: [PATCH 18/20] feat : blue print api done --- .../cloud/blueprint/BlueprintController.java | 16 ++++--- .../cloud/blueprint/domain/ComponentDot.java | 39 ++++++++--------- .../domain/aws/AwsComponentType.java | 2 +- .../blueprint/dto/aws/AwsBluePrintDto.java | 1 - .../dto/aws/AwsBluePrintListDto.java | 14 ++++++ .../service/AwsBlueprintService.java | 5 ++- .../service/AwsBlueprintServiceImpl.java | 18 ++++---- .../auth/AuthorizationFromTokenAspect.java | 2 +- .../infra/service/aws_cdk/Ec2ServiceImpl.java | 43 ------------------- 9 files changed, 56 insertions(+), 84 deletions(-) create mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java delete mode 100644 src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java diff --git a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java index ca906a3..e00534c 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java @@ -2,6 +2,7 @@ import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; +import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintListDto; import com.kumofactory.cloud.blueprint.service.AwsBlueprintService; import com.kumofactory.cloud.global.middleware.auth.AuthorizationFromToken; import lombok.RequiredArgsConstructor; @@ -19,10 +20,10 @@ public class BlueprintController { private final Logger logger = LoggerFactory.getLogger(BlueprintController.class); private final AwsBlueprintService awsBlueprintService; - + @GetMapping("/aws/{id}") @AuthorizationFromToken - public AwsBluePrintDto getAwsBlueprint(@PathVariable("id") Long id) { + public AwsBluePrintDto getAwsBlueprint(@PathVariable("id") Long id, String userId) { try { logger.info("aws blue print id: {}", id); AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(id); @@ -34,15 +35,16 @@ public AwsBluePrintDto getAwsBlueprint(@PathVariable("id") Long id) { @GetMapping("/aws/list") @AuthorizationFromToken - public List getAwsBlueprintList(String userId) { + public List getAwsBlueprintList(String userId) { logger.info("userId: {}", userId); - List awsBlueprintList = awsBlueprintService.getMyAwsBlueprints(userId); - return awsBlueprintList; + return awsBlueprintService.getMyAwsBlueprints(userId); } @PostMapping("/aws") - public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { - awsBlueprintService.store(awsBluePrintDto); + @AuthorizationFromToken + public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto, String userId) { + logger.info(userId); + awsBlueprintService.store(awsBluePrintDto, userId); return "hello-world"; } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java index 1e7f557..5f8fd92 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java @@ -8,10 +8,7 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; import java.util.Date; @Entity @@ -20,24 +17,24 @@ @NoArgsConstructor @AllArgsConstructor public class ComponentDot { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - @CreationTimestamp - private Date created_at; - @UpdateTimestamp - private Date updated_at; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; - private Integer x; // 점의 x 좌표 - private Integer y; // 점의 y 좌표 - private String componentId; // 점이 속한 컴포넌트의 id + private Integer x; // 점의 x 좌표 + private Integer y; // 점의 y 좌표 + private String componentId; // 점이 속한 컴포넌트의 id - public static ComponentDot createComponentDot(ComponentDotDto componentDotDto) { - ComponentDot componentDot = new ComponentDot(); - componentDot.setComponentId(componentDotDto.getComponentId()); - componentDot.setX(componentDotDto.getX()); - componentDot.setY(componentDotDto.getY()); - return componentDot; - } + public static ComponentDot createComponentDot(ComponentDotDto componentDotDto) { + ComponentDot componentDot = new ComponentDot(); + componentDot.setComponentId(componentDotDto.getComponentId()); + componentDot.setX(componentDotDto.getX()); + componentDot.setY(componentDotDto.getY()); + return componentDot; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java index e5221ad..51499cc 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java @@ -1,5 +1,5 @@ package com.kumofactory.cloud.blueprint.domain.aws; public enum AwsComponentType { - VPC, SUBNET, ELB, EC2, EFS, WAF, ROUTE53, NAT_GATEWAY, RDS, + VPC, SUBNET, ELB, EC2, EFS, WAF, ROUTE53, NAT_GATEWAY, RDS, S3, ElastiCache, CloudFront, AutoScaling } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java index b4a9b87..eb73f6d 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -13,7 +13,6 @@ @Data public class AwsBluePrintDto { - private Long id; private String name; private List components; private List links; diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java new file mode 100644 index 0000000..99bec33 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java @@ -0,0 +1,14 @@ +package com.kumofactory.cloud.blueprint.dto.aws; + +import lombok.Getter; +import lombok.Setter; + +import java.util.Date; + +@Getter +@Setter +public class AwsBluePrintListDto { + private Long id; + private String name; + private Date createdAt; +} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java index a736dc2..9e1f8c3 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java @@ -3,6 +3,7 @@ import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; +import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintListDto; import java.util.List; @@ -10,7 +11,7 @@ public interface AwsBlueprintService { AwsBluePrintDto getAwsBlueprint(Long bluePrintId); - List getMyAwsBlueprints(String userId); + List getMyAwsBlueprints(String userId); - void store(AwsBluePrintDto awsBluePrintDto); + void store(AwsBluePrintDto awsBluePrintDto, String userId); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java index 271f8d5..28fef92 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java @@ -6,6 +6,7 @@ import com.kumofactory.cloud.blueprint.dto.ComponentLineDto; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; +import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintListDto; import com.kumofactory.cloud.blueprint.dto.aws.AwsComponentDto; import com.kumofactory.cloud.blueprint.repository.ComponentDotRepository; import com.kumofactory.cloud.blueprint.repository.ComponentLineRepository; @@ -39,7 +40,7 @@ public class AwsBlueprintServiceImpl implements AwsBlueprintService { @Override public AwsBluePrintDto getAwsBlueprint(Long bluePrintId) { - AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintByMemberId(bluePrintId); + AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintById(bluePrintId); if (awsBluePrintById == null) { throw new RuntimeException("awsBluePrintById is null"); } @@ -47,7 +48,6 @@ public AwsBluePrintDto getAwsBlueprint(Long bluePrintId) { List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); List componentLines = componentLineRepository.findAllByBluePrint(awsBluePrintById); AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); - awsBluePrintDto.setId(awsBluePrintById.getId()); awsBluePrintDto.setName(awsBluePrintById.getName()); awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); awsBluePrintDto.setLinks(AwsBluePrintDto.componentLinkDtoListMapper(componentLines)); @@ -55,29 +55,31 @@ public AwsBluePrintDto getAwsBlueprint(Long bluePrintId) { } @Override - public List getMyAwsBlueprints(String oauthId) { + public List getMyAwsBlueprints(String oauthId) { Member member = memberRepository.findMemberByOauthId(oauthId); if (member == null) { throw new RuntimeException("member is null"); } List awsBluePrints = awsBluePrintRepository.findAwsBluePrintsByMember(member); - List awsBluePrintDtos = new ArrayList<>(); + List awsBluePrintDtos = new ArrayList<>(); for (AwsBluePrint awsBluePrint : awsBluePrints) { - AwsBluePrintDto dto = new AwsBluePrintDto(); - dto.setId(awsBluePrint.getId()); + AwsBluePrintListDto dto = new AwsBluePrintListDto(); dto.setName(awsBluePrint.getName()); + dto.setId(awsBluePrint.getId()); + dto.setCreatedAt(awsBluePrint.getCreated_at()); awsBluePrintDtos.add(dto); } return awsBluePrintDtos; } @Override - public void store(AwsBluePrintDto awsBluePrintDto) { + public void store(AwsBluePrintDto awsBluePrintDto, String userId) { + Member member = memberRepository.findMemberByOauthId(userId); // BluePrint 저장 AwsBluePrint awsBluePrint = new AwsBluePrint(); awsBluePrint.setName(awsBluePrintDto.getName()); -// awsBluePrint.setMember(member); + awsBluePrint.setMember(member); AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); logger.info("savedBlueprint: {}", savedBlueprint); diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java index dc45521..dfc48f3 100644 --- a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java +++ b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java @@ -37,7 +37,7 @@ public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, Authori } // 추출된 사용자 정보를 매개변수로 전달 Object[] args = joinPoint.getArgs(); - args[0] = userId; + args[args.length - 1] = userId; return joinPoint.proceed(args); } diff --git a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java deleted file mode 100644 index e736e8c..0000000 --- a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.kumofactory.cloud.infra.service.aws_cdk; - -import java.util.ArrayList; -import java.util.List; - -import lombok.extern.slf4j.Slf4j; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; -import software.amazon.awscdk.services.ec2.BastionHostLinux; -import software.amazon.awscdk.services.ec2.BastionHostLinuxProps; -import software.amazon.awscdk.services.ec2.SubnetFilter; -import software.amazon.awscdk.services.ec2.SubnetSelection; -import software.amazon.awscdk.services.ec2.SubnetSelection.Builder; -import software.amazon.awscdk.services.ec2.Instance; - -/** - * AWS CDK를 이용한 EC2 서비스 - */ -@Service -@Slf4j -public class Ec2ServiceImpl implements Ec2Service { - - private final Logger logger = LoggerFactory.getLogger(Ec2ServiceImpl.class); - - @Override - public void createBastionHostLinux() { - logger.info("================ Create BastionHostLinux Instance ================"); - List subnetsId = new ArrayList<>(); - List subnetFilters = new ArrayList<>(); - subnetsId.add("subnet-043aec505f84e38ac"); - SubnetFilter subnetFilter = SubnetFilter.byIds(subnetsId); - subnetFilters.add(subnetFilter); - BastionHostLinuxProps.builder().subnetSelection(SubnetSelection.builder().subnetFilters(subnetFilters).build()); - } - - @Override - public void createEc2Instance() { - logger.info("================ Create EC2 Instance ================"); - - - } -} From 3b3295058ecf87c4c70c24efbe4f288c11564ea3 Mon Sep 17 00:00:00 2001 From: wook Date: Mon, 17 Jul 2023 14:11:28 +0900 Subject: [PATCH 19/20] =?UTF-8?q?fix=20:=20lint=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java index c29ffa5..be96706 100644 --- a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java +++ b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java @@ -73,6 +73,7 @@ public boolean validateRefreshToken(String token) { } } + // TODO : Refresh Token API 구현 public String refreshAccessToken(Claims claims) { String id = claims.getSubject(); TokenDto tokenDto = create(id); From 3014a89771ed9cf3a9758d7b386ec5f372fb533c Mon Sep 17 00:00:00 2001 From: wook Date: Mon, 17 Jul 2023 14:13:44 +0900 Subject: [PATCH 20/20] Revert "Merge remote-tracking branch 'origin/feature/#17-blueprint' into feature/#17-blueprint" This reverts commit 9f42d0be8d1aa6cec1d60c02fb5cf28dcefc995f. --- .../cloud/blueprint/BlueprintController.java | 57 ++---- .../cloud/blueprint/domain/ComponentDot.java | 39 +++-- .../domain/aws/AwsComponentType.java | 2 +- .../blueprint/dto/aws/AwsBluePrintDto.java | 38 ++-- .../dto/aws/AwsBluePrintListDto.java | 14 -- .../aws/AwsBluePrintRepository.java | 7 - .../service/AwsBlueprintService.java | 7 +- .../service/AwsBlueprintServiceImpl.java | 114 ++++++------ .../middleware/AuthorizationFromToken.java | 4 + .../auth/AuthorizationFromTokenAspect.java | 77 +++++---- .../infra/service/aws_cdk/Ec2ServiceImpl.java | 43 +++++ .../infra/service/aws_cdk/stack/Ec2Stack.java | 16 ++ .../cloud/jwt/provider/JwtTokenProvider.java | 1 - .../cloud/member/MemberRepository.java | 4 +- .../cloud/member/domain/Member.java | 28 +-- .../cloud/oauth/service/OAuthService.java | 162 ++++++++---------- 16 files changed, 309 insertions(+), 304 deletions(-) delete mode 100644 src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java create mode 100644 src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java create mode 100644 src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java create mode 100644 src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java diff --git a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java index e00534c..9dafc31 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/BlueprintController.java @@ -2,56 +2,37 @@ import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; -import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintListDto; import com.kumofactory.cloud.blueprint.service.AwsBlueprintService; -import com.kumofactory.cloud.global.middleware.auth.AuthorizationFromToken; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; -import java.util.List; - @RestController @RequiredArgsConstructor @RequestMapping("/blueprint") @Slf4j public class BlueprintController { - private final Logger logger = LoggerFactory.getLogger(BlueprintController.class); - private final AwsBlueprintService awsBlueprintService; - - @GetMapping("/aws/{id}") - @AuthorizationFromToken - public AwsBluePrintDto getAwsBlueprint(@PathVariable("id") Long id, String userId) { - try { - logger.info("aws blue print id: {}", id); - AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(id); - return awsBlueprint; - } catch (RuntimeException e) { - return null; - } - } - - @GetMapping("/aws/list") - @AuthorizationFromToken - public List getAwsBlueprintList(String userId) { - logger.info("userId: {}", userId); - return awsBlueprintService.getMyAwsBlueprints(userId); - } + private final Logger logger = LoggerFactory.getLogger(BlueprintController.class); + private final AwsBlueprintService awsBlueprintService; - @PostMapping("/aws") - @AuthorizationFromToken - public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto, String userId) { - logger.info(userId); - awsBlueprintService.store(awsBluePrintDto, userId); - return "hello-world"; - } + // TODO : 토큰에서 유저 정보 가져오는 로직 추가 + // TODO : blueprint list 주는 api 추가 + // TODO : blueprint id 값으로 blueprint 가져오는 api 추가 + @GetMapping("/aws") + public AwsBluePrintDto getAwsBlueprint() { + try { + AwsBluePrintDto awsBlueprint = awsBlueprintService.getAwsBlueprint(); + return awsBlueprint; + } catch (RuntimeException e) { + return null; + } + } - @GetMapping("/test") - @AuthorizationFromToken - public String testMiddleware(String userId) { - System.out.printf("userId: %s\n", userId); - return userId; - } + @PostMapping("/aws") + public String createAwsBlueprint(@RequestBody AwsBluePrintDto awsBluePrintDto) { + awsBlueprintService.store(awsBluePrintDto); + return "hello-world"; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java index 5f8fd92..1e7f557 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/ComponentDot.java @@ -8,7 +8,10 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; import java.util.Date; @Entity @@ -17,24 +20,24 @@ @NoArgsConstructor @AllArgsConstructor public class ComponentDot { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - @CreationTimestamp - private Date created_at; - @UpdateTimestamp - private Date updated_at; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @CreationTimestamp + private Date created_at; + @UpdateTimestamp + private Date updated_at; - private Integer x; // 점의 x 좌표 - private Integer y; // 점의 y 좌표 - private String componentId; // 점이 속한 컴포넌트의 id + private Integer x; // 점의 x 좌표 + private Integer y; // 점의 y 좌표 + private String componentId; // 점이 속한 컴포넌트의 id - public static ComponentDot createComponentDot(ComponentDotDto componentDotDto) { - ComponentDot componentDot = new ComponentDot(); - componentDot.setComponentId(componentDotDto.getComponentId()); - componentDot.setX(componentDotDto.getX()); - componentDot.setY(componentDotDto.getY()); - return componentDot; - } + public static ComponentDot createComponentDot(ComponentDotDto componentDotDto) { + ComponentDot componentDot = new ComponentDot(); + componentDot.setComponentId(componentDotDto.getComponentId()); + componentDot.setX(componentDotDto.getX()); + componentDot.setY(componentDotDto.getY()); + return componentDot; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java index 51499cc..e5221ad 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/domain/aws/AwsComponentType.java @@ -1,5 +1,5 @@ package com.kumofactory.cloud.blueprint.domain.aws; public enum AwsComponentType { - VPC, SUBNET, ELB, EC2, EFS, WAF, ROUTE53, NAT_GATEWAY, RDS, S3, ElastiCache, CloudFront, AutoScaling + VPC, SUBNET, ELB, EC2, EFS, WAF, ROUTE53, NAT_GATEWAY, RDS, } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java index eb73f6d..91d662e 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -13,23 +13,23 @@ @Data public class AwsBluePrintDto { - private String name; - private List components; - private List links; - - public static List awsComponentDtosMapper(List awsComponentDtos) { - List awsComponentDtoList = new ArrayList<>(); - for (AwsComponent awsComponent : awsComponentDtos) { - awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); - } - return awsComponentDtoList; - } - - public static List componentLinkDtoListMapper(List componentLinks) { - List componentLinkDtoList = new ArrayList<>(); - for (ComponentLine pointLink : componentLinks) { - componentLinkDtoList.add(ComponentLineDto.mapper(pointLink)); - } - return componentLinkDtoList; - } + private String name; + private List components; + private List links; + + public static List awsComponentDtosMapper(List awsComponentDtos) { + List awsComponentDtoList = new ArrayList<>(); + for (AwsComponent awsComponent : awsComponentDtos) { + awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); + } + return awsComponentDtoList; + } + + public static List componentLinkDtoListMapper(List componentLinks) { + List componentLinkDtoList = new ArrayList<>(); + for (ComponentLine pointLink : componentLinks) { + componentLinkDtoList.add(ComponentLineDto.mapper(pointLink)); + } + return componentLinkDtoList; + } } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java deleted file mode 100644 index 99bec33..0000000 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintListDto.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.kumofactory.cloud.blueprint.dto.aws; - -import lombok.Getter; -import lombok.Setter; - -import java.util.Date; - -@Getter -@Setter -public class AwsBluePrintListDto { - private Long id; - private String name; - private Date createdAt; -} diff --git a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java index c8ddacf..8bb149f 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/repository/aws/AwsBluePrintRepository.java @@ -1,17 +1,10 @@ package com.kumofactory.cloud.blueprint.repository.aws; import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; -import com.kumofactory.cloud.member.domain.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; - @Repository public interface AwsBluePrintRepository extends JpaRepository { AwsBluePrint findAwsBluePrintById(long id); - - AwsBluePrint findAwsBluePrintByMemberId(long memberId); - - List findAwsBluePrintsByMember(Member member); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java index 9e1f8c3..3354151 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintService.java @@ -3,15 +3,14 @@ import com.kumofactory.cloud.blueprint.domain.aws.AwsBluePrint; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; -import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintListDto; import java.util.List; public interface AwsBlueprintService { - AwsBluePrintDto getAwsBlueprint(Long bluePrintId); + AwsBluePrintDto getAwsBlueprint(); - List getMyAwsBlueprints(String userId); + List getMyAwsBlueprints(); - void store(AwsBluePrintDto awsBluePrintDto, String userId); + void store(AwsBluePrintDto awsBluePrintDto); } diff --git a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java index 28fef92..e1f712b 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/service/AwsBlueprintServiceImpl.java @@ -6,14 +6,12 @@ import com.kumofactory.cloud.blueprint.dto.ComponentLineDto; import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintDto; -import com.kumofactory.cloud.blueprint.dto.aws.AwsBluePrintListDto; import com.kumofactory.cloud.blueprint.dto.aws.AwsComponentDto; import com.kumofactory.cloud.blueprint.repository.ComponentDotRepository; import com.kumofactory.cloud.blueprint.repository.ComponentLineRepository; import com.kumofactory.cloud.blueprint.repository.aws.AwsBluePrintRepository; import com.kumofactory.cloud.blueprint.repository.aws.AwsComponentRepository; import com.kumofactory.cloud.member.MemberRepository; -import com.kumofactory.cloud.member.domain.Member; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; @@ -30,75 +28,63 @@ @Repository public class AwsBlueprintServiceImpl implements AwsBlueprintService { - private final MemberRepository memberRepository; - private final AwsBluePrintRepository awsBluePrintRepository; - private final AwsComponentRepository awsComponentRepository; - private final ComponentLineRepository componentLineRepository; - private final ComponentDotRepository componentDotRepository; - private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); + private final MemberRepository memberRepository; + private final AwsBluePrintRepository awsBluePrintRepository; + private final AwsComponentRepository awsComponentRepository; + private final ComponentLineRepository componentLineRepository; + private final ComponentDotRepository componentDotRepository; + private final Logger logger = LoggerFactory.getLogger(AwsBlueprintServiceImpl.class); - @Override - public AwsBluePrintDto getAwsBlueprint(Long bluePrintId) { - AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintById(bluePrintId); - if (awsBluePrintById == null) { - throw new RuntimeException("awsBluePrintById is null"); - } + @Override + public AwsBluePrintDto getAwsBlueprint() { + AwsBluePrint awsBluePrintById = awsBluePrintRepository.findAwsBluePrintById(1L); + logger.info("awsBluePrintById: {}", awsBluePrintById); + if (awsBluePrintById == null) { + throw new RuntimeException("awsBluePrintById is null"); + } - List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); - List componentLines = componentLineRepository.findAllByBluePrint(awsBluePrintById); - AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); - awsBluePrintDto.setName(awsBluePrintById.getName()); - awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); - awsBluePrintDto.setLinks(AwsBluePrintDto.componentLinkDtoListMapper(componentLines)); - return awsBluePrintDto; - } + List awsComponents = awsComponentRepository.findAllByBluePrint(awsBluePrintById); + List componentLines = componentLineRepository.findAllByBluePrint(awsBluePrintById); + AwsBluePrintDto awsBluePrintDto = new AwsBluePrintDto(); + awsBluePrintDto.setName(awsBluePrintById.getName()); + awsBluePrintDto.setComponents(AwsBluePrintDto.awsComponentDtosMapper(awsComponents)); + awsBluePrintDto.setLinks(AwsBluePrintDto.componentLinkDtoListMapper(componentLines)); + return awsBluePrintDto; + } - @Override - public List getMyAwsBlueprints(String oauthId) { - Member member = memberRepository.findMemberByOauthId(oauthId); - if (member == null) { - throw new RuntimeException("member is null"); - } + @Override + public List getMyAwsBlueprints() { + return null; + } - List awsBluePrints = awsBluePrintRepository.findAwsBluePrintsByMember(member); - List awsBluePrintDtos = new ArrayList<>(); - for (AwsBluePrint awsBluePrint : awsBluePrints) { - AwsBluePrintListDto dto = new AwsBluePrintListDto(); - dto.setName(awsBluePrint.getName()); - dto.setId(awsBluePrint.getId()); - dto.setCreatedAt(awsBluePrint.getCreated_at()); - awsBluePrintDtos.add(dto); - } - return awsBluePrintDtos; - } + @Override + public void store(AwsBluePrintDto awsBluePrintDto) { +// Member member = memberRepository.findMemberById(1L); - @Override - public void store(AwsBluePrintDto awsBluePrintDto, String userId) { - Member member = memberRepository.findMemberByOauthId(userId); - // BluePrint 저장 - AwsBluePrint awsBluePrint = new AwsBluePrint(); - awsBluePrint.setName(awsBluePrintDto.getName()); - awsBluePrint.setMember(member); - AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); - logger.info("savedBlueprint: {}", savedBlueprint); + // BluePrint 저장 + AwsBluePrint awsBluePrint = new AwsBluePrint(); + awsBluePrint.setName(awsBluePrintDto.getName()); +// awsBluePrint.setMember(member); + AwsBluePrint savedBlueprint = awsBluePrintRepository.save(awsBluePrint); + logger.info("savedBlueprint: {}", savedBlueprint); - List components = new ArrayList<>(); - List links = awsBluePrintDto.getLinks(); + List components = new ArrayList<>(); + List links = awsBluePrintDto.getLinks(); - // Components 저장 - for (AwsComponentDto component : awsBluePrintDto.getComponents()) { - AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); - components.add(awsComponent); - } - awsComponentRepository.saveAll(components); + // Components 저장 + for (AwsComponentDto component : awsBluePrintDto.getComponents()) { + AwsComponent awsComponent = AwsComponent.createAwsComponent(component, savedBlueprint); + components.add(awsComponent); + } + awsComponentRepository.saveAll(components); - // Lines 저장 - List componentLines = new ArrayList<>(); - for (ComponentLineDto link : links) { - ComponentLine componentLink = ComponentLine.createComponentLink(link, savedBlueprint); - componentLines.add(componentLink); - } - componentLineRepository.saveAll(componentLines); - } + // Lines 저장 + List componentLines = new ArrayList<>(); + for (ComponentLineDto link : links) { + ComponentLine componentLink = ComponentLine.createComponentLink(link, savedBlueprint); + componentLines.add(componentLink); + } + componentLineRepository.saveAll(componentLines); + } } diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java b/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java new file mode 100644 index 0000000..37edd75 --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.java @@ -0,0 +1,4 @@ +package com.kumofactory.cloud.global.middleware; + +public class AuthorizationFromToken { +} diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java index dfc48f3..64f90a9 100644 --- a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java +++ b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java @@ -2,53 +2,62 @@ import com.kumofactory.cloud.jwt.provider.JwtTokenProvider; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; -import java.util.Objects; @Aspect @Component @RequiredArgsConstructor -@Slf4j public class AuthorizationFromTokenAspect { - private final JwtTokenProvider jwtTokenProvider; - private final Logger logger = LoggerFactory.getLogger(AuthorizationFromTokenAspect.class); - - @Around(value = "@annotation(AuthorizationFromToken)") - public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, AuthorizationFromToken AuthorizationFromToken) throws Throwable { - ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); - String userId = getUserFromAccessToken(request.getHeader("Authorization")); - if (userId == null) { - throw new RuntimeException("유효하지 않은 토큰입니다."); - } - // 추출된 사용자 정보를 매개변수로 전달 - Object[] args = joinPoint.getArgs(); - args[args.length - 1] = userId; - - return joinPoint.proceed(args); - } - - // user oauth id 추출 - private String getUserFromAccessToken(String token) { - String accessToken = token.split(" ")[1]; - boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); - if (isValidate) { - return jwtTokenProvider.getClaimsFormToken(accessToken).getSubject(); - } - return null; - } + private final JwtTokenProvider jwtTokenProvider; + + @Around(value = "@annotation(AuthorizationFromToken)") + public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, AuthorizationFromToken AuthorizationFromToken) throws Throwable { + HttpServletRequest request = findHttpServletRequest(joinPoint.getArgs()); + HttpServletRequest header = findHttpServletRequest(joinPoint.getArgs()); + String accessToken = extractAccessTokenFromCookies(request.getCookies()); + String userId = getUserFromAccessToken(accessToken); + + // 추출된 사용자 정보를 매개변수로 전달 + Object[] args = joinPoint.getArgs(); + args[0] = userId; + + return joinPoint.proceed(args); + } + + private HttpServletRequest findHttpServletRequest(Object[] args) { + for (Object arg : args) { + if (arg instanceof HttpServletRequest) { + return (HttpServletRequest) arg; + } + } + throw new IllegalArgumentException("HttpServletRequest not found in method parameters"); + } + + private String extractAccessTokenFromCookies(Cookie[] cookies) { + if (cookies != null) { + return Arrays.stream(cookies) + .filter(cookie -> "access_token".equals(cookie.getName())) + .map(Cookie::getValue) + .findFirst() + .orElse(null); + } + return null; + } + + private String getUserFromAccessToken(String accessToken) { + boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); + if (isValidate) { + return jwtTokenProvider.getClaimsFormToken(accessToken).getSubject(); + } + return null; + } } diff --git a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java new file mode 100644 index 0000000..e736e8c --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.java @@ -0,0 +1,43 @@ +package com.kumofactory.cloud.infra.service.aws_cdk; + +import java.util.ArrayList; +import java.util.List; + +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import software.amazon.awscdk.services.ec2.BastionHostLinux; +import software.amazon.awscdk.services.ec2.BastionHostLinuxProps; +import software.amazon.awscdk.services.ec2.SubnetFilter; +import software.amazon.awscdk.services.ec2.SubnetSelection; +import software.amazon.awscdk.services.ec2.SubnetSelection.Builder; +import software.amazon.awscdk.services.ec2.Instance; + +/** + * AWS CDK를 이용한 EC2 서비스 + */ +@Service +@Slf4j +public class Ec2ServiceImpl implements Ec2Service { + + private final Logger logger = LoggerFactory.getLogger(Ec2ServiceImpl.class); + + @Override + public void createBastionHostLinux() { + logger.info("================ Create BastionHostLinux Instance ================"); + List subnetsId = new ArrayList<>(); + List subnetFilters = new ArrayList<>(); + subnetsId.add("subnet-043aec505f84e38ac"); + SubnetFilter subnetFilter = SubnetFilter.byIds(subnetsId); + subnetFilters.add(subnetFilter); + BastionHostLinuxProps.builder().subnetSelection(SubnetSelection.builder().subnetFilters(subnetFilters).build()); + } + + @Override + public void createEc2Instance() { + logger.info("================ Create EC2 Instance ================"); + + + } +} diff --git a/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java new file mode 100644 index 0000000..1cdb2cd --- /dev/null +++ b/src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.java @@ -0,0 +1,16 @@ +package com.kumofactory.cloud.infra.service.aws_cdk.stack; + +import software.amazon.awscdk.Stack; +import software.amazon.awscdk.services.ec2.ISubnet; +import software.amazon.awscdk.services.ec2.Subnet; +import software.amazon.awscdk.services.ec2.Vpc; +import software.amazon.awscdk.services.ec2.VpcLookupOptions; +import software.constructs.Construct; + +public class Ec2Stack extends Stack { + public Ec2Stack(final Construct scope, String id) { + super(scope, id); + Vpc vpc = (Vpc) Vpc.fromLookup(this, "KumofactoryVPC", VpcLookupOptions.builder().vpcId("vpc-0719a1184fa5ccdcd").build()); + ISubnet kumofactorySubnet = Subnet.fromSubnetId(this, "KumofactorySubnet", "subnet-043aec505f84e38ac"); + } +} diff --git a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java index 085c995..be96706 100644 --- a/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java +++ b/src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.java @@ -73,7 +73,6 @@ public boolean validateRefreshToken(String token) { } } - // TODO : Refresh Token API 구현 public String refreshAccessToken(Claims claims) { String id = claims.getSubject(); diff --git a/src/main/java/com/kumofactory/cloud/member/MemberRepository.java b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java index 9e0024f..594b50e 100644 --- a/src/main/java/com/kumofactory/cloud/member/MemberRepository.java +++ b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java @@ -6,7 +6,5 @@ @Repository public interface MemberRepository extends JpaRepository { - Member findMemberById(Long id); - - Member findMemberByOauthId(String oauth_id); + Member findMemberById(Long id); } diff --git a/src/main/java/com/kumofactory/cloud/member/domain/Member.java b/src/main/java/com/kumofactory/cloud/member/domain/Member.java index 0055d94..ab53028 100644 --- a/src/main/java/com/kumofactory/cloud/member/domain/Member.java +++ b/src/main/java/com/kumofactory/cloud/member/domain/Member.java @@ -20,22 +20,22 @@ @Setter public class Member { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - private String oauthId; + private String oauth_id; - private String provider; + private String provider; - @OneToMany(mappedBy = "member") - private List bluePrints; + @OneToMany(mappedBy = "member") + private List bluePrints; - // =========== 생성함수 =========== // - public static Member createMember(UserInfoDto userInfoDto) { - Member member = new Member(); - member.setOauthId(userInfoDto.id()); - member.setProvider(userInfoDto.provider()); - return member; - } + // =========== 생성함수 =========== // + public static Member createMember(UserInfoDto userInfoDto) { + Member member = new Member(); + member.setOauth_id(userInfoDto.id()); + member.setProvider(userInfoDto.provider()); + return member; + } } diff --git a/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java b/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java index 19dcd74..ac3a01e 100644 --- a/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java +++ b/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java @@ -4,12 +4,9 @@ import com.kumofactory.cloud.config.OAuthProvider; import com.kumofactory.cloud.jwt.dto.TokenDto; import com.kumofactory.cloud.jwt.provider.JwtTokenProvider; -import com.kumofactory.cloud.member.MemberRepository; -import com.kumofactory.cloud.member.domain.Member; import com.kumofactory.cloud.oauth.dto.UserInfoDto; import com.kumofactory.cloud.oauth.service.github.GitHubService; import com.kumofactory.cloud.oauth.service.google.GoogleService; - import static com.kumofactory.cloud.oauth.dto.OAuthDto.*; import lombok.extern.slf4j.Slf4j; @@ -28,88 +25,79 @@ @Slf4j @RequiredArgsConstructor public class OAuthService { - private final GoogleService googleService; - private final GitHubService githubService; - - private final JwtTokenProvider jwtTokenProvider; - private final MemberRepository memberRepository; - - private TokenDto token; - - public ResponseEntity request(OAuthProvider provider, String code) - throws JsonProcessingException { - - HttpHeaders responseHeaders = new HttpHeaders(); - - switch (provider) { - case GOOGLE: { - token = getAccessTokenFromGoogle(code); - break; - } - case GITHUB: { - token = getAccessTokenFromGitHub(code); - break; - } - default: { - throw new IllegalArgumentException("Unknown type of social login."); - } - } - - responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json"); - - ResponseCookie refreshTokenCookie = ResponseCookie - .from("refreshToken", token.getRefreshToken()) - .httpOnly(true) - .path("/") - .maxAge(Duration.ofDays(365)) - .build(); - - responseHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); - - Map map = new HashMap<>(); - map.put("accessToken", token.getAccessToken()); - - return ResponseEntity.ok() - .headers(responseHeaders) - .body(new ObjectMapper().writeValueAsString(map)); - } - - // TODO : 중복된 코드 간소화 (getAccessTokenFromXXX) - private TokenDto getAccessTokenFromGoogle(String code) - throws JsonProcessingException { - - GoogleToken accessTokenFromGoogle = googleService.requestAccessToken(code); - if (accessTokenFromGoogle != null) { - - UserInfoDto userInfo = googleService.requestUserInfo(accessTokenFromGoogle.accessToken()); - saveMember(userInfo); - - return jwtTokenProvider.create(userInfo.id()); - } - return null; - } - - private TokenDto getAccessTokenFromGitHub(String code) - throws JsonProcessingException { - - GitHubToken accessTokenFromGithub = githubService.requestAccessToken(code); - if (accessTokenFromGithub != null) { - - UserInfoDto userInfo = githubService.requestUserInfo(accessTokenFromGithub.accessToken()); - saveMember(userInfo); - - - return jwtTokenProvider.create(userInfo.id()); - } - return null; - } - - // 처음 가입한 member 일때만 저장 - private void saveMember(UserInfoDto userInfo) { - Member member = memberRepository.findMemberByOauthId(userInfo.id()); - if (member == null) { - member = Member.createMember(userInfo); - memberRepository.save(member); - } - } + private final GoogleService googleService; + private final GitHubService githubService; + + private final JwtTokenProvider jwtTokenProvider; + + private TokenDto token; + + public ResponseEntity request(OAuthProvider provider, String code) + throws JsonProcessingException { + + HttpHeaders responseHeaders = new HttpHeaders(); + + switch (provider) { + case GOOGLE:{ + token = getAccessTokenFromGoogle(code); + break; + } + case GITHUB:{ + token = getAccessTokenFromGitHub(code); + break; + } + default:{ + throw new IllegalArgumentException("Unknown type of social login."); + } + } + + responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json"); + + ResponseCookie refreshTokenCookie = ResponseCookie + .from("refreshToken", token.getRefreshToken()) + .httpOnly(true) + .path("/") + .maxAge(Duration.ofDays(365)) + .build(); + + responseHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); + + Map map = new HashMap<>(); + map.put("accessToken", token.getAccessToken()); + + return ResponseEntity.ok() + .headers(responseHeaders) + .body(new ObjectMapper().writeValueAsString(map)); + } + + // TODO : 중복된 코드 간소화 (getAccessTokenFromXXX) + private TokenDto getAccessTokenFromGoogle(String code) + throws JsonProcessingException{ + + GoogleToken accessTokenFromGoogle = googleService.requestAccessToken(code); + if(accessTokenFromGoogle != null) { + + UserInfoDto userInfo = googleService.requestUserInfo(accessTokenFromGoogle.accessToken()); + TokenDto token = jwtTokenProvider.create(userInfo.id()); + + return token; + } + assert accessTokenFromGoogle != null; + return null; + } + + private TokenDto getAccessTokenFromGitHub(String code) + throws JsonProcessingException{ + + GitHubToken accessTokenFromGithub = githubService.requestAccessToken(code); + if(accessTokenFromGithub != null) { + + UserInfoDto userInfo = githubService.requestUserInfo(accessTokenFromGithub.accessToken()); + TokenDto token = jwtTokenProvider.create(userInfo.id()); + + return token; + } + assert accessTokenFromGithub != null; + return null; + } }