-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee70567
commit 2b82ff7
Showing
5 changed files
with
100 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 33 additions & 32 deletions
65
src/main/java/com/cdd/sangchupassport/support/PassportArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
65 changes: 28 additions & 37 deletions
65
src/main/java/com/cdd/sangchupassport/support/PassportValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters