-
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.
Merge pull request #181 from urinaner/feature/180
[BE] AOP 적용
- Loading branch information
Showing
32 changed files
with
299 additions
and
54 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
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
2 changes: 1 addition & 1 deletion
2
...ple/backend/global/config/BaseEntity.java → ...ple/backend/common/domain/BaseEntity.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
...end/common/exception/dto/ResponseDto.java → ...ample/backend/common/dto/ResponseDto.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,4 +1,4 @@ | ||
package org.example.backend.common.exception.dto; | ||
package org.example.backend.common.dto; | ||
|
||
import lombok.*; | ||
|
||
|
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/org/example/backend/common/exception/auth/AuthException.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,15 @@ | ||
package org.example.backend.common.exception.auth; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.example.backend.common.exception.BaseException; | ||
import org.example.backend.common.exception.BaseExceptionType; | ||
|
||
@RequiredArgsConstructor | ||
public class AuthException extends BaseException { | ||
private final AuthExceptionType exceptionType; | ||
|
||
@Override | ||
public BaseExceptionType exceptionType() { | ||
return exceptionType; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
backend/src/main/java/org/example/backend/common/exception/auth/AuthExceptionType.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,23 @@ | ||
package org.example.backend.common.exception.auth; | ||
|
||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.example.backend.common.exception.BaseExceptionType; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@RequiredArgsConstructor | ||
public enum AuthExceptionType implements BaseExceptionType { | ||
PLEASE_LOGIN(BAD_REQUEST ,"로그인 해주세요"); | ||
private final HttpStatus httpStatus; | ||
private final String errorMessage; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return httpStatus; | ||
} | ||
@Override | ||
public String errorMessage() { | ||
return errorMessage; | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/org/example/backend/global/aop/AuthAdmin.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 org.example.backend.global.aop; | ||
|
||
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 AuthAdmin { | ||
} |
50 changes: 50 additions & 0 deletions
50
backend/src/main/java/org/example/backend/global/aop/AuthAdminResolver.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,50 @@ | ||
package org.example.backend.global.aop; | ||
|
||
import static org.example.backend.common.exception.auth.AuthExceptionType.PLEASE_LOGIN; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.example.backend.admin.service.AdminService; | ||
import org.example.backend.common.exception.auth.AuthException; | ||
import org.example.backend.jwt.JWTUtil; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
|
||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class AuthAdminResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final JWTUtil jwtUtil; | ||
private final HttpServletRequest request; | ||
private final AdminService adminService; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(AuthUser.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument( | ||
MethodParameter parameter, | ||
ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, | ||
org.springframework.web.bind.support.WebDataBinderFactory binderFactory) throws Exception { | ||
|
||
String authorizationHeader = request.getHeader("Authorization"); | ||
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { | ||
throw new AuthException(PLEASE_LOGIN); | ||
} | ||
|
||
String token = authorizationHeader.substring(7); | ||
if (!jwtUtil.validateToken(token)) { | ||
throw new AuthException(PLEASE_LOGIN); | ||
} | ||
|
||
String loginId = jwtUtil.extractClaims(token).get("loginId", String.class); | ||
return adminService.getAdminById(Long.valueOf(loginId)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/org/example/backend/global/aop/AuthUser.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 org.example.backend.global.aop; | ||
|
||
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 AuthUser { | ||
} |
50 changes: 50 additions & 0 deletions
50
backend/src/main/java/org/example/backend/global/aop/AuthUserResolver.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,50 @@ | ||
package org.example.backend.global.aop; | ||
|
||
import static org.example.backend.common.exception.auth.AuthExceptionType.PLEASE_LOGIN; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.example.backend.common.exception.auth.AuthException; | ||
import org.example.backend.jwt.JWTUtil; | ||
import org.example.backend.user.service.UserService; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
|
||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class AuthUserResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final JWTUtil jwtUtil; | ||
private final HttpServletRequest request; | ||
private final UserService userService; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(AuthUser.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument( | ||
MethodParameter parameter, | ||
ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, | ||
org.springframework.web.bind.support.WebDataBinderFactory binderFactory) throws Exception { | ||
|
||
String authorizationHeader = request.getHeader("Authorization"); | ||
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { | ||
throw new AuthException(PLEASE_LOGIN); | ||
} | ||
|
||
String token = authorizationHeader.substring(7); | ||
if (!jwtUtil.validateToken(token)) { | ||
throw new AuthException(PLEASE_LOGIN); | ||
} | ||
|
||
String loginId = jwtUtil.extractClaims(token).get("loginId", String.class); | ||
return userService.getUserById(Long.valueOf(loginId)); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...g/CustomAuthenticationFailureHandler.java → ...h/CustomAuthenticationFailureHandler.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
...l/config/CustomPageableHandlerConfig.java → ...fig/auth/CustomPageableHandlerConfig.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
3 changes: 2 additions & 1 deletion
3
...backend/global/config/SecurityConfig.java → ...nd/global/config/auth/SecurityConfig.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
...ckend/global/config/AWSConfiguration.java → ...d/global/config/aws/AWSConfiguration.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
...ple/backend/global/config/S3Uploader.java → ...backend/global/config/aws/S3Uploader.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
...kend/global/config/JpaAuditingConfig.java → .../global/config/jpa/JpaAuditingConfig.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
...ample/backend/global/config/SjConfig.java → ...le/backend/global/config/sj/SjConfig.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
.../backend/global/config/SwaggerConfig.java → .../global/config/swagger/SwaggerConfig.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
.../backend/global/config/CorsMvcConfig.java → ...kend/global/config/web/CorsMvcConfig.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
Oops, something went wrong.