-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: 회원가입 기능 추가
- Loading branch information
Showing
46 changed files
with
1,056 additions
and
21 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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/integrated/techhub/auth/application/AuthQueryService.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,20 @@ | ||
package com.integrated.techhub.auth.application; | ||
|
||
import com.integrated.techhub.member.domain.repository.MemberRepository; | ||
import com.integrated.techhub.member.exception.MemberExistsException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthQueryService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public void validateExistedMember(final String email) { | ||
if (memberRepository.existsByEmail(email)) { | ||
throw new MemberExistsException(); | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/integrated/techhub/auth/application/AuthService.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,27 @@ | ||
package com.integrated.techhub.auth.application; | ||
|
||
import com.integrated.techhub.auth.domain.PasswordEncoder; | ||
import com.integrated.techhub.auth.dto.SignUpRequest; | ||
import com.integrated.techhub.member.domain.Member; | ||
import com.integrated.techhub.member.domain.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final AuthQueryService authQueryService; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
public Long registerMember(final SignUpRequest request) { | ||
authQueryService.validateExistedMember(request.email()); | ||
Member member = request.toEntity(); | ||
member.encodePassword(passwordEncoder); | ||
return memberRepository.save(member).getId(); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/integrated/techhub/auth/domain/PasswordEncoder.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,9 @@ | ||
package com.integrated.techhub.auth.domain; | ||
|
||
public interface PasswordEncoder { | ||
|
||
String encode(String rawPassword); | ||
|
||
boolean isMatch(String rawPassword, String encryptedPassword); | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/integrated/techhub/auth/dto/SignUpRequest.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,48 @@ | ||
package com.integrated.techhub.auth.dto; | ||
|
||
import com.integrated.techhub.member.domain.Member; | ||
import com.integrated.techhub.member.domain.Position; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
|
||
public record SignUpRequest( | ||
@Email(message = "이메일 형식이어야 합니다. 올바른 형식인지 확인해주세요.") | ||
@NotBlank(message = "Null 또는 공백이 포함될 수 없습니다. 올바른 값인지 확인해주세요.") | ||
String email, | ||
|
||
@Pattern(regexp = "^(?=.*[a-zA-Z])(?=.*\\d).{8,}$", message = "최소 8자, 문자 1개와 숫자 1개 이상을 포함해야합니다. 다시 입력해주세요.") | ||
@NotBlank(message = "Null 또는 공백이 포함될 수 없습니다. 올바른 값인지 확인해주세요.") | ||
String password, | ||
|
||
@NotBlank(message = "Null 또는 공백이 포함될 수 없습니다. 올바른 값인지 확인해주세요.") | ||
String nickname, | ||
|
||
@NotBlank(message = "Null 또는 공백이 포함될 수 없습니다. 올바른 값인지 확인해주세요.") | ||
String githubUserName, | ||
|
||
@NotBlank(message = "Null 또는 공백이 포함될 수 없습니다. 올바른 값인지 확인해주세요.") | ||
String name, | ||
|
||
@Pattern(regexp = "^(BE|FE|AOS)$", message = "BE, FE, AOS만 입력할 수 있습니다. 알맞은 포지션인지 확인해주세요.") | ||
@NotBlank(message = "Null 또는 공백이 포함될 수 없습니다. 올바른 값인지 확인해주세요.") | ||
String position, | ||
|
||
@NotNull(message = "Null이 포함될 수 없습니다. 올바른 값인지 확인해주세요.") | ||
Integer cardinalNumber | ||
) { | ||
|
||
public Member toEntity() { | ||
return Member.builder() | ||
.email(email) | ||
.password(password) | ||
.nickname(nickname) | ||
.githubUsername(githubUserName) | ||
.name(name) | ||
.position(Position.valueOf(position)) | ||
.cardinalNumber(cardinalNumber) | ||
.build(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/integrated/techhub/auth/infra/encode/BCryptPasswordEncoder.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,20 @@ | ||
package com.integrated.techhub.auth.infra.encode; | ||
|
||
import com.integrated.techhub.auth.domain.PasswordEncoder; | ||
import org.mindrot.jbcrypt.BCrypt; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class BCryptPasswordEncoder implements PasswordEncoder { | ||
|
||
@Override | ||
public String encode(String rawPassword) { | ||
return BCrypt.hashpw(rawPassword, BCrypt.gensalt()); | ||
} | ||
|
||
@Override | ||
public boolean isMatch(String rawPassword, String encryptedPassword) { | ||
return BCrypt.checkpw(rawPassword, encryptedPassword); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/integrated/techhub/auth/presentation/AuthController.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,28 @@ | ||
package com.integrated.techhub.auth.presentation; | ||
|
||
import com.integrated.techhub.auth.application.AuthService; | ||
import com.integrated.techhub.auth.dto.SignUpRequest; | ||
import jakarta.validation.Valid; | ||
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.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.net.URI; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/auth") | ||
public class AuthController { | ||
|
||
private final AuthService authService; | ||
|
||
@PostMapping("/sign-up") | ||
public ResponseEntity<Void> signUp(@RequestBody @Valid final SignUpRequest request) { | ||
final Long memberId = authService.registerMember(request); | ||
return ResponseEntity.created(URI.create("/members/" + memberId)).build(); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/integrated/techhub/common/config/AsyncConfig.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,37 @@ | ||
package com.integrated.techhub.common.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
@Slf4j | ||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
@Override | ||
@Bean(name = "MailExecutor") | ||
public Executor getAsyncExecutor() { | ||
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(2); | ||
executor.setMaxPoolSize(5); | ||
executor.setQueueCapacity(10); | ||
executor.setThreadNamePrefix("MailExecutor-"); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return (ex, method, params) -> | ||
log.error("Exception handler for async method '" + method.toGenericString() | ||
+ "' threw unexpected exception itself", ex); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/integrated/techhub/common/config/EmbeddedRedisConfig.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,88 @@ | ||
package com.integrated.techhub.common.config; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.StringUtils; | ||
import redis.embedded.RedisServer; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class EmbeddedRedisConfig { | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int redisPort; | ||
|
||
private RedisServer redisServer; | ||
|
||
@PostConstruct | ||
public void redisServer() throws IOException { | ||
int port = isRedisRunning()? findAvailablePort() : redisPort; | ||
redisServer = new RedisServer(port); | ||
redisServer.start(); | ||
} | ||
|
||
/** | ||
* Embedded Redis가 현재 실행중인지 확인 | ||
*/ | ||
private boolean isRedisRunning() throws IOException { | ||
return isRunning(executeGrepProcessCommand(redisPort)); | ||
} | ||
|
||
/** | ||
* 해당 port를 사용중인 프로세스 확인하는 sh 실행 | ||
*/ | ||
private Process executeGrepProcessCommand(int port) throws IOException { | ||
String command = String.format("netstat -nat | grep LISTEN|grep %d", port); | ||
String[] shell = {"/bin/sh", "-c", command}; | ||
return Runtime.getRuntime().exec(shell); | ||
} | ||
|
||
/** | ||
* 해당 Process가 현재 실행중인지 확인 | ||
*/ | ||
private boolean isRunning(Process process) { | ||
String line; | ||
StringBuilder pidInfo = new StringBuilder(); | ||
|
||
try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) { | ||
|
||
while ((line = input.readLine()) != null) { | ||
pidInfo.append(line); | ||
} | ||
|
||
} catch (Exception e) { | ||
} | ||
|
||
return !StringUtils.isEmpty(pidInfo.toString()); | ||
} | ||
|
||
/** | ||
* 현재 PC/서버에서 사용가능한 포트 조회 | ||
*/ | ||
public int findAvailablePort() throws IOException { | ||
|
||
for (int port = 10000; port <= 65535; port++) { | ||
Process process = executeGrepProcessCommand(port); | ||
if (!isRunning(process)) { | ||
return port; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Not Found Available port: 10000 ~ 65535"); | ||
} | ||
|
||
@PreDestroy | ||
public void stopRedis() { | ||
if (redisServer != null) { | ||
redisServer.stop(); | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/integrated/techhub/common/exception/ErrorCode.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,10 @@ | ||
package com.integrated.techhub.common.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public record ErrorCode( | ||
HttpStatus status, | ||
String message | ||
) { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/integrated/techhub/common/exception/ErrorResponse.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,13 @@ | ||
package com.integrated.techhub.common.exception; | ||
|
||
public record ErrorResponse(int status, String message) { | ||
|
||
@Override | ||
public String toString() { | ||
return "{\n" + | ||
"\t\"status\": " + status + | ||
",\n\t\"message\": \"" + message + '\"' + | ||
"\n}"; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/integrated/techhub/common/exception/TechHubException.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,15 @@ | ||
package com.integrated.techhub.common.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class TechHubException extends RuntimeException { | ||
|
||
private final ErrorCode errorCode; | ||
|
||
public TechHubException(ErrorCode errorCode) { | ||
super(errorCode.message()); | ||
this.errorCode = errorCode; | ||
} | ||
|
||
} |
Oops, something went wrong.