-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
} |
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"); | ||
} | ||
} |
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 { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.example.commerce_site.common.util; | ||
|
||
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 JwtException(e.getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 에러가 발생하면 사용자한테는 어떤 에러 코드가 전달되나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 헉 커스텀 예외처리해 500이 뜨도록 수정했습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 500은 서버가 에러난 상황인데, 지금은 어떤 에러코드가 좋을지 한번더 고민해보면 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jwt 디코딩은 인증과 관련된 문제이므로 401 이 더 알맞을 것 같네요 |
||
} | ||
} | ||
} |
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(); | ||
} | ||
} |
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 객체를 만들고 스프링에서 이를 주입해줍니다.