Skip to content

Commit

Permalink
feat: jwt 사용을 위한 member entity 수정 #21
Browse files Browse the repository at this point in the history
  • Loading branch information
heokyeongju committed Nov 7, 2022
1 parent 6ac8eb1 commit 34e4d66
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mutbook.week4_mission.app.base.entity.BaseEntity;
import com.mutbook.week4_mission.util.Util;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -13,8 +14,11 @@

import javax.persistence.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static javax.persistence.EnumType.STRING;

Expand All @@ -38,12 +42,10 @@ public class Member extends BaseEntity {
@Convert(converter = AuthLevel.Converter.class)
@Column(name = "auth_level")
private AuthLevel authLevel;

@Column(name ="cash")
private long cash;
@Enumerated(STRING)
// @Column(name = "type")
// private Type type;
@Column(columnDefinition = "TEXT")
private String accessToken;

public List<GrantedAuthority> genAuthorities() {
List<GrantedAuthority> authorities = new ArrayList<>();
Expand All @@ -56,4 +58,54 @@ public List<GrantedAuthority> genAuthorities() {

return authorities;
}

public static Member fromMap(Map<String, Object> map) {
return fromJwtClaims(map);
}

public static Member fromJwtClaims(Map<String, Object> jwtClaims) {
long id = 0;

if (jwtClaims.get("id") instanceof Long) {
id = (long) jwtClaims.get("id");
} else if (jwtClaims.get("id") instanceof Integer) {
id = (long) (int) jwtClaims.get("id");
}

LocalDateTime createDate = null;
LocalDateTime modifyDate = null;

if (jwtClaims.get("createDate") instanceof List) {
createDate = Util.date.bitsToLocalDateTime((List<Integer>) jwtClaims.get("createDate"));
}

if (jwtClaims.get("modifyDate") instanceof List) {
modifyDate = Util.date.bitsToLocalDateTime((List<Integer>) jwtClaims.get("modifyDate"));
}

String username = (String) jwtClaims.get("username");
String email = (String) jwtClaims.get("email");
String accessToken = (String) jwtClaims.get("accessToken");

return Member
.builder()
.id(id)
.createDate(createDate)
.modifyDate(modifyDate)
.username(username)
.email(email)
.accessToken(accessToken)
.build();
}

public Map<String, Object> getAccessTokenClaims() {
return Util.mapOf(
"id", getId(),
"createDate", getCreateDate(),
"modifyDate", getModifyDate(),
"username", getUsername(),
"email", getEmail(),
"authorities", genAuthorities()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
Expand All @@ -33,6 +34,14 @@ public static Object toStr(Map<String, Object> map) {
return null;
}
}
public static String toStr(RsData rsData) {
try {
return getObjectMapper().writeValueAsString(rsData).toString();
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}

public static Map<String, Object> toMap(String jsonStr) {
try {
Expand Down Expand Up @@ -155,6 +164,9 @@ public static String getQueryParamValue(String url, String paramName, String def
}
}
public static class date {
public static LocalDateTime bitsToLocalDateTime(List<Integer> bits) {
return LocalDateTime.of(bits.get(0), bits.get(1), bits.get(2), bits.get(3), bits.get(4), bits.get(5), bits.get(6));
}
public static int getEndDayOf(int year, int month) {
String yearMonth = year + "-" + "%02d".formatted(month);

Expand Down

0 comments on commit 34e4d66

Please sign in to comment.