mirror of
https://github.com/ktds-dg0501/kt-event-marketing.git
synced 2025-12-06 06:06:24 +00:00
UserPricipal 충돌 부분 조치
This commit is contained in:
parent
1168ba2d1d
commit
ea807cf33e
@ -49,17 +49,19 @@ public class JwtTokenProvider {
|
||||
* Access Token 생성
|
||||
*
|
||||
* @param userId 사용자 ID
|
||||
* @param storeId 매장 ID
|
||||
* @param email 이메일
|
||||
* @param name 이름
|
||||
* @param roles 역할 목록
|
||||
* @return Access Token
|
||||
*/
|
||||
public String createAccessToken(Long userId, String email, String name, List<String> roles) {
|
||||
public String createAccessToken(Long userId, Long storeId, String email, String name, List<String> roles) {
|
||||
Date now = new Date();
|
||||
Date expiryDate = new Date(now.getTime() + accessTokenValidityMs);
|
||||
|
||||
return Jwts.builder()
|
||||
.subject(userId.toString())
|
||||
.claim("storeId", storeId != null ? storeId.toString() : null)
|
||||
.claim("email", email)
|
||||
.claim("name", name)
|
||||
.claim("roles", roles)
|
||||
@ -110,12 +112,14 @@ public class JwtTokenProvider {
|
||||
Claims claims = parseToken(token);
|
||||
|
||||
Long userId = Long.parseLong(claims.getSubject());
|
||||
String storeIdStr = claims.get("storeId", String.class);
|
||||
Long storeId = storeIdStr != null ? Long.parseLong(storeIdStr) : null;
|
||||
String email = claims.get("email", String.class);
|
||||
String name = claims.get("name", String.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> roles = claims.get("roles", List.class);
|
||||
|
||||
return new UserPrincipal(userId, email, name, roles);
|
||||
return new UserPrincipal(userId, storeId, email, name, roles);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -23,6 +23,11 @@ public class UserPrincipal implements UserDetails {
|
||||
*/
|
||||
private final Long userId;
|
||||
|
||||
/**
|
||||
* 매장 ID
|
||||
*/
|
||||
private final Long storeId;
|
||||
|
||||
/**
|
||||
* 사용자 이메일
|
||||
*/
|
||||
|
||||
@ -5,8 +5,10 @@ import com.kt.event.common.security.JwtTokenProvider;
|
||||
import com.kt.event.user.dto.request.LoginRequest;
|
||||
import com.kt.event.user.dto.response.LoginResponse;
|
||||
import com.kt.event.user.dto.response.LogoutResponse;
|
||||
import com.kt.event.user.entity.Store;
|
||||
import com.kt.event.user.entity.User;
|
||||
import com.kt.event.user.exception.UserErrorCode;
|
||||
import com.kt.event.user.repository.StoreRepository;
|
||||
import com.kt.event.user.repository.UserRepository;
|
||||
import com.kt.event.user.service.AuthenticationService;
|
||||
import com.kt.event.user.service.UserService;
|
||||
@ -34,6 +36,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class AuthenticationServiceImpl implements AuthenticationService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final StoreRepository storeRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtTokenProvider jwtTokenProvider;
|
||||
private final UserService userService;
|
||||
@ -42,10 +45,12 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
public AuthenticationServiceImpl(UserRepository userRepository,
|
||||
StoreRepository storeRepository,
|
||||
PasswordEncoder passwordEncoder,
|
||||
JwtTokenProvider jwtTokenProvider,
|
||||
UserService userService) {
|
||||
this.userRepository = userRepository;
|
||||
this.storeRepository = storeRepository;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.jwtTokenProvider = jwtTokenProvider;
|
||||
this.userService = userService;
|
||||
@ -68,21 +73,26 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
||||
throw new BusinessException(UserErrorCode.AUTH_FAILED.getErrorCode());
|
||||
}
|
||||
|
||||
// 3. JWT 토큰 생성
|
||||
// 3. 매장 정보 조회
|
||||
Store store = storeRepository.findByUserId(user.getId()).orElse(null);
|
||||
Long storeId = store != null ? store.getId() : null;
|
||||
|
||||
// 4. JWT 토큰 생성
|
||||
String token = jwtTokenProvider.createAccessToken(
|
||||
user.getId(),
|
||||
storeId,
|
||||
user.getEmail(),
|
||||
user.getName(),
|
||||
List.of(user.getRole().name())
|
||||
);
|
||||
|
||||
// 4. Redis 세션 저장 (TTL 7일)
|
||||
// 5. Redis 세션 저장 (TTL 7일)
|
||||
saveSession(token, user.getId(), user.getRole().name());
|
||||
|
||||
// 5. 최종 로그인 시각 업데이트 (비동기)
|
||||
// 6. 최종 로그인 시각 업데이트 (비동기)
|
||||
userService.updateLastLoginAt(user.getId());
|
||||
|
||||
// 6. 응답 반환
|
||||
// 7. 응답 반환
|
||||
return LoginResponse.builder()
|
||||
.token(token)
|
||||
.userId(user.getId())
|
||||
|
||||
@ -103,6 +103,7 @@ public class UserServiceImpl implements UserService {
|
||||
// 6. JWT 토큰 생성
|
||||
String token = jwtTokenProvider.createAccessToken(
|
||||
savedUser.getId(),
|
||||
savedStore.getId(),
|
||||
savedUser.getEmail(),
|
||||
savedUser.getName(),
|
||||
List.of(savedUser.getRole().name())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user