Skip to content

Commit

Permalink
feat: jwt 발급을 위한 코드 추가 #21
Browse files Browse the repository at this point in the history
  • Loading branch information
heokyeongju committed Nov 7, 2022
1 parent 34e4d66 commit dc9b9d1
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<Member> findByEmail(String email){
Expand Down Expand Up @@ -112,6 +116,22 @@ public RsData<AddCashRsDataBody> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String, Object> getClaims(String token) {
String body = Jwts.parserBuilder()
.setSigningKey(getSecretKey())
.build()
.parseClaimsJws(token)
.getBody()
.get("body", String.class);

return Util.json.toMap(body);
}
}
4 changes: 3 additions & 1 deletion 4Week_Mission/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ logging:
custom:
site:
name: MUTBOOKS
baseUrl: "http://localhost:8010"
baseUrl: "http://localhost:8010"
jwt:
secretKey: secretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKeysecretKey

0 comments on commit dc9b9d1

Please sign in to comment.