Skip to content

Commit

Permalink
[Feat] 스프링 시큐리티 & 비밀번호 암호 - #63
Browse files Browse the repository at this point in the history
  • Loading branch information
82everywin committed May 27, 2024
1 parent 3a7095e commit a7f87f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public ResponseEntity<MemberResponseDto> createMember(@Valid @RequestBody Member

@Operation(summary = "로그인")
@PostMapping("/login")
public String loginMember(@RequestBody @Valid MemberRequestDto.LoginRequestDto request){
public ResponseEntity<String> loginMember(@RequestBody @Valid MemberRequestDto.LoginRequestDto request){
MemberLoginResponseDto responseDto = memberService.login(request);

String jwtToken = jwtTokenProvider.createToken(request.getLoginId());

return jwtToken;
return ResponseEntity.status(HttpStatus.OK).body(jwtToken);
// return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}
@Operation(summary = "회원정보 수정")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class PasswordConfig {
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.todolist.todolist.dto.member.MemberResponseDto;
import com.todolist.todolist.repository.MemberRepository;

import com.todolist.todolist.security.PasswordConfig;
import com.todolist.todolist.validators.BaseException;
import com.todolist.todolist.validators.ErrorCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,6 +22,8 @@ public class MemberService {

private final MemberRepository memberRepository;

private final PasswordConfig passwordConfig;

/*
1. 가입
2. 로그인
Expand All @@ -36,6 +39,8 @@ public MemberResponseDto create(MemberRequestDto request) {
throw new BaseException(ErrorCode.DUPLICATE_LOGINID);

Member member = MemberMapper.INSTANCE.toEntity(request);
// 비밀번호 암호화
member.updatePassword(passwordConfig.passwordEncoder().encode(request.getPassword()));
memberRepository.save(member);

return MemberMapper.INSTANCE.toDto(member);
Expand All @@ -45,7 +50,7 @@ public MemberResponseDto create(MemberRequestDto request) {
// 2. 로그인
public MemberLoginResponseDto login(MemberRequestDto.LoginRequestDto request){
Member member = throwFindbyLoginId(request.getLoginId());
if (!member.getPassword().equals(request.getPassword()))
if (!passwordConfig.passwordEncoder().matches(request.getPassword(), member.getPassword()))
throw new BaseException(ErrorCode.UNAUTHORIZED_LOGIN);
return new MemberLoginResponseDto(member.getLoginId());
}
Expand Down Expand Up @@ -74,7 +79,7 @@ public MemberResponseDto update(Long id, MemberRequestDto request){
throw new BaseException(ErrorCode.DUPLICATE_LOGINID);
else {
member.updateLoginId(request.getLoginId());
member.updatePassword(request.getPassword());
member.updatePassword(passwordConfig.passwordEncoder().encode(request.getPassword()));
member.updateName(request.getName());
}

Expand Down

0 comments on commit a7f87f0

Please sign in to comment.