-
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.
- Loading branch information
Showing
7 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
.../src/main/java/com/example/SecondSeminar/common/config/security/BCryptPasswordConfig.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,16 @@ | ||
package com.example.SecondSeminar.common.config.security; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Configuration | ||
public class BCryptPasswordConfig { | ||
private static final int STRENGTH = 10; | ||
|
||
@Bean | ||
public PasswordEncoder bCryptPasswordEncoder() { | ||
return new BCryptPasswordEncoder(STRENGTH); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...Seminar/src/main/java/com/example/SecondSeminar/member/application/AuthMemberService.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,39 @@ | ||
package com.example.SecondSeminar.member.application; | ||
|
||
import com.example.SecondSeminar.common.exception.BaseCustomException; | ||
import com.example.SecondSeminar.member.domain.AuthMember; | ||
import com.example.SecondSeminar.member.domain.AuthMemberJpaRepository; | ||
import com.example.SecondSeminar.member.dto.request.AuthMemberRequest; | ||
import com.example.SecondSeminar.member.exception.MemberExceptionType; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class AuthMemberService { | ||
|
||
private final AuthMemberJpaRepository authMemberJpaRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
@Transactional | ||
public String create(AuthMemberRequest request) { | ||
AuthMember authMember = AuthMember.builder() | ||
.nickname(request.nickname()) | ||
.password(passwordEncoder.encode(request.password())) | ||
.build(); | ||
authMemberJpaRepository.save(authMember); | ||
|
||
return authMember.getId().toString(); | ||
} | ||
|
||
public void signIn(AuthMemberRequest request) { | ||
AuthMember serviceMember = authMemberJpaRepository.findByNickname(request.nickname()) | ||
.orElseThrow(() -> new BaseCustomException(MemberExceptionType.NOT_FOUND_MEMBER)); | ||
if (!passwordEncoder.matches(request.password(), serviceMember.getPassword())) { | ||
throw new BaseCustomException(MemberExceptionType.INCORRECT_PASSWORD); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...minar/SecondSeminar/src/main/java/com/example/SecondSeminar/member/domain/AuthMember.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,29 @@ | ||
package com.example.SecondSeminar.member.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
public class AuthMember { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String nickname; | ||
private String password; | ||
|
||
@Builder | ||
public AuthMember(String nickname, String password) { | ||
this.nickname = nickname; | ||
this.password = password; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...eminar/src/main/java/com/example/SecondSeminar/member/domain/AuthMemberJpaRepository.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,8 @@ | ||
package com.example.SecondSeminar.member.domain; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AuthMemberJpaRepository extends JpaRepository<AuthMember, Long> { | ||
Optional<AuthMember> findByNickname(String nickname); | ||
} |
7 changes: 7 additions & 0 deletions
7
...Seminar/src/main/java/com/example/SecondSeminar/member/dto/request/AuthMemberRequest.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,7 @@ | ||
package com.example.SecondSeminar.member.dto.request; | ||
|
||
public record AuthMemberRequest( | ||
String nickname, | ||
String password | ||
) { | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...nar/src/main/java/com/example/SecondSeminar/member/presentation/AuthMemberController.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,31 @@ | ||
package com.example.SecondSeminar.member.presentation; | ||
|
||
import com.example.SecondSeminar.member.application.AuthMemberService; | ||
import com.example.SecondSeminar.member.dto.request.AuthMemberRequest; | ||
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.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequestMapping("/api/users/") | ||
@RequiredArgsConstructor | ||
@RestController | ||
public class AuthMemberController { | ||
|
||
private final AuthMemberService authMemberService; | ||
|
||
@PostMapping("sign-up") | ||
public ResponseEntity<Void> signUp(@RequestBody AuthMemberRequest request) { | ||
URI location = URI.create(authMemberService.create(request)); | ||
return ResponseEntity.created(location).build(); | ||
} | ||
|
||
@PostMapping("sign-in") | ||
public ResponseEntity<Void> signIn(@RequestBody AuthMemberRequest request) { | ||
authMemberService.signIn(request); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |