Skip to content

Commit

Permalink
✨ Create Access Token 메소드 생성
Browse files Browse the repository at this point in the history
MemberId가 담긴 Access Token 생성, JWT_SECRET_KEY 추가
  • Loading branch information
min-0 committed Aug 18, 2024
1 parent 5e830b3 commit c8e51f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
40 changes: 23 additions & 17 deletions src/main/java/com/dnd/dndtravel/auth/service/AuthTokenService.java
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();
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ spring:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
dialect: org.hibernate.dialect.MySQLDialect
jwt:
secret-key: ${JWT_SECRET_KEY}

0 comments on commit c8e51f5

Please sign in to comment.