-
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.
Browse files
Browse the repository at this point in the history
…terceptor userSpace 검증 인터셉터 개발
- Loading branch information
Showing
19 changed files
with
254 additions
and
11 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ument_resolver/jwtLogin/JwtLoginAuth.java → ...gumentResolver/jwtLogin/JwtLoginAuth.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
2 changes: 1 addition & 1 deletion
2
.../JwtLoginAuthHandlerArgumentResolver.java → .../JwtLoginAuthHandlerArgumentResolver.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
12 changes: 12 additions & 0 deletions
12
src/main/java/space/space_spring/argumentResolver/userSpace/CheckUserSpace.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package space.space_spring.argumentResolver.userSpace; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CheckUserSpace { | ||
boolean required() default true; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/space/space_spring/argumentResolver/userSpace/UserSpaceAuth.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package space.space_spring.argumentResolver.userSpace; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UserSpaceAuth { | ||
} |
32 changes: 32 additions & 0 deletions
32
...a/space/space_spring/argumentResolver/userSpace/UserSpaceAuthHandlerArgumentResolver.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package space.space_spring.argumentResolver.userSpace; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
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 space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; | ||
import space.space_spring.dao.UserSpaceDao; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class UserSpaceAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final UserSpaceDao userSpaceDao; | ||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
// 일단 parameter의 return value type 을 검사하지는 X | ||
return parameter.hasParameterAnnotation(UserSpaceAuth.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { | ||
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
return | ||
userSpaceDao.findUserSpaceAuthById((Long)request.getAttribute("userSpaceId")); | ||
|
||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/space/space_spring/argumentResolver/userSpace/UserSpaceId.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package space.space_spring.argumentResolver.userSpace; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UserSpaceId { | ||
} |
26 changes: 26 additions & 0 deletions
26
...ava/space/space_spring/argumentResolver/userSpace/UserSpaceIdHandlerArgumentResolver.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package space.space_spring.argumentResolver.userSpace; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
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 space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; | ||
|
||
@Component | ||
public class UserSpaceIdHandlerArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
// 일단 parameter의 return value type 을 검사하지는 X | ||
return parameter.hasParameterAnnotation(UserSpaceId.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { | ||
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
return request.getAttribute("userSpaceId"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/space/space_spring/config/UserSpaceValidationInterceptorURL.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package space.space_spring.config; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum UserSpaceValidationInterceptorURL { | ||
//SPACE("/space/**"), | ||
TEST("/space/{spaceId}/test/**"), | ||
; | ||
|
||
private final String urlPattern; | ||
|
||
UserSpaceValidationInterceptorURL(String urlPattern) { | ||
this.urlPattern = urlPattern; | ||
} | ||
} |
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
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
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
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
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
71 changes: 71 additions & 0 deletions
71
src/main/java/space/space_spring/interceptor/UserSpaceValidationInterceptor.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package space.space_spring.interceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.HandlerMapping; | ||
import space.space_spring.argumentResolver.userSpace.CheckUserSpace; | ||
import space.space_spring.dao.SpaceDao; | ||
import space.space_spring.dao.UserDao; | ||
import space.space_spring.dao.UserSpaceDao; | ||
import space.space_spring.entity.Space; | ||
import space.space_spring.entity.User; | ||
import space.space_spring.entity.UserSpace; | ||
import space.space_spring.exception.CustomException; | ||
import space.space_spring.exception.UserSpaceException; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static space.space_spring.response.status.BaseExceptionResponseStatus.*; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class UserSpaceValidationInterceptor implements HandlerInterceptor { | ||
|
||
private final UserSpaceDao userSpaceDao; | ||
private final UserDao userDao; | ||
private final SpaceDao spaceDao; | ||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{ | ||
|
||
// @CheckUserSpace(require=false)인 경우 검증하지 않음 | ||
HandlerMethod handlerMethod = (HandlerMethod) handler; | ||
CheckUserSpace methodAnnotation= handlerMethod.getMethodAnnotation(CheckUserSpace.class); | ||
if(methodAnnotation!=null && !methodAnnotation.required()){ | ||
System.out.print("[DeBug]Interceptor pass By Annotation"); | ||
return true; | ||
} | ||
Long userId = (Long) request.getAttribute("userId"); | ||
|
||
// URL에서 spaceId 추출 | ||
Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); | ||
Long spaceId = Long.parseLong((String) pathVariables.get("spaceId")); | ||
|
||
request.setAttribute("userSpaceId",getUserSpace(spaceId,userId)); | ||
System.out.print("userSpaceID:"+getUserSpace(spaceId,userId)); | ||
return true; | ||
|
||
} | ||
|
||
private Long getUserSpace(long spaceId,long userId){ | ||
// userSpaceDao에서 검증 | ||
User userByUserId = userDao.findUserByUserId(userId); | ||
if(userByUserId==null){ | ||
throw new CustomException(CANNOT_FIND_USER_ID); | ||
} | ||
Space spaceBySpaceId = spaceDao.findSpaceBySpaceId(spaceId); | ||
if(spaceBySpaceId==null){ | ||
throw new CustomException(SPACE_NOT_FOUND); | ||
} | ||
Optional<UserSpace> userSpace = userSpaceDao.findUserSpaceByUserAndSpace(userByUserId, spaceBySpaceId); | ||
Optional.ofNullable(userSpace | ||
.orElseThrow(() -> new UserSpaceException(USER_IS_NOT_IN_SPACE))); | ||
return userSpace.get().getUserSpaceId(); | ||
} | ||
|
||
|
||
} |
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