-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from eod940/054-FEAT-POST
feat: [054-FEAT-POST] Post API 기능 구현
- Loading branch information
Showing
32 changed files
with
1,300 additions
and
16 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/com/valuewith/tweaver/auth/CustomAuthPrincipal.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 com.valuewith.tweaver.auth; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface CustomAuthPrincipal { | ||
boolean errorOnInvalidType() default false; | ||
|
||
String expression() default ""; | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/valuewith/tweaver/auth/CustomAuthenticationPrincipalArgumentResolver.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,80 @@ | ||
package com.valuewith.tweaver.auth; | ||
|
||
import com.valuewith.tweaver.constants.ErrorCode; | ||
import com.valuewith.tweaver.exception.CustomAuthException; | ||
import java.lang.annotation.Annotation; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.expression.BeanResolver; | ||
import org.springframework.expression.Expression; | ||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.StringUtils; | ||
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; | ||
|
||
@Component | ||
public class CustomAuthenticationPrincipalArgumentResolver implements | ||
HandlerMethodArgumentResolver { | ||
|
||
private ExpressionParser parser = new SpelExpressionParser(); | ||
private BeanResolver beanResolver; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return findMethodAnnotation(CustomAuthPrincipal.class, parameter) != null; | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
|
||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null) { | ||
throw new CustomAuthException(ErrorCode.NO_PRINCIPAL); // [100] 401 unauthorized | ||
} | ||
Object principal = authentication.getPrincipal(); | ||
CustomAuthPrincipal annotation = findMethodAnnotation(CustomAuthPrincipal.class, parameter); | ||
String expressionToParse = annotation.expression(); | ||
if (StringUtils.hasLength(expressionToParse)) { | ||
StandardEvaluationContext context = new StandardEvaluationContext(); | ||
context.setRootObject(principal); | ||
context.setVariable("this", principal); | ||
context.setBeanResolver(this.beanResolver); | ||
Expression expression = this.parser.parseExpression(expressionToParse); | ||
principal = expression.getValue(context); | ||
} | ||
if (principal != null && !ClassUtils.isAssignable(parameter.getParameterType(), | ||
principal.getClass())) { | ||
if (annotation.errorOnInvalidType()) { | ||
throw new ClassCastException( | ||
principal + " is not assignable to " + parameter.getParameterType()); | ||
} | ||
throw new CustomAuthException(ErrorCode.NO_PRINCIPAL); // [100] 401 unauthorized | ||
} | ||
return principal; | ||
} | ||
|
||
private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, | ||
MethodParameter parameter) { | ||
T annotation = parameter.getParameterAnnotation(annotationClass); | ||
if (annotation != null) { | ||
return annotation; | ||
} | ||
Annotation[] annotationsToSearch = parameter.getParameterAnnotations(); | ||
for (Annotation toSearch : annotationsToSearch) { | ||
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass); | ||
if (annotation != null) { | ||
return annotation; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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
10 changes: 6 additions & 4 deletions
10
src/main/java/com/valuewith/tweaver/constants/ImageType.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,19 +1,21 @@ | ||
package com.valuewith.tweaver.constants; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum ImageType { | ||
|
||
PROFILE("profile/"), | ||
THUMBNAIL("thumbnail/"), | ||
LOCATION("location/"), | ||
MEMBER("member/"); | ||
MEMBER("member/"), | ||
POST("post/"); | ||
|
||
private final String path; | ||
|
||
|
||
ImageType(String path) { | ||
this.path = path; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/valuewith/tweaver/exception/CustomAuthException.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,20 @@ | ||
package com.valuewith.tweaver.exception; | ||
|
||
import com.valuewith.tweaver.constants.ErrorCode; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class CustomAuthException extends RuntimeException { | ||
|
||
private final ErrorCode errorCode; | ||
private final String message; | ||
private final HttpStatus httpStatus; | ||
|
||
public CustomAuthException(ErrorCode errorCode) { | ||
this.errorCode = errorCode; | ||
this.message = errorCode.getDescription(); | ||
this.httpStatus = errorCode.getHttpStatus(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.