-
Notifications
You must be signed in to change notification settings - Fork 4
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
νμ κ΄λ¦¬ κΈ°λ₯
- Loading branch information
Showing
13 changed files
with
208 additions
and
2 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
61 changes: 61 additions & 0 deletions
61
src/main/java/jungle/HandTris/application/service/MemberService.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,61 @@ | ||
package jungle.HandTris.application.service; | ||
|
||
import jungle.HandTris.domain.Member; | ||
import jungle.HandTris.domain.exception.DuplicateNicknameException; | ||
import jungle.HandTris.domain.exception.DuplicateUsernameException; | ||
import jungle.HandTris.domain.exception.PasswordMismatchException; | ||
import jungle.HandTris.domain.exception.UserNotFoundException; | ||
import jungle.HandTris.domain.repo.MemberRepository; | ||
import jungle.HandTris.global.exception.ErrorCode; | ||
import jungle.HandTris.presentation.dto.request.MemberRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import jakarta.transaction.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MemberService { | ||
private final MemberRepository memberRepository; | ||
private final BCryptPasswordEncoder bCryptPasswordEncoder; | ||
|
||
@Transactional | ||
public Long signin (MemberRequest memberRequest) { | ||
String username = memberRequest.username(); | ||
String password = memberRequest.password(); | ||
|
||
Member member = Optional.ofNullable(memberRepository.findByUsername(username)) | ||
.orElseThrow(() -> new UserNotFoundException()); | ||
|
||
// λΉλ°λ²νΈ νμΈ | ||
if (!bCryptPasswordEncoder.matches(password, member.getPassword())) { | ||
throw new PasswordMismatchException(); | ||
} | ||
|
||
return member.getId(); | ||
} | ||
|
||
@Transactional | ||
public void signup(MemberRequest memberRequest) { | ||
boolean usernameExists = memberRepository.existsByUsername(memberRequest.username()); | ||
boolean nicknameExists = memberRepository.existsByNickname(memberRequest.nickname()); | ||
|
||
if (nicknameExists) { | ||
throw new DuplicateNicknameException(); | ||
} | ||
|
||
if (usernameExists) { | ||
throw new DuplicateUsernameException(); | ||
} | ||
|
||
String username = memberRequest.username(); | ||
String password = memberRequest.password(); | ||
String nickname = memberRequest.nickname(); | ||
|
||
Member data = new Member(username, bCryptPasswordEncoder.encode(password), nickname); | ||
|
||
memberRepository.save(data); | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
src/main/java/jungle/HandTris/domain/exception/DuplicateNicknameException.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,4 @@ | ||
package jungle.HandTris.domain.exception; | ||
|
||
public class DuplicateNicknameException extends RuntimeException{ | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/jungle/HandTris/domain/exception/DuplicateUsernameException.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,4 @@ | ||
package jungle.HandTris.domain.exception; | ||
|
||
public class DuplicateUsernameException extends RuntimeException { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/jungle/HandTris/domain/exception/PasswordMismatchException.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,4 @@ | ||
package jungle.HandTris.domain.exception; | ||
|
||
public class PasswordMismatchException extends RuntimeException { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/jungle/HandTris/domain/exception/UserNotFoundException.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,4 @@ | ||
package jungle.HandTris.domain.exception; | ||
|
||
public class UserNotFoundException extends RuntimeException { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/jungle/HandTris/domain/repo/MemberRepository.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,12 @@ | ||
package jungle.HandTris.domain.repo; | ||
|
||
import jungle.HandTris.domain.Member; | ||
import org.springframework.data.repository.Repository; | ||
|
||
public interface MemberRepository extends Repository<Member, Long> { | ||
Member findByUsername(String username); | ||
boolean existsByUsername(String username); | ||
boolean existsByNickname(String nickname); | ||
|
||
Member save(Member member); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/jungle/HandTris/global/config/Security/SecurityConfig.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 jungle.HandTris.global.config.Security; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
|
||
@Bean | ||
public BCryptPasswordEncoder bCryptPasswordEncoder() { | ||
|
||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
|
||
http | ||
.authorizeHttpRequests(auth -> auth | ||
.anyRequest().permitAll() | ||
) | ||
.csrf((auth) -> auth.disable() | ||
); | ||
|
||
return http.build(); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/main/java/jungle/HandTris/presentation/controller/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,34 @@ | ||
package jungle.HandTris.presentation.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import jungle.HandTris.application.service.MemberService; | ||
import jungle.HandTris.global.dto.ResponseEnvelope; | ||
import jungle.HandTris.presentation.dto.request.MemberRequest; | ||
import jungle.HandTris.presentation.dto.response.MemberIdDetails; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/auth") | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
|
||
private final MemberService memberService; | ||
|
||
@PostMapping("/signup") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public ResponseEnvelope<String> signup(@RequestBody @Valid MemberRequest memberRequest) { | ||
memberService.signup(memberRequest); | ||
|
||
return ResponseEnvelope.of("Signup Successful"); | ||
} | ||
|
||
@PostMapping("/signin") | ||
public ResponseEnvelope<MemberIdDetails> signin(@RequestBody MemberRequest memberRequest) { | ||
Long memberId = memberService.signin(memberRequest); | ||
|
||
return ResponseEnvelope.of(new MemberIdDetails(memberId)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/jungle/HandTris/presentation/dto/request/MemberRequest.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 jungle.HandTris.presentation.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Pattern; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record MemberRequest(@NotBlank(message = "μμ΄λλ νμ μ λ ₯ κ°μ λλ€.") | ||
@Size(min = 4, max = 10, message = "μμ΄λλ μ΅μ 4μ μ΄μ, 10μ μ΄νμ¬μΌ ν©λλ€") | ||
@Pattern(regexp = "^[a-z0-9]+$", message = "μμ΄λλ μνλ²³ μλ¬Έμ(a~z), μ«μ(0~9)λ‘ κ΅¬μ±λμ΄μΌ ν©λλ€.") | ||
@Pattern(regexp = "^(?!.*admin$)", message = "adminμ μ¬μ©ν μ μμ΅λλ€.") | ||
String username, | ||
|
||
@NotBlank(message = "λΉλ°λ²νΈλ νμ μ λ ₯ κ°μ λλ€.") | ||
@Size(min = 8, max = 15, message = "λΉλ°λ²νΈλ 8μ μ΄μ 15μ μ΄νμ¬μΌ ν©λλ€.") | ||
@Pattern(regexp = "^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[!@#$%^&*])[a-zA-Z\\d!@#$%^&*]+$", message = "λΉλ°λ²νΈλ μλ¬Έ, μ«μ, νΉμλ¬Έμλ₯Ό κ°κ° 1κ° μ΄μ ν¬ν¨ν΄μΌ ν©λλ€.") | ||
String password, | ||
|
||
@NotBlank(message = "λλ€μμ νμ μ λ ₯ κ°μ λλ€.") | ||
@Size(min = 4, max = 20, message = "λλ€μμ μ΅μ 4μ μ΄μ, 20μ μ΄νμ¬μΌ ν©λλ€") | ||
@Pattern(regexp = "^[a-zA-Z0-9]+$", message = "λλ€μμ μνλ²³ λμλ¬Έμμ μ«μλ§ μ¬μ© κ°λ₯ν©λλ€.") | ||
@Pattern(regexp = "^(?!.*admin$)", message = "adminμ μ¬μ©ν μ μμ΅λλ€.") | ||
String nickname) { | ||
|
||
} | ||
|
4 changes: 4 additions & 0 deletions
4
src/main/java/jungle/HandTris/presentation/dto/response/MemberIdDetails.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,4 @@ | ||
package jungle.HandTris.presentation.dto.response; | ||
|
||
public record MemberIdDetails(Long Id) { | ||
} |