-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: User Entity 추가 * test: /user/login 인수테스트 작성 * feat: 로그인 뼈대코드 구성 * feat: testcontainer 도입 * test: TestContainer 적용 * test: TestContainer MySQL 버전 5.7로 변경 * test: RestAssured port 선언조건 추가 * feat: 사용자 로그인 구현 * feat: 사용자 마지막 로그인한 날짜 추가 * test: ActiveProfiles 설정 추가 * build: 토큰 값 minute 주석 추가 * refactor: refreshToken 만료일자 14일로 수정 * build: 불필요한 설정파일 제거 * refactor: 예외처리부 추가 * test: 학교 메일로 테스트케이스 수정 * refactor: 반환값 accessToken임을 명시
- Loading branch information
1 parent
44ba5ea
commit 7acab63
Showing
22 changed files
with
566 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package in.koreatech.koin.auth; | ||
|
||
import in.koreatech.koin.domain.user.User; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.security.Keys; | ||
import java.security.Key; | ||
import java.time.Instant; | ||
import java.util.Base64; | ||
import java.util.Date; | ||
import javax.crypto.SecretKey; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class JwtProvider { | ||
|
||
@Value("${jwt.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${jwt.access-token.expiration-time}") | ||
private Long expirationTime; | ||
|
||
public String createToken(User user) { | ||
if (user == null) { | ||
throw new IllegalArgumentException("존재하지 않는 사용자입니다."); | ||
} | ||
|
||
Key key = getSecretKey(); | ||
return Jwts.builder() | ||
.signWith(key) | ||
.header() | ||
.add("typ", "JWT") | ||
.add("alg", key.getAlgorithm()) | ||
.and() | ||
.claim("id", user.getId()) | ||
.expiration(new Date(Instant.now().toEpochMilli() + expirationTime)) | ||
.compact(); | ||
} | ||
|
||
private SecretKey getSecretKey() { | ||
String encoded = Base64.getEncoder().encodeToString(secretKey.getBytes()); | ||
return Keys.hmacShaKeyFor(encoded.getBytes()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/in/koreatech/koin/controller/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package in.koreatech.koin.controller; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
log.warn(e.getMessage()); | ||
return ResponseEntity.badRequest().body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) { | ||
log.warn(e.getMessage()); | ||
return ResponseEntity.badRequest().body(e.getMessage()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/in/koreatech/koin/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package in.koreatech.koin.controller; | ||
|
||
import in.koreatech.koin.dto.UserLoginRequest; | ||
import in.koreatech.koin.dto.UserLoginResponse; | ||
import in.koreatech.koin.service.UserService; | ||
import jakarta.validation.Valid; | ||
import java.net.URI; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
@PostMapping("/user/login") | ||
public ResponseEntity<UserLoginResponse> login(@RequestBody @Valid UserLoginRequest request) { | ||
UserLoginResponse response = userService.login(request); | ||
return ResponseEntity.created(URI.create("/")) | ||
.body(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package in.koreatech.koin.domain.user; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Entity | ||
@Table(name = "students") | ||
public class Student { | ||
|
||
@Id | ||
private Long userId; | ||
|
||
@Size(max = 255) | ||
@Column(name = "anonymous_nickname") | ||
private String anonymousNickname = "익명_" + System.currentTimeMillis(); | ||
|
||
@Size(max = 20) | ||
@Column(name = "student_number", length = 20) | ||
private String studentNumber; | ||
|
||
@Size(max = 50) | ||
@Column(name = "major", length = 50) | ||
private String department; | ||
|
||
@Column(name = "identity") | ||
@Enumerated(EnumType.STRING) | ||
private UserIdentity userIdentity; | ||
|
||
@Column(name = "is_graduated") | ||
private Boolean isGraduated; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package in.koreatech.koin.domain.user; | ||
|
||
import in.koreatech.koin.domain.BaseEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Lob; | ||
import jakarta.persistence.Table; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import java.time.LocalDateTime; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@Table(name = "users") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class User extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@NotNull | ||
@Lob | ||
@Column(name = "password", nullable = false) | ||
private String password; | ||
|
||
@Size(max = 50) | ||
@Column(name = "nickname", length = 50) | ||
private String nickname; | ||
|
||
@Size(max = 50) | ||
@Column(name = "name", length = 50) | ||
private String name; | ||
|
||
@Size(max = 20) | ||
@Column(name = "phone_number", length = 20) | ||
private String phoneNumber; | ||
|
||
@NotNull | ||
@Enumerated(EnumType.STRING) | ||
@Column(name = "user_type", nullable = false, length = 20) | ||
private UserType userType; | ||
|
||
@Size(max = 100) | ||
@NotNull | ||
@Column(name = "email", nullable = false, length = 100) | ||
private String email; | ||
|
||
@Column(name = "gender") | ||
@Enumerated(value = EnumType.ORDINAL) | ||
private UserGender gender; | ||
|
||
@NotNull | ||
@Column(name = "is_authed", nullable = false) | ||
private Boolean isAuthed = false; | ||
|
||
@Column(name = "last_logged_at") | ||
private LocalDateTime lastLoggedAt; | ||
|
||
@Size(max = 255) | ||
@Column(name = "profile_image_url") | ||
private String profileImageUrl; | ||
|
||
@NotNull | ||
@Column(name = "is_deleted", nullable = false) | ||
private Boolean isDeleted = false; | ||
|
||
@Size(max = 255) | ||
@Column(name = "auth_token") | ||
private String authToken; | ||
|
||
@Size(max = 255) | ||
@Column(name = "auth_expired_at") | ||
private String authExpiredAt; | ||
|
||
@Size(max = 255) | ||
@Column(name = "reset_token") | ||
private String resetToken; | ||
|
||
@Size(max = 255) | ||
@Column(name = "reset_expired_at") | ||
private String resetExpiredAt; | ||
|
||
@Builder | ||
public User(String password, String nickname, String name, String phoneNumber, UserType userType, | ||
String email, | ||
UserGender gender, Boolean isAuthed, LocalDateTime lastLoggedAt, String profileImageUrl, | ||
Boolean isDeleted, | ||
String authToken, String authExpiredAt, String resetToken, String resetExpiredAt) { | ||
this.password = password; | ||
this.nickname = nickname; | ||
this.name = name; | ||
this.phoneNumber = phoneNumber; | ||
this.userType = userType; | ||
this.email = email; | ||
this.gender = gender; | ||
this.isAuthed = isAuthed; | ||
this.lastLoggedAt = lastLoggedAt; | ||
this.profileImageUrl = profileImageUrl; | ||
this.isDeleted = isDeleted; | ||
this.authToken = authToken; | ||
this.authExpiredAt = authExpiredAt; | ||
this.resetToken = resetToken; | ||
this.resetExpiredAt = resetExpiredAt; | ||
} | ||
|
||
public boolean isSamePassword(String password) { | ||
return this.password.equals(password); | ||
} | ||
|
||
public void updateLastLoggedTime(LocalDateTime lastLoggedTime) { | ||
lastLoggedAt = lastLoggedTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package in.koreatech.koin.domain.user; | ||
|
||
public enum UserGender { | ||
MAN, | ||
WOMAN, | ||
; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/in/koreatech/koin/domain/user/UserIdentity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package in.koreatech.koin.domain.user; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* 신원 (0: 학생, 1: 대학원생, 2: 교수, 3: 교직원, 4: 졸업생, 5: 점주) | ||
*/ | ||
@Getter | ||
public enum UserIdentity { | ||
UNDERGRADUATE("학부생"), | ||
GRADUATE("대학원생"), | ||
PROFESSOR("교수"), | ||
STAFF("교직원"), | ||
ALUMNI("졸업생"), | ||
OWNER("점주"), | ||
; | ||
|
||
private final String value; | ||
|
||
UserIdentity(String value) { | ||
this.value = value; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/in/koreatech/koin/domain/user/UserToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package in.koreatech.koin.domain.user; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
import org.springframework.data.redis.core.TimeToLive; | ||
|
||
@Getter | ||
@RedisHash("refreshToken") | ||
public class UserToken { | ||
|
||
private static final long REFRESH_TOKEN_EXPIRE_DAY = 14L; | ||
|
||
@Id | ||
private Long id; | ||
|
||
private final String refreshToken; | ||
|
||
@TimeToLive(unit = TimeUnit.DAYS) | ||
private final Long expiration; | ||
|
||
private UserToken(Long id, String refreshToken, Long expiration) { | ||
this.id = id; | ||
this.refreshToken = refreshToken; | ||
this.expiration = expiration; | ||
} | ||
|
||
public static UserToken create(Long userId, String refreshToken) { | ||
return new UserToken(userId, refreshToken, REFRESH_TOKEN_EXPIRE_DAY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package in.koreatech.koin.domain.user; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum UserType { | ||
STUDENT("STUDENT", "학생"), | ||
USER("USER", "사용자"), | ||
; | ||
|
||
private final String value; | ||
private final String description; | ||
|
||
UserType(String value, String description) { | ||
this.value = value; | ||
this.description = description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.