-
Notifications
You must be signed in to change notification settings - Fork 0
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
[43] AOP 를 사용한 권한 체크 #45
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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
11 changes: 11 additions & 0 deletions
11
src/main/java/org/example/commerce_site/common/auth/PartnerCheck.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.commerce_site.common.auth; | ||
|
||
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 PartnerCheck { | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/org/example/commerce_site/common/auth/RoleCheckAspect.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,66 @@ | ||
package org.example.commerce_site.common.auth; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.example.commerce_site.attribute.UserRoles; | ||
import org.example.commerce_site.common.exception.CustomException; | ||
import org.example.commerce_site.common.exception.ErrorCode; | ||
import org.example.commerce_site.common.util.JwtUtil; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.stereotype.Component; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Aspect | ||
@RequiredArgsConstructor | ||
@Component | ||
public class RoleCheckAspect { | ||
|
||
private static final String AUTHORIZATION = "Authorization"; | ||
private static final String BEARER_PREFIX = "Bearer "; | ||
private final HttpServletRequest httpServletRequest; | ||
private final JwtUtil jwtUtil; | ||
|
||
@Around("@annotation(org.example.commerce_site.common.auth.PartnerCheck)") | ||
public Object checkPartner(ProceedingJoinPoint joinPoint) throws Throwable { | ||
return checkRole(joinPoint, UserRoles.ROLE_PARTNER); | ||
} | ||
|
||
@Around("@annotation(org.example.commerce_site.common.auth.UserCheck)") | ||
public Object checkUser(ProceedingJoinPoint joinPoint) throws Throwable { | ||
return checkRole(joinPoint, UserRoles.ROLE_USER); | ||
} | ||
|
||
private Object checkRole(ProceedingJoinPoint joinPoint, UserRoles requiredRole) throws Throwable { | ||
String token = extractTokenFromRequest(); | ||
Jwt jwt = jwtUtil.decodeToken(token); | ||
List<String> roleList = extractAuthorities(jwt); | ||
|
||
if (!roleList.contains(requiredRole.name())) { | ||
throw new CustomException(ErrorCode.ACCESS_DENIED); | ||
} | ||
|
||
return joinPoint.proceed(); | ||
} | ||
|
||
private String extractTokenFromRequest() { | ||
String authorizationHeader = httpServletRequest.getHeader(AUTHORIZATION); | ||
if (authorizationHeader == null || !authorizationHeader.startsWith(BEARER_PREFIX)) { | ||
throw new CustomException(ErrorCode.ACCESS_DENIED); | ||
} | ||
return authorizationHeader.substring(BEARER_PREFIX.length()); | ||
} | ||
|
||
private List<String> extractAuthorities(Jwt jwt) { | ||
var resourceAccess = (Map<String, Object>)jwt.getClaim("resource_access"); | ||
var roles = (Map<String, Object>)resourceAccess.get("oauth2-client-app"); | ||
return (List<String>)roles.get("roles"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/example/commerce_site/common/auth/UserCheck.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.commerce_site.common.auth; | ||
|
||
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 UserCheck { | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/org/example/commerce_site/common/util/JwtUtil.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,27 @@ | ||
package org.example.commerce_site.common.util; | ||
|
||
import org.example.commerce_site.common.exception.CustomException; | ||
import org.example.commerce_site.common.exception.ErrorCode; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.jwt.JwtException; | ||
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtUtil { | ||
private final NimbusJwtDecoder jwtDecoder; | ||
|
||
public Jwt decodeToken(String token) { | ||
try { | ||
return jwtDecoder.decode(token); | ||
} catch (JwtException e) { | ||
log.error(e.getMessage()); | ||
throw new CustomException(ErrorCode.JWT_DECODING_ERROR); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/example/commerce_site/config/JwtDecoderConfig.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,14 @@ | ||
package org.example.commerce_site.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; | ||
|
||
@Configuration | ||
public class JwtDecoderConfig { | ||
@Bean | ||
public NimbusJwtDecoder jwtDecoder() { | ||
String jwkSetUri = "http://localhost:9090/realms/oauth2/protocol/openid-connect/certs"; | ||
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build(); | ||
} | ||
} |
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
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.
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.
이 request는 어디서 받아오나요?
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.
클라이언트가 서버에 요청을 보내면 tomcat 이 이를 수신하고 디스패쳐 서블릿으로 보냅니다.
디스패쳐 서블릿은 모든 http 요청을 가로채서 요청이 처리 되는 동안에 유효한 HttpServletRequest 객체를 만들고 스프링에서 이를 주입해줍니다.