-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
f1b731c
commit 88ea3e1
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
server-toy-project/src/main/java/com/gdsc_teamb/servertoyproject/login/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,36 @@ | ||
package com.gdsc_teamb.servertoyproject.login; | ||
|
||
import com.gdsc_teamb.servertoyproject.jwt.TokenInfo; | ||
import com.gdsc_teamb.servertoyproject.jwt.JwtTokenProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final AuthenticationManagerBuilder authenticationManagerBuilder; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
@Transactional | ||
public TokenInfo login(String memberId, String password) { | ||
// 1. Login ID/PW 를 기반으로 Authentication 객체 생성 | ||
// 이때 authentication 는 인증 여부를 확인하는 authenticated 값이 false | ||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(memberId, password); | ||
|
||
// 2. 실제 검증 (사용자 비밀번호 체크)이 이루어지는 부분 | ||
// authenticate 매서드가 실행될 때 CustomUserDetailsService 에서 만든 loadUserByUsername 메서드가 실행 | ||
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken); | ||
|
||
// 3. 인증 정보를 기반으로 JWT 토큰 생성 | ||
TokenInfo tokenInfo = jwtTokenProvider.generateToken(authentication); | ||
|
||
return tokenInfo; | ||
} | ||
} |