Skip to content

Commit 88ea3e1

Browse files
committed
feat: Add MemberService (#3)
1 parent f1b731c commit 88ea3e1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.gdsc_teamb.servertoyproject.login;
2+
3+
import com.gdsc_teamb.servertoyproject.jwt.TokenInfo;
4+
import com.gdsc_teamb.servertoyproject.jwt.JwtTokenProvider;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
7+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
8+
import org.springframework.security.core.Authentication;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@Service
13+
@Transactional(readOnly = true)
14+
@RequiredArgsConstructor
15+
public class MemberService {
16+
17+
private final MemberRepository memberRepository;
18+
private final AuthenticationManagerBuilder authenticationManagerBuilder;
19+
private final JwtTokenProvider jwtTokenProvider;
20+
21+
@Transactional
22+
public TokenInfo login(String memberId, String password) {
23+
// 1. Login ID/PW 를 기반으로 Authentication 객체 생성
24+
// 이때 authentication 는 인증 여부를 확인하는 authenticated 값이 false
25+
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(memberId, password);
26+
27+
// 2. 실제 검증 (사용자 비밀번호 체크)이 이루어지는 부분
28+
// authenticate 매서드가 실행될 때 CustomUserDetailsService 에서 만든 loadUserByUsername 메서드가 실행
29+
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
30+
31+
// 3. 인증 정보를 기반으로 JWT 토큰 생성
32+
TokenInfo tokenInfo = jwtTokenProvider.generateToken(authentication);
33+
34+
return tokenInfo;
35+
}
36+
}

0 commit comments

Comments
 (0)