-
Notifications
You must be signed in to change notification settings - Fork 8
fix: RequiredArgsConstructor 동작하도록 임시 수정 #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Gyuhyeok99
merged 2 commits into
solid-connection:develop
from
Gyuhyeok99:fix/409-role-auth-long-param
Jul 29, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 41 additions & 11 deletions
52
src/main/java/com/example/solidconnection/security/aspect/RoleAuthorizationAspect.java
This file contains hidden or 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,41 +1,71 @@ | ||
| package com.example.solidconnection.security.aspect; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.ACCESS_DENIED; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND; | ||
|
|
||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import com.example.solidconnection.common.resolver.AuthorizedUser; | ||
| import com.example.solidconnection.security.annotation.RequireRoleAccess; | ||
| import com.example.solidconnection.siteuser.domain.Role; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import com.example.solidconnection.siteuser.repository.SiteUserRepository; | ||
| import java.lang.reflect.Parameter; | ||
| import java.util.Arrays; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.aspectj.lang.ProceedingJoinPoint; | ||
| import org.aspectj.lang.annotation.Around; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.aspectj.lang.reflect.MethodSignature; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Aspect | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class RoleAuthorizationAspect { | ||
|
|
||
| // todo: 추후 siteUserId로 파라미터 변경 시 수정 필요 | ||
| private final SiteUserRepository siteUserRepository; | ||
|
|
||
| // todo: 추후 개선 필요 | ||
| @Around("@annotation(requireRoleAccess)") | ||
| public Object checkRoleAccess(ProceedingJoinPoint joinPoint, RequireRoleAccess requireRoleAccess) throws Throwable { | ||
| SiteUser siteUser = null; | ||
| for (Object arg : joinPoint.getArgs()) { | ||
| if (arg instanceof SiteUser) { | ||
| siteUser = (SiteUser) arg; | ||
| break; | ||
| } | ||
| } | ||
| if (siteUser == null) { | ||
|
|
||
| Long siteUserId = extractAuthorizedUserId(joinPoint); | ||
|
|
||
| if (siteUserId == null) { | ||
| throw new CustomException(ACCESS_DENIED); | ||
| } | ||
| Role[] allowedRoles = requireRoleAccess.roles(); | ||
|
|
||
| SiteUser siteUser = siteUserRepository.findById(siteUserId) | ||
| .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
|
|
||
| validateUserRole(siteUser, requireRoleAccess.roles()); | ||
|
|
||
| return joinPoint.proceed(); | ||
| } | ||
|
|
||
| private Long extractAuthorizedUserId(ProceedingJoinPoint joinPoint) { | ||
| MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
| Parameter[] parameters = signature.getMethod().getParameters(); | ||
| Object[] args = joinPoint.getArgs(); | ||
|
|
||
| for (int i = 0; i < parameters.length; i++) { | ||
| if (parameters[i].isAnnotationPresent(AuthorizedUser.class)) { | ||
| Object arg = args[i]; | ||
| if (arg instanceof Long) { | ||
| return (Long) arg; | ||
| } else if (parameters[i].getType() == long.class) { | ||
| return (Long) arg; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private void validateUserRole(SiteUser siteUser, Role[] allowedRoles) { | ||
| boolean hasAccess = Arrays.asList(allowedRoles).contains(siteUser.getRole()); | ||
|
|
||
| if (!hasAccess) { | ||
| throw new CustomException(ACCESS_DENIED); | ||
| } | ||
| return joinPoint.proceed(); | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
아키텍처 및 보안 고려사항
현재 구현의 주요 변경사항과 고려사항:
보안 측면
@AuthorizedUser어노테이션이 붙은 파라미터가 실제로 인증된 사용자의 ID인지 검증 필요성능 측면
개선 제안
@AuthenticationPrincipal과 유사한 방식으로HandlerMethodArgumentResolver활용이러한 점들이 PR 설명에서 언급된 "임시 수정"의 이유인 것으로 보입니다.
🏁 Script executed:
Length of output: 33412
🏁 Script executed:
Length of output: 694
아키텍처 및 보안 고려사항
보안 측면
extractAuthorizedUserId가ProceedingJoinPoint.getArgs()로 인자를 읽어오기 때문에 무결성 검사를 반드시 강화해야 합니다.성능 측면
SiteUserRepository.findById로 DB 조회가 이루어져 응답 지연이 발생할 수 있습니다.아키텍처 측면
HandlerMethodArgumentResolver와SecurityContextHolder기반의 표준 Spring Security 패턴을 적용해 책임 분리를 검토하세요.장기적으로는 위 세 가지를 개선하여 안정성·성능·가독성을 모두 높일 수 있습니다.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
흠~ 이 말은 일리가 있긴 하네요.
그런데 이 PR은 수습이 목표이고, 디스코드에 논의한대로 PreAuthorize 어노테이션을 사용하면
레포지토리가 아니라 security context 에서 사용자 정보를 가져오니 DB 조회가 없을것입니다~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nayonsoso 맞습니다! 임시 수습 목적이라면 현재 접근법이 합리적이네요.
말씀하신 대로
@PreAuthorize를 사용하면 SecurityContext에서 인증된 사용자 정보를 직접 가져와서 DB 조회 없이 권한 검사가 가능합니다. 이는 제가 앞서 제안한 SecurityContextHolder 활용 방안과 정확히 일치하는 방향입니다.현재 구현 →
@PreAuthorize마이그레이션은 아키텍처적으로 올바른 선택입니다:임시 수습으로서는 충분히 동작할 것 같습니다! 👍
🧠 Learnings used