-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* refactor: MemberAuthCommandService oAuth2Login 메서드명 변경 - oAuth2Login -> login * refactor: Authentication을 사용한 인증/인가 리팩터링 * refactor: AuthenticationArgumentResolver 파라미터에 어노테이션 제거 * feat: 토큰 Subject에 식별자 추가 - 하위 호환성을 지키기 위해 기존 방식은 유지
- Loading branch information
1 parent
21629ab
commit 9728b00
Showing
50 changed files
with
982 additions
and
477 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/com/festago/auth/AdminAuthenticationArgumentResolver.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,40 @@ | ||
package com.festago.auth; | ||
|
||
import com.festago.auth.domain.authentication.AdminAuthentication; | ||
import com.festago.auth.domain.authentication.Authentication; | ||
import com.festago.common.exception.UnexpectedException; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.util.Assert; | ||
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; | ||
|
||
public class AdminAuthenticationArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final AuthenticateContext authenticateContext; | ||
|
||
public AdminAuthenticationArgumentResolver(AuthenticateContext authenticateContext) { | ||
Assert.notNull(authenticateContext, "The authenticateContext must not be null"); | ||
this.authenticateContext = authenticateContext; | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterType().equals(AdminAuthentication.class); | ||
} | ||
|
||
@Override | ||
public AdminAuthentication resolveArgument( | ||
MethodParameter parameter, | ||
ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, | ||
WebDataBinderFactory binderFactory | ||
) { | ||
Authentication authentication = authenticateContext.getAuthentication(); | ||
if (authentication instanceof AdminAuthentication adminAuthentication) { | ||
return adminAuthentication; | ||
} | ||
throw new UnexpectedException("인가된 권한이 인자의 권한과 맞지 않습니다."); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/com/festago/auth/AnnotationAuthorizationInterceptor.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,52 @@ | ||
package com.festago.auth; | ||
|
||
import com.festago.auth.annotation.Authorization; | ||
import com.festago.auth.application.HttpRequestTokenExtractor; | ||
import com.festago.auth.domain.AuthenticationTokenExtractor; | ||
import com.festago.auth.domain.authentication.Authentication; | ||
import com.festago.common.exception.ErrorCode; | ||
import com.festago.common.exception.ForbiddenException; | ||
import com.festago.common.exception.UnauthorizedException; | ||
import com.festago.common.exception.UnexpectedException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
public class AnnotationAuthorizationInterceptor implements HandlerInterceptor { | ||
|
||
private final HttpRequestTokenExtractor httpRequestTokenExtractor; | ||
private final AuthenticationTokenExtractor authenticationTokenExtractor; | ||
private final AuthenticateContext authenticateContext; | ||
|
||
public AnnotationAuthorizationInterceptor( | ||
HttpRequestTokenExtractor httpRequestTokenExtractor, | ||
AuthenticationTokenExtractor authenticationTokenExtractor, | ||
AuthenticateContext authenticateContext) | ||
{ | ||
Assert.notNull(httpRequestTokenExtractor, "The httpRequestTokenExtractor must not be null"); | ||
Assert.notNull(authenticationTokenExtractor, "The authenticationTokenExtractor must not be null"); | ||
Assert.notNull(authenticateContext, "The authenticateContext must not be null"); | ||
this.httpRequestTokenExtractor = httpRequestTokenExtractor; | ||
this.authenticationTokenExtractor = authenticationTokenExtractor; | ||
this.authenticateContext = authenticateContext; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
HandlerMethod handlerMethod = (HandlerMethod) handler; | ||
Authorization authorization = handlerMethod.getMethodAnnotation(Authorization.class); | ||
if (authorization == null) { | ||
throw new UnexpectedException("HandlerMethod에 Authorization 어노테이션이 없습니다."); | ||
} | ||
String token = httpRequestTokenExtractor.extract(request) | ||
.orElseThrow(() -> new UnauthorizedException(ErrorCode.NEED_AUTH_TOKEN)); | ||
Authentication authentication = authenticationTokenExtractor.extract(token); | ||
if (authentication.getRole() != authorization.role()) { | ||
throw new ForbiddenException(ErrorCode.NOT_ENOUGH_PERMISSION); | ||
} | ||
authenticateContext.setAuthentication(authentication); | ||
return true; | ||
} | ||
} |
82 changes: 0 additions & 82 deletions
82
backend/src/main/java/com/festago/auth/AuthInterceptor.java
This file was deleted.
Oops, something went wrong.
18 changes: 11 additions & 7 deletions
18
backend/src/main/java/com/festago/auth/AuthenticateContext.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,26 +1,30 @@ | ||
package com.festago.auth; | ||
|
||
import com.festago.auth.domain.Role; | ||
import com.festago.auth.domain.authentication.AnonymousAuthentication; | ||
import com.festago.auth.domain.authentication.Authentication; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.annotation.RequestScope; | ||
|
||
@Component | ||
@RequestScope | ||
public class AuthenticateContext { | ||
|
||
private Long id; | ||
private Role role = Role.ANONYMOUS; | ||
private Authentication authentication = AnonymousAuthentication.getInstance(); | ||
|
||
public void setAuthenticate(Long id, Role role) { | ||
this.id = id; | ||
this.role = role; | ||
public void setAuthentication(Authentication authentication) { | ||
this.authentication = authentication; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
return authentication.getId(); | ||
} | ||
|
||
public Role getRole() { | ||
return role; | ||
return authentication.getRole(); | ||
} | ||
|
||
public Authentication getAuthentication() { | ||
return authentication; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/com/festago/auth/FixedAuthorizationInterceptor.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,49 @@ | ||
package com.festago.auth; | ||
|
||
import com.festago.auth.application.HttpRequestTokenExtractor; | ||
import com.festago.auth.domain.AuthenticationTokenExtractor; | ||
import com.festago.auth.domain.Role; | ||
import com.festago.auth.domain.authentication.Authentication; | ||
import com.festago.common.exception.ErrorCode; | ||
import com.festago.common.exception.ForbiddenException; | ||
import com.festago.common.exception.UnauthorizedException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
public class FixedAuthorizationInterceptor implements HandlerInterceptor { | ||
|
||
private final HttpRequestTokenExtractor httpRequestTokenExtractor; | ||
private final AuthenticationTokenExtractor authenticationTokenExtractor; | ||
private final AuthenticateContext authenticateContext; | ||
private final Role role; | ||
|
||
public FixedAuthorizationInterceptor( | ||
HttpRequestTokenExtractor httpRequestTokenExtractor, | ||
AuthenticationTokenExtractor authenticationTokenExtractor, | ||
AuthenticateContext authenticateContext, | ||
Role role | ||
) { | ||
Assert.notNull(httpRequestTokenExtractor, "The httpRequestTokenExtractor must not be null"); | ||
Assert.notNull(authenticationTokenExtractor, "The authenticationTokenExtractor must not be null"); | ||
Assert.notNull(authenticateContext, "The authenticateContext must not be null"); | ||
Assert.notNull(role, "The role must not be null"); | ||
this.httpRequestTokenExtractor = httpRequestTokenExtractor; | ||
this.authenticationTokenExtractor = authenticationTokenExtractor; | ||
this.authenticateContext = authenticateContext; | ||
this.role = role; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
String token = httpRequestTokenExtractor.extract(request) | ||
.orElseThrow(() -> new UnauthorizedException(ErrorCode.NEED_AUTH_TOKEN)); | ||
Authentication authentication = authenticationTokenExtractor.extract(token); | ||
if (authentication.getRole() != role) { | ||
throw new ForbiddenException(ErrorCode.NOT_ENOUGH_PERMISSION); | ||
} | ||
authenticateContext.setAuthentication(authentication); | ||
return true; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
backend/src/main/java/com/festago/auth/MemberAuthenticationArgumentResolver.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,37 @@ | ||
package com.festago.auth; | ||
|
||
import com.festago.auth.domain.authentication.Authentication; | ||
import com.festago.auth.domain.authentication.MemberAuthentication; | ||
import com.festago.common.exception.UnexpectedException; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.util.Assert; | ||
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; | ||
|
||
public class MemberAuthenticationArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final AuthenticateContext authenticateContext; | ||
|
||
public MemberAuthenticationArgumentResolver(AuthenticateContext authenticateContext) { | ||
Assert.notNull(authenticateContext, "The authenticateContext must not be null"); | ||
this.authenticateContext = authenticateContext; | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterType().equals(MemberAuthentication.class); | ||
} | ||
|
||
@Override | ||
public MemberAuthentication resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory | ||
) { | ||
Authentication authentication = authenticateContext.getAuthentication(); | ||
if (authentication instanceof MemberAuthentication memberAuthentication) { | ||
return memberAuthentication; | ||
} | ||
throw new UnexpectedException("인가된 권한이 인자의 권한과 맞지 않습니다."); | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/festago/auth/annotation/Authorization.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 com.festago.auth.annotation; | ||
|
||
import com.festago.auth.domain.Role; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Authorization { | ||
|
||
Role role(); | ||
} |
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
8 changes: 0 additions & 8 deletions
8
backend/src/main/java/com/festago/auth/application/AuthTokenExtractor.java
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
backend/src/main/java/com/festago/auth/application/AuthTokenProvider.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.