Skip to content

Commit

Permalink
Remove: 목적지 검증 기능 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
KIMSEI1124 committed Mar 27, 2024
1 parent ee70567 commit 2b82ff7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 124 deletions.
61 changes: 23 additions & 38 deletions src/main/java/com/cdd/sangchupassport/Passport.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.cdd.sangchupassport;

import static com.cdd.sangchupassport.support.SangchuHeader.*;

import org.springframework.http.HttpHeaders;

import com.cdd.sangchupassport.exception.PassportErrorCode;
import com.cdd.sangchupassport.exception.PassportException;
import com.cdd.sangchupassport.token.PassportToken;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.http.HttpHeaders;

import java.util.List;

import static com.cdd.sangchupassport.support.SangchuHeader.SANGCHU_PASSPORT;

/**
* 각 변수의 의미는 다음과 같습니다.<p>
Expand All @@ -28,36 +26,23 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Passport {
@JsonIgnore
private final HttpHeaders headers = new HttpHeaders();
@JsonProperty("passport_id")
private String id;
@JsonProperty("member_id")
private int memberId;
@JsonProperty("expired_time")
private long expiredTime;
@JsonProperty("destinations")
private List<String> destinations;

public boolean isEqualToDestination(String destination) {
return destinations.isEmpty() || !destination.equals(destinations.get(0));
}

public void stamp(ObjectMapper mapper, CrudRepository<PassportToken, String> repository) {
destinations.remove(0);
if (destinations.isEmpty()) {
repository.save(PassportToken.from(id));
} else {
makeHttpHeaders(mapper);
}
}
@JsonIgnore
private final HttpHeaders headers = new HttpHeaders();
@JsonProperty("passport_id")
private String id;
@JsonProperty("member_id")
private int memberId;
@JsonProperty("expired_time")
private long expiredTime;

private void makeHttpHeaders(ObjectMapper mapper) {
try {
headers.add(SANGCHU_PASSPORT.getName(),
mapper.writeValueAsString(this));
} catch (JsonProcessingException e) {
throw new PassportException(this, PassportErrorCode.INVALID_PATTEN);
}
}
public void makeHttpHeaders(ObjectMapper mapper) {
try {
headers.add(
SANGCHU_PASSPORT.getName(),
mapper.writeValueAsString(this)
);
} catch (JsonProcessingException e) {
throw new PassportException(this, PassportErrorCode.INVALID_PATTEN);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

@Getter
public enum PassportErrorCode {
EXPIRED_PASSPORT(400, "PASSPORT_001", "만료된 인증입니다."),
REUSE_PASSPORT(400, "PASSPORT_002", "이미 사용된 인증입니다."),
INVALID_DESTINATION(400, "PASSPORT_003", "잘못된 접근입니다."),
INVALID_PATTEN(400, "PASSPORT_004", "올바르지 않은 패턴입니다.");
EXPIRED_PASSPORT(400, "PASSPORT_001", "만료된 인증입니다."),
REUSE_PASSPORT(400, "PASSPORT_002", "이미 사용된 인증입니다."),
INVALID_PATTEN(400, "PASSPORT_003", "올바르지 않은 패턴입니다.");

private final int statusCode;
private final String errorCode;
private final String message;
private final int statusCode;
private final String errorCode;
private final String message;

PassportErrorCode(int statusCode, String errorCode, String message) {
this.statusCode = statusCode;
this.errorCode = errorCode;
this.message = message;
}
PassportErrorCode(int statusCode, String errorCode, String message) {
this.statusCode = statusCode;
this.errorCode = errorCode;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
package com.cdd.sangchupassport.support;

import com.cdd.sangchupassport.Passport;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import static com.cdd.sangchupassport.support.SangchuHeader.*;

import java.util.Objects;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.util.Objects;

import static com.cdd.sangchupassport.support.SangchuHeader.SANGCHU_PASSPORT;
import com.cdd.sangchupassport.Passport;
import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class PassportArgumentResolver implements HandlerMethodArgumentResolver {
private final ObjectMapper mapper;
private final PassportValidator passportValidator;


@Override
public boolean supportsParameter(MethodParameter parameter) {
boolean hasParameterAnnotation = parameter.hasParameterAnnotation(RequestPassport.class);
boolean assignableFrom = Passport.class.isAssignableFrom(parameter.getParameterType());
return hasParameterAnnotation && assignableFrom;
}

@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
Passport passport = mapper.readValue(
Objects.requireNonNull(
webRequest.getNativeRequest(
HttpServletRequest.class)
).getHeader(SANGCHU_PASSPORT.getName()),
Passport.class);
passportValidator.validatePassword(passport);
return passport;
}
private final ObjectMapper mapper;
private final PassportValidator passportValidator;

@Override
public boolean supportsParameter(MethodParameter parameter) {
boolean hasParameterAnnotation = parameter.hasParameterAnnotation(RequestPassport.class);
boolean assignableFrom = Passport.class.isAssignableFrom(parameter.getParameterType());
return hasParameterAnnotation && assignableFrom;
}

@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
Passport passport = mapper.readValue(
Objects.requireNonNull(
webRequest.getNativeRequest(
HttpServletRequest.class)
).getHeader(SANGCHU_PASSPORT.getName()),
Passport.class);
passportValidator.validatePassport(passport);
passport.makeHttpHeaders(mapper);
return passport;
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,43 @@
package com.cdd.sangchupassport.support;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;

import com.cdd.sangchupassport.Passport;
import com.cdd.sangchupassport.exception.PassportErrorCode;
import com.cdd.sangchupassport.exception.PassportException;
import com.cdd.sangchupassport.token.PassportToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;

@Component
public class PassportValidator {
private final CrudRepository<PassportToken, String> repository;
private final String destination;
@Autowired
private ObjectMapper mapper;

public PassportValidator(
CrudRepository<PassportToken, String> repository,
String destination
) {
this.repository = repository;
this.destination = destination;
}
private final CrudRepository<PassportToken, String> repository;
private final ObjectMapper objectMapper;

public void validatePassword(final Passport passport) {
validateExpiredTime(passport);
validateDestination(passport);
validateReuse(passport);
passport.stamp(mapper, repository);
}
public PassportValidator(
CrudRepository<PassportToken, String> repository,
@Autowired ObjectMapper mapper
) {
this.repository = repository;
objectMapper = mapper;
}

private void validateExpiredTime(final Passport passport) {
long now = System.currentTimeMillis();
if (now > passport.getExpiredTime()) {
throw new PassportException(passport, PassportErrorCode.EXPIRED_PASSPORT);
}
}
public void validatePassport(final Passport passport) {
validateExpiredTime(passport);
validateReuse(passport);
}

private void validateDestination(final Passport passport) {
if (passport.isEqualToDestination(destination)) {
throw new PassportException(passport, PassportErrorCode.INVALID_DESTINATION);
}
}
private void validateExpiredTime(final Passport passport) {
long now = System.currentTimeMillis();
if (now > passport.getExpiredTime()) {
throw new PassportException(passport, PassportErrorCode.EXPIRED_PASSPORT);
}
}

private void validateReuse(final Passport passport) {
if (repository.existsById(passport.getId())) {
throw new PassportException(passport, PassportErrorCode.REUSE_PASSPORT);
}
}
private void validateReuse(final Passport passport) {
if (repository.existsById(passport.getId())) {
throw new PassportException(passport, PassportErrorCode.REUSE_PASSPORT);
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/cdd/sangchupassport/support/SangchuHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

@Getter
public enum SangchuHeader {
SANGCHU_PASSPORT("Sangchu-Passport");
SANGCHU_PASSPORT("Sangchu-Passport");

private final String name;
private final String name;

SangchuHeader(String name) {
this.name = name;
}
SangchuHeader(String name) {
this.name = name;
}
}

0 comments on commit 2b82ff7

Please sign in to comment.