Skip to content

Commit 9704394

Browse files
committed
feat(CustomAuthenticationProvider): AuthenticationManager 의 authentication 처리 커스텀
- CenterSignUseCase를 사용하여 계정 ID로 센터 ID와 인코딩된 비밀번호 조회 - PasswordEncoder로 비밀번호 검증 후, JwtUseCase를 통해 액세스 토큰 생성 - UsernamePasswordAuthenticationToken 인증 성공 시 JwtAuthenticationToken 반환
1 parent 4ba392a commit 9704394

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.somemore.auth.idpw.provider;
2+
3+
import com.somemore.auth.authentication.JwtAuthenticationToken;
4+
import com.somemore.auth.jwt.domain.EncodedToken;
5+
import com.somemore.auth.jwt.domain.TokenType;
6+
import com.somemore.auth.jwt.domain.UserRole;
7+
import com.somemore.auth.jwt.usecase.JwtUseCase;
8+
import com.somemore.center.usecase.query.CenterSignUseCase;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.security.authentication.AuthenticationProvider;
11+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
12+
import org.springframework.security.core.Authentication;
13+
import org.springframework.security.core.AuthenticationException;
14+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
15+
import org.springframework.security.crypto.password.PasswordEncoder;
16+
import org.springframework.stereotype.Component;
17+
import org.springframework.transaction.annotation.Transactional;
18+
19+
import java.util.List;
20+
21+
@Component
22+
@RequiredArgsConstructor
23+
@Transactional(readOnly = true)
24+
public class CustomAuthenticationProvider implements AuthenticationProvider {
25+
26+
private final CenterSignUseCase centerSignUseCase;
27+
private final JwtUseCase jwtUseCase;
28+
private final PasswordEncoder passwordEncoder;
29+
30+
@Override
31+
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
32+
String accountId = authentication.getName();
33+
String rawAccountPassword = authentication.getCredentials().toString();
34+
35+
String centerId = centerSignUseCase.getIdByAccountId(accountId).toString();
36+
String encodedPassword = centerSignUseCase.getPasswordByAccountId(accountId);
37+
38+
if (passwordEncoder.matches(rawAccountPassword, encodedPassword)) {
39+
EncodedToken accessToken = jwtUseCase.generateToken(
40+
centerId,
41+
UserRole.CENTER.getAuthority(),
42+
TokenType.ACCESS
43+
);
44+
45+
return new JwtAuthenticationToken(
46+
centerId,
47+
accessToken,
48+
List.of(new SimpleGrantedAuthority(UserRole.CENTER.getAuthority()))
49+
);
50+
}
51+
52+
return null;
53+
}
54+
55+
@Override
56+
public boolean supports(Class<?> authentication) {
57+
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
58+
}
59+
}

0 commit comments

Comments
 (0)