Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[feat] Spring Secrity, JWT 의존성 추가 #9

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.8'
id 'io.spring.dependency-management' version '1.1.6'
id 'java'
id 'org.springframework.boot' version '3.2.8'
id 'io.spring.dependency-management' version '1.1.6'
}

group = 'wanted'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
useJUnitPlatform()
useJUnitPlatform()
}
7 changes: 7 additions & 0 deletions src/main/java/wanted/media/user/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wanted.media.user.config;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@EnableWebSecurity
public class SecurityConfig {
}
14 changes: 14 additions & 0 deletions src/main/java/wanted/media/user/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package wanted.media.user.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wanted.media.user.service.UserService;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

private final UserService userService;
}
34 changes: 34 additions & 0 deletions src/main/java/wanted/media/user/domain/Code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package wanted.media.user.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Entity
@Table(name = "codes")
public class Code {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long codeId;

@OneToOne
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OneToOne 어노테이션은 왜 달았는지 궁금합니당 💭

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OneToOne 어노테이션은 왜 달았는지 궁금합니당 💭

가입승인시 유저에게 발급되는 랜덤한 코드를 생성하는 api를 만들기 위한 엔티티이기 때문에, 회원과 1대1관계로 연결해주어야 한다고 생각했습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러면 연관관계의 주인이 User가 되는거죠?? User 테이블에 code라는 컬럼을 추가하는건 어떻게 생각하세요?? code라는 테이블이 따로 생겨야 하는 이유가 있을까요??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러면 연관관계의 주인이 User가 되는거죠?? User 테이블에 code라는 컬럼을 추가하는건 어떻게 생각하세요?? code라는 테이블이 따로 생겨야 하는 이유가 있을까요??

가입이 승인된 후에는 필요하지 않은 코드이기도 하고, 만료시간을 두어서 일정 기간 안에 코드 등록을 하지 않으면 코드 재발급이 되게끔 하기 위해서는 따로 code 테이블을 두어서 관리하는게 낫지 않을까 생각했습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그럼 코드 테이블에 코드를 저장하는 경우는 어떤 경우가 있을까요??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그럼 코드 테이블에 코드를 저장하는 경우는 어떤 경우가 있을까요??

어제 강사님께서 사용자 관련으로 말씀해주신 것 바탕으로 저희가 서비스를 구성해봤습니다.
image

  1. 회원가입 후, 인증코드 승인에 상관없이 회원으로 등록
  2. 회원이 인증코드를 입력하면 회원등급 변경 (normal → premium)
  3. 인증과정에서 고객에게 더욱 풍부한 안내를 하기 위해 따로 테이블을 생성
  4. DB 관리를 위해 일정 시간이 지나면 인증코드는 자동으로 삭제할 예정

@JoinColumn(name = "user_id")
private User user;

@Size(max = 10)
@Column(nullable = false)
private String authCode;

@CreatedDate
private LocalDateTime createdTime;
}
5 changes: 5 additions & 0 deletions src/main/java/wanted/media/user/domain/Grade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package wanted.media.user.domain;

public enum Grade {
NORMAL_USER, PREMIUM_USER;
}
26 changes: 26 additions & 0 deletions src/main/java/wanted/media/user/domain/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package wanted.media.user.domain;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Entity
@Table(name = "tokens")
public class Token {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long tokenId;

@Column(nullable = false)
private String refreshToken;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
}
18 changes: 18 additions & 0 deletions src/main/java/wanted/media/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package wanted.media.user.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.UUID;
Expand All @@ -11,11 +13,27 @@
@AllArgsConstructor
@Builder
@Entity
@Getter
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Column(name = "user_id", nullable = false)
private UUID userId;

@Column(unique = true, nullable = false)
@Size(max = 50)
private String account;

@Size(max = 200)
@Column(nullable = false)
private String email;

@Size(max = 200)
@Column(nullable = false)
private String password;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Grade grade;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package wanted.media.user.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import wanted.media.user.domain.User;

import java.util.UUID;

public interface UserRepository extends JpaRepository<User, UUID> {
}
12 changes: 12 additions & 0 deletions src/main/java/wanted/media/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package wanted.media.user.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import wanted.media.user.repository.UserRepository;

@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
}