From dc9b9d135ab7dfc835614ed14667550148232757 Mon Sep 17 00:00:00 2001 From: heokyeongju Date: Mon, 7 Nov 2022 16:46:40 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20jwt=20=EB=B0=9C=EA=B8=89=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?#21?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/service/MemberService.java | 20 +++++++ .../app/security/jwt/JwtConfig.java | 20 +++++++ .../app/security/jwt/JwtProvider.java | 56 +++++++++++++++++++ .../src/main/resources/application.yml | 4 +- 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtConfig.java create mode 100644 4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtProvider.java diff --git a/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/domain/member/service/MemberService.java b/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/domain/member/service/MemberService.java index 9ef9b9b..ccccd08 100644 --- a/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/domain/member/service/MemberService.java +++ b/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/domain/member/service/MemberService.java @@ -9,6 +9,7 @@ import com.mutbook.week4_mission.app.base.exception.AlreadyExistException; import com.mutbook.week4_mission.app.domain.member.repository.MemberRepository; import com.mutbook.week4_mission.app.security.dto.MemberContext; +import com.mutbook.week4_mission.app.security.jwt.JwtProvider; import lombok.AllArgsConstructor; import lombok.Data; import lombok.RequiredArgsConstructor; @@ -18,6 +19,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; import java.util.Optional; @@ -30,6 +32,8 @@ public class MemberService { private final PasswordEncoder passwordEncoder; private final EmailService emailService; private final CashService cashService; + private final JwtProvider jwtProvider; + public Optional findByEmail(String email){ @@ -112,6 +116,22 @@ public RsData addCash(Member member, long price, String event new AddCashRsDataBody(cashLog, newRestCash) ); } + + @Transactional + public String genAccessToken(Member member) { + String accessToken = member.getAccessToken(); + + if (StringUtils.hasLength(accessToken) == false) { + accessToken = jwtProvider.generateAccessToken(member.getAccessTokenClaims(), 60L * 60 * 24 * 365 * 100); + member.setAccessToken(accessToken); + } + + return accessToken; + } + + public boolean verifyWithWhiteList(Member member, String token) { + return member.getAccessToken().equals(token); + } @Data @AllArgsConstructor public static class AddCashRsDataBody { diff --git a/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtConfig.java b/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtConfig.java new file mode 100644 index 0000000..b699b1d --- /dev/null +++ b/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtConfig.java @@ -0,0 +1,20 @@ +package com.mutbook.week4_mission.app.security.jwt; + +import io.jsonwebtoken.security.Keys; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import javax.crypto.SecretKey; +import java.util.Base64; + +@Configuration +public class JwtConfig { + @Value("${custom.jwt.secretKey}") + private String secretKeyPlain; + + @Bean + public SecretKey jwtSecretKey() { + String keyBase64Encoded = Base64.getEncoder().encodeToString(secretKeyPlain.getBytes()); + return Keys.hmacShaKeyFor(keyBase64Encoded.getBytes()); + } +} diff --git a/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtProvider.java b/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtProvider.java new file mode 100644 index 0000000..880c832 --- /dev/null +++ b/4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtProvider.java @@ -0,0 +1,56 @@ +package com.mutbook.week4_mission.app.security.jwt; + +import com.mutbook.week4_mission.util.Util; +import io.jsonwebtoken.SignatureAlgorithm; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import io.jsonwebtoken.Jwts; +import javax.crypto.SecretKey; +import java.util.Date; +import java.util.Map; + +@Component +@RequiredArgsConstructor +public class JwtProvider { + private final SecretKey jwtSecretKey; + + private SecretKey getSecretKey() { + return jwtSecretKey; + } + + public String generateAccessToken(Map claims, long seconds) { + long now = new Date().getTime(); + Date accessTokenExpiresIn = new Date(now + 1000L * seconds); + + return Jwts.builder() + .claim("body", Util.json.toStr(claims)) + .setExpiration(accessTokenExpiresIn) + .signWith(getSecretKey(), SignatureAlgorithm.HS512) + .compact(); + } + + public boolean verify(String token) { + try { + Jwts.parserBuilder() + .setSigningKey(getSecretKey()) + .build() + .parseClaimsJws(token); + } catch (Exception e) { + return false; + } + + return true; + } + + public Map getClaims(String token) { + String body = Jwts.parserBuilder() + .setSigningKey(getSecretKey()) + .build() + .parseClaimsJws(token) + .getBody() + .get("body", String.class); + + return Util.json.toMap(body); + } +} diff --git a/4Week_Mission/src/main/resources/application.yml b/4Week_Mission/src/main/resources/application.yml index 3b74f54..2674125 100644 --- a/4Week_Mission/src/main/resources/application.yml +++ b/4Week_Mission/src/main/resources/application.yml @@ -31,4 +31,6 @@ logging: custom: site: name: MUTBOOKS - baseUrl: "http://localhost:8010" \ No newline at end of file + baseUrl: "http://localhost:8010" + jwt: + secretKey: secretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKey