-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MemberId가 담긴 Access Token 생성, JWT_SECRET_KEY 추가
- Loading branch information
Showing
2 changed files
with
26 additions
and
18 deletions.
There are no files selected for viewing
40 changes: 23 additions & 17 deletions
40
src/main/java/com/dnd/dndtravel/auth/service/AuthTokenService.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 |
---|---|---|
@@ -1,45 +1,51 @@ | ||
package com.dnd.dndtravel.auth.service; | ||
|
||
import com.dnd.dndtravel.auth.domain.AuthToken; | ||
import com.dnd.dndtravel.auth.dto.AuthMember; | ||
import com.dnd.dndtravel.auth.repository.AuthTokenRepository; | ||
import com.dnd.dndtravel.member.domain.Member; | ||
import com.dnd.dndtravel.member.service.MemberService; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Date; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class AuthTokenService { | ||
private final AuthTokenRepository authTokenRepository; | ||
|
||
private final MemberService memberService; | ||
|
||
public static ConcurrentHashMap<String, AuthMember> accessTokenMap = new ConcurrentHashMap(256); | ||
private final AuthTokenRepository authTokenRepository; | ||
private static final String secretKey = "${JWT_SECRET_KEY}"; | ||
|
||
@Transactional | ||
public AuthToken issue(final Member member) { | ||
final Optional<AuthToken> byMemberId = authTokenRepository.findByMemberId(member.getId()); | ||
return getInnerAuthToken(member, byMemberId); | ||
} | ||
|
||
private AuthToken getInnerAuthToken(Member member, Optional<AuthToken> byMemberId) { | ||
AuthToken authToken; | ||
|
||
if (byMemberId.isEmpty()) { | ||
String accessToken = createAccessToken(member.getId()); // Access Token 생성 시 MemberId 포함 | ||
authToken = authTokenRepository.save(AuthToken.of(member.getId())); | ||
} else { | ||
authToken = byMemberId.get(); | ||
final String accessToken = authToken.getAccessToken(); | ||
accessTokenMap.remove(accessToken); | ||
authToken.reIssuance(); | ||
} | ||
log.info("memberId: " + member.getId()); | ||
log.info("access 토근 발급: " + authToken.getAccessToken()); | ||
log.info("refresh 토근 발급: " + authToken.getRefreshToken()); | ||
final AuthMember authMember = new AuthMember(member); | ||
accessTokenMap.put(authToken.getAccessToken(), authMember); | ||
return authToken; | ||
} | ||
} | ||
|
||
private String createAccessToken(Long memberId) { | ||
Date now = new Date(); | ||
|
||
return Jwts.builder() | ||
.subject(String.valueOf(memberId)) | ||
.issuedAt(now) | ||
.expiration(new Date(now.getTime() + 1000 * 60 * 60 * 24)) //만료 기간 1일 | ||
.signWith(SignatureAlgorithm.HS256, secretKey) | ||
.compact(); | ||
} | ||
} |
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