-
Notifications
You must be signed in to change notification settings - Fork 37
๐ 2๋จ๊ณ - ์ธ๊ฐ(Authorization) ๋ฆฌ๋ทฐ ์์ฒญ ๋๋ฆฝ๋๋ค. #10
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
Conversation
- more test for new API
- ํ๋์ ํํฐ์์ ์ฌ๋ฌ ์ธ๊ฐ ๋ก์ง์ ์ฒ๋ฆฌํ๋๋ก ๋ณ๊ฒฝ
- ์ธ์ฆ๋ ์ฌ์ฉ์ ์ฒดํฌ๋ฅผ ์ํ AuthorityAuthorizationManager - ๊ถํ ์ฒดํฌ๋ฅผ ์ํ SecuredAuthorizationManager ์ถ๊ฐ
- MvcRequestMatcher for matching by HttpMethod And URI - AnyRequestMatcher for matching any request
- ์ธ๊ฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋งค๋์ ธ๋ค ์์ฑ - AuthenticatedAuthorizationManager ๋จ์ ์ธ์ฆ ํ์ธ - AuthorityAuthorizationManager ๋ฉ์๋์์ ์ด๋ ธํ ์ด์ ์ ๊ฐ์ ธ์ ๊ถํ ํ์ธ - PermitAllAuthorizationManager ๋ชจ๋ ์์ฒญ ํ์ฉ - DenyAllAuthorizationManager ๋ชจ๋ ํ์ฉ ๊ฑฐ๋ถ
- ์ ์ ๊ถํ ์ ์ด๋ฅผ Interceptor์๊ฒ ์์ํ๊ธฐ ์ํ Secured ์ด๋ ธํ ์ด์ ์ถ๊ฐ
โฆizationManager - ์ธํฐ์ ํฐ์ ํํฐ๋ ๋์ด์ ์ง์ ์ธ๊ฐ๋ฅผ ํด์ฃผ์ง ์๊ณ , AuthorizationManager ๋ฅผ ์ฌ์ฉํ๋ค.
โฆnged - RequestMatcherDelegatingAuthorizationManager ์์ ์์ฒญ์ ๋ง๋ ์ธ๊ฐ๋ฅผ ์๋ํ๋ค.
- ํํฐ๋ ํ๊ณณ์์ ์ฌ๋ฌ ์ธ๊ฐ๋ฅผ ์ฒ๋ฆฌํ์ง๋ง, MethodInterceptor or Aspect ์์๋ ํ๋์ ์ธ๊ฐ๋ฅผ ์ฒ๋ฆฌ ๊ทธ๋์ ๊ตณ์ด ํฉ์ณ์ ์ฒ๋ฆฌํ ํ์๊ฐ ์๋๋ฏํจ. ๋ง์ฝ ํ์ํ๋ค๋ฉด RequestMatcher ํ์์ผ๋ก Annotation ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋๋ก ์ฒ๋ฆฌ? - RequestMatcherDelegatingAuthorizationManager ๋ HttpServletRequest ๋ฅผ ์ฌ์ฉํ๋ ๋งค๋์ ๋ง ๋ฑ๋ก(Filter ์ฌ์ฉํ๋ ์ ๋ค)
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.
์๋ ํ์ธ์ ์ธ์ค๋ ๐
Authorization ๋ฏธ์ ์ ํจ๊ปํ๊ฒ ๋ ์ต์ง์์ ๋๋ค.
๋ฏธ์ ์ ์งํํด์ฃผ์ จ๋ค์ ๐ ๋ช๊ฐ์ง ์ฝ๋ฉํธ ๋จ๊ฒจ๋์์ผ๋ ํ์ธ๋ถํ๋๋ ค์ ๐
๊ถ๊ธํ๊ฑฐ๋ ๊ณ ๋ฏผ์ด ๋๋ ๋ถ๋ถ์ด ์์ผ์๋ค๋ฉด ์ธ์ ๋ pr ์ฝ๋ฉํธ ๋๋ dm์ผ๋ก ์์ฒญ ๋ถํ๋๋ฆฝ๋๋ค.
๊ฐ์ฌํฉ๋๋ค ๐โโ๏ธ
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
|
||
public class SecuredAuthorizationManager implements AuthorizationManager<MethodInvocation> { |
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.
์กฐ๊ธ ํท๊ฐ๋ฆฌ์ค ์ ์์ ๋ฒํ ๋ถ๋ถ์ด๋ผ๊ณ ์๊ฐํด์. ๋๊ฐ์ง๋ฅผ ๋ถ๋ฆฌํด๋๊ณ ์๊ฐํด๋ณด๋ฉด,
SecuredAuthorizationManager
๋ @Secured
์ด๋
ธํ
์ด์
์ ๋ณด๊ณ ์ธ๊ฐ ๊ณผ์ ์ ์ฒดํฌํ๋ ๊ฒ์ด๊ณ , AuthorityAuthorizationManager
๋ ๋ช
ํํ๊ฒ ์ธ๊ฐ ์ฒ๋ฆฌ๋ฅผ ์ง์ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด์์.
๊ทธ๋์ ์ฌ์ค ์ด๋ป๊ฒ๋ณด๋ฉด SecuredAuthorizationManager
์ AuthorityAuthorizationManager
๋ ๋ถ๋ฆฌ๋์ด์๋ค๊ธฐ๋ณด๋ค๋ ์ค์ ๋ก๋ SecuredAuthorizationManager
๊ฐ AuthorityAuthorizationManager
๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , @Secured
์์ ๊บผ๋ธ ๊ฒ์ ๊ฐ์ง๊ณ AuthorityAuthorizationManager
๋ก ๋ค์ ๊ฒ์ฆ์ ์งํํ๊ณ ์๋ค๊ณ ๋ณผ ์ ์์ด์. ๋ช
ํํ๊ฒ ์ฉ๋ ์์ฒด๊ฐ ๋ถ๋ฆฌ๋์ด์๊ณ , ๊ณตํตํ๋ ์ธ๊ฐ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ SecuredAuthorizationManager๏ฟฝ
๊ฐ ์ฌ์ฉํ๋๊ฑฐ์ฃ . (์ค์ ๋ก๋ ๋จ์ผ ๊ถํ๋ง ๊ฒ์ฆํ๊ธฐ ๋ณด๋ค๋ Authorities
๋ก ๋ค์ค ๊ถํ์ ๊ฐ์ง๊ณ ์ฒดํฌํฉ๋๋ค.)
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.
SecuredAuthorizationManager ์์ ์ ํํ ์ธ๊ฐ ์ฒ๋ฆฌ๋ฅผ AuthorityAuthorizationManager ๋ฅผ ์์ํ๋๋ก ํ ์ ์๊ฒ ๋ค์.
AuthorityAuthorizationManager์์๋ ์ ๋ค๋ฆญ์ ํตํด ์ ๋ฌ๋ฐ์ ํ๋ผ๋ฏธํฐ์ ๋ฐ๋ผ์ ๋ค๋ฅธ ์ฒ๋ฆฌ๋ฅผ ํ ์ ๋ ์์ ๊ฒ ๊ฐ์์.
์ ๊ฐ ์ดํดํ ๋ด์ฉ์ ์ ๋ฆฌํ๋ฉด SecuredAuthorizationManager ๋ ์ธ๊ฐ์ ํ์ํ ๊ฐ์ ์ด๋
ธํ
์ด์
๊ธฐ๋ฐ์ผ๋ก ๋์ ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
ํ์ง๋ง SecuredAuthorizationManager ๋ ์ ํํ ์ด๋ค ์ธ๊ฐ ์์
์ ํ ์ง๋ ๊ด์ฌ์ด ์์ต๋๋ค. ๊ทธ๋์ AuthorityAuthorizationManager ์๊ฒ ์ธ๊ฐ ์์
์ ์์ํฉ๋๋ค.
์ ๋ค๋ฆญ์ ์ฌ์ฉํ๋ AuthorityAuthorizationManager ์์๋ ๊ตฌํ์ฒด๋ ์ ๋ค๋ฆญ ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ์ ๋ฌ ๋ฐ์ ๊ฐ๊ณผ Authentication ์ ํตํด์ ์ธ๊ฐ๋ฅผ ์์
ํด์ฃผ๋ ๊ฒ ๊ฐ์ต๋๋ค.
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.
๋ค ์ดํดํด์ฃผ์ ๋ด์ฉ์ด ๋ง์ต๋๋ค :)
README.md
Outdated
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.
ํ์ ์๊ตฌ์ฌํญ ์ ์ ๋ฆฌํด์ฃผ์ จ๋ค์ ๐
} | ||
// @Bean | ||
// public SecuredAspect securedAspect() { | ||
// return new SecuredAspect(); | ||
// } | ||
|
||
@Bean | ||
public RequestMatcherDelegatingAuthorizationManager requestMatcherDelegatingAuthorizationManager() { |
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.
์ ์ ํ๊ฒ ์ ์ถ๊ฐํด์ฃผ์ จ๋ค์ :)
public static AuthorizationDecision granted() { | ||
return new AuthorizationDecision(true); | ||
} | ||
|
||
public static AuthorizationDecision denied() { | ||
return new AuthorizationDecision(false); | ||
} |
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.
๋ฉ์๋๋ก ์ ์ํ ์๋ ์์ง๋ง ๊ฒฐ๊ตญ ๋์ผํ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฌ๋ฒ ์์ฑํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ถ๋ณํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์๋ ํ์ฌ ์ํฉ์ด๋ผ๋ฉด ๋จ์ํ ์์๋ก ๋ง๋ค๊ณ ์ฌ์ฌ์ฉํ๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ด ๋๊ฒ ๋ค์ :)
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.
AuthorizationDecision ์์ ์ฑ๊ณต ์คํจ๋ง ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ์ข์ ๋ฐฉ๋ฒ์ด๋ค์.
return; | ||
AuthorizationDecision authorizationDecision = authorizationManager.checkInFilter(request, authentication); | ||
if (authorizationDecision.isDenied()) { | ||
throw new AuthenticationException(); |
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.
์ธ๊ฐ๊ถํ์ด ๊ฑฐ์ ๋์๋๋ฐ AuthenticationException
์ธ ์ธ์ฆ ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ด ๋ง์๊น์?
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.
Forbidden ์ผ๋ก ์ฒ๋ฆฌํ๋๋ก ๋ณ๊ฒฝํด์ผ๊ฒ ๋ค์.
์ด ๋ถ๋ถ์์๋ ์ด์ชฝ์์๋ ์ check๋ณด๋ค verify๋ฅผ ๊ถ์ฅํ๋ ์ง ๋ณด์ฌ์ฃผ๋ ๊ฒ ๊ฐ๋ค์. AuthorizationDecision ์ธ๊ฐ ๊ฒฐ๊ณผ์ ๋ํ ์ฒ๋ฆฌ์ ์ผ๊ด์ฑ์ ๋ณด์ฅํด์ฃผ๋ ๊ฒ์ด ํ์ํด ๋ณด์ด๋ค์!
boolean matchURI = Arrays.stream(requestURIs) | ||
.anyMatch(uri -> uri.equalsIgnoreCase(request.getRequestURI())); |
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.
์ค์ spring security์์๋ ์ฑ๋ฅ์ ๋ฌธ์ ๋ก ์ธํด stream ์ฌ์ฉ์ ์ ํํ๊ณ ์์ต๋๋ค.
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.
์ฑ๋ฅ ๋ฌธ์ ์ ๊ฐ๋
์ฑ์ ์ธ์ ๋ ๊ณ ๋ฏผ์ด ๋๋ ๋ถ๋ถ์
๋๋ค.
์์ฆ์ ๊ฐ๋ฐํ๋ฉด Stream ์ ๊ฐ๋
์ฑ์ด ์กฐ๊ธ ๋ ์ข๋ค๊ณ ์๊ฐํด์ ์ ํํ๋ ๊ฒ ๊ฐ๋ค์.
๋ง์ํด์ฃผ์ ๋๋ก ForEach ๋ก ์์ ํด๋ณด๊ฒ ์ต๋๋ค!
if (method.isAnnotationPresent(Secured.class)) { | ||
Secured secured = method.getAnnotation(Secured.class); | ||
boolean hasNoRole = authentication.getAuthorities() | ||
.stream() | ||
.noneMatch(auth -> Arrays.asList(secured.value()).contains(auth)); | ||
if (hasNoRole) { | ||
throw new ForbiddenException(); | ||
} | ||
} |
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.
์ง๋ฌธ์ฃผ์ ๋ด์ฉ์ ๋ต๋ณ ๋ฌ์๋์๋ ๊ฒ์ฒ๋ผ AuthoritiesAuthorizationManager
๋ก ๊ตฌํํ๋ ๊ฒ๊น์ง๋ ์๋์ด๋ ์ธ๊ฐ๋ถ๋ถ์ ๋ํด์ AuthorityAuthorizationManager
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ๋ณ๊ฒฝํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์.
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.
์ธ๊ฐ ๋ถ๋ถ์ ์์ํ๋๋ก ๋ณ๊ฒฝํด๋ณด์์ต๋๋ค!
|
||
import java.util.Set; | ||
|
||
public class AuthorityAuthorizationManager implements AuthorizationManager<HttpServletRequest> { |
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.
public class AuthorityAuthorizationManager implements AuthorizationManager<HttpServletRequest> { | |
public class AuthorityAuthorizationManager implements AuthorizationManager<T> { |
์์์ ๋์ดํ ์ด์ ์์ ์ฌ์ค HttpServletRequest
๋ณด๋ค๋ ์ ๋ค๋ฆญ ํ์
์ ํ์ฉํ์ฌ ๋ค๋ฅธ ๊ณณ์์ ํ์ฉํ ์ ์๊ฒ๋ ํ๋ ๊ฒ์ด ์ข๊ฒ ์ฃ .
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.
ํํฐ์ ์ธํฐ์ ํฐ ํน์ ์ธํฐ์ ํฐ์์๋ ์ฌ๋ฌ ์ข ๋ฅ ๊ฐ์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ข์ ์ ํ ๊ฐ์ต๋๋ค,
๊ทธ๋ ๋ค๋ฉด AuthorityAuthorizationManager ๋ ์ฌ๋ฌ ์ข ๋ฅ์ ๊ตฌํ์ฒด๋ฅผ ๋ง๋ค์ด์ ๊ด๋ฆฌํ ์ ๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
์๋ฅผ ๋ค์ด RequestMatcherDelegatingAuthorizationManager ์ฒ๋ผ ์ฌ๋ฌ ๊ตฌํ์ฒด์ support ๋ฉ์๋๋ Matcher๋ฅผ ๋ง๋ญ๋๋ค.
์ด๋ฅผ ํตํด ์ฒ๋ฆฌํ ์ ์๋ ์ธ๊ฐ ์์
์ธ์ง ํ์ธํ๊ณ ๊ทธ์ ๋ง๋ AuthorityAuthorizationManager ๊ตฌํ์ฒด๊ฐ ์๋ํ๋ ๋ฐฉ์์ผ๋ก ์ฝ๊ฒ ๊ด๋ฆฌ ๋ ๊ฒ ๊ฐ์์!
import nextstep.security.authentication.Authentication; | ||
import nextstep.security.authorization.AuthorizationDecision; | ||
|
||
@FunctionalInterface |
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.
์ ํ์ฌํญ์ ๋์์๋ verify๋ ๊ตฌํํ์ง ์์ผ์ ๋ ๋๊ธด ํ์ง๋ง, check์ verify๋ ๊ฐ๊ฐ ์ด๋ค ์ํฉ์์ ์ฌ์ฉํ ํด์ผ ์ข์์ง ์ธ์ค๋์ ์๊ฒฌ์ ์ด๋ ์ ๊ฐ์?
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.
์ ๊ฐ ์ด ๋ฏธ์ ์ ์งํํ๋ฉด์ ๋๋ ์ ์ ์ธ ๊ฐ์ง์ ๋๋ค.
- AuthorizationDecision ์ ๋์ ์ํ์ฑ
- AuthorizationDecision ๊ฐ ์คํจ๋ฌ์ ๋์ ์์ธ ์ฒ๋ฆฌ ์ผ๊ด์ฑ
- ์ฌ์ฉ๋ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด
์ด๋ฐ ์ํฉ๋๋ฌธ์ verify๋ฅผ ๊ถ์ฅํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
ํ์ง๋ง AuthorizationDecision ์ ๊ฒฐ๊ณผ๋ฅผ ์ปค์คํ ์์ธ๋ ์กฐ๊ฑด๋ถ ๋ก์ง์ด ํ์ํ ๊ฒฝ์ฐ๋ง check์ ์ฌ์ฉํด ๊ฒฐ๊ณผ๋ฅผ ํธ๋ค๋ง ํ ๊ฒ ๊ฐ์ต๋๋ค.
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.
์ฌ์ค spring security์์๋ verify
๋ฅผ ๊ถ์ฅํ๋ ๊ฒ์ ์๋์์. (์คํด์ ์์ง๊ฐ ์์ ์ ์์ ๊ฒ ๊ฐ์ ๋ฏธ๋ฆฌ ๋ง์๋๋ฆฌ๋ฉด ์ ๋ต์ ์๊ตฌํ๊ธฐ ์ํด ๋จ๊ฒจ๋๋ฆฐ ์ง๋ฌธ์ ์๋๋๋ค.)
๋ง์ํด์ฃผ์ ์ด์ ์ค ๋จผ์ 1์ ๋ํด์ ์ด์ผ๊ธฐํ๋ฉด null ์ํ์ฑ์ ๋ฐ์์ํค๋ ๊ฒ์ ๋ง์ง๋ง AuthorizationDecision
์ ๋ฐํ์ ์ธ๊ฐ๋จ(true), ์ธ๊ฐ์๋จ(false)๋ง ์๋ ๊ฒ์ด ์๋๋ผ ์ธ๊ฐ ์ํฉ์ด ์์์ ๋ํ๋ด๊ธฐ๋ ํฉ๋๋ค. ๊ฐ๋ น @Secured
๊ฐ ๊ฑธ๋ฆฐ ๊ณณ์ด ์์ด ๊ฒ์ฆํ authorities
๊ฐ ์๋ ๊ฒฝ์ฐ๊ฐ ์๊ฒ ๋ค์.
public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation mi) {
Set<String> authorities = getAuthorities(mi);
return authorities.isEmpty() ? null : this.authoritiesAuthorizationManager.check(authentication, authorities);
}
private Set<String> getAuthorities(MethodInvocation methodInvocation) {
Method method = methodInvocation.getMethod();
Object target = methodInvocation.getThis();
Class<?> targetClass = (target != null) ? target.getClass() : null;
MethodClassKey cacheKey = new MethodClassKey(method, targetClass);
return this.cachedAuthorities.computeIfAbsent(cacheKey, (k) -> resolveAuthorities(method, targetClass));
}
private Set<String> resolveAuthorities(Method method, Class<?> targetClass) {
Secured secured = findSecuredAnnotation(method, targetClass);
return (secured != null) ? Set.of(secured.value()) : Collections.emptySet();
}
๋ค์์ผ๋ก ์ค์ํ ๊ฒ์ ์ธ๊ฐ๋ฅผ ์ฒดํฌํ์๋ ์ธ๊ฐ๋์ง ์์ ์ ์ ์ธ ๊ฒ์ด AuthorizationManager
์
์ฅ์์๋ ์์ธ์ํฉ์ ์๋ ์ ์๋ค์์. ์์ธ๋ฅผ ๋ฐ์์ํค๋ ๊ฒ์ ์ด ์ํฉ์ด ์คํจํ์ผ๋ ์คํจ๋ฅผ ์ ํํ๋ ๊ฒ์ด ๋ชฉ์ ์ผํ
๋ฐ์. spring security ์
์ฅ์์๋ AuthorizationManager
๋ ์ธ๊ฐ๊ฐ ๋์๋์ง ํ์ธํด์ฃผ๋ ๊ฐ์ฒด์ด๊ณ , ์ค์ ์๋ฌ๋ AuthorizationFilter
๋ฑ๊ณผ ๊ฐ์ ํํฐ์์ ์ธ๊ฐ๊ฐ ์๋์๋ ์ํฉ์ ํ๋จํ์ฌ ์์ธ๋ฅผ ๋ฐ์์์ผ์ฃผ๊ณ ์์ด์.
๊ทธ๋์ ๋ด๋ถ์ ์ผ๋ก ์กฐ๊ธ ๋ ๋ณต์กํ์ง๋ง AuthorizationFilter
์์๋ ์ธ๊ฐ ๊ฒฐ๊ณผ์ ๋ํ ์ด๋ฒคํธ๋ฅผ ๋ฐํํ ๋ค์ ์์ธ๋ฅผ ๋ฐ์์ํค๊ธฐ๋ ํด์.
AuthorizationResult result = this.authorizationManager.authorize(this::getAuthentication, request);
this.eventPublisher.publishAuthorizationEvent(this::getAuthentication, request, result);
if (result != null && !result.isGranted()) {
throw new AuthorizationDeniedException("Access Denied", result);
}
chain.doFilter(request, response);
spring security์ AuthorizationManager
๋ฅผ ๋ณด๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํค๋ ์ฝ๋๊ฐ ์๋ ๊ฒ์ ๋ณด์๊ฒ ๋ ํ
๋ฐ ๊ทธ๋ฐ ๊ฒ๋ค๋ ์์ ๊ฐ์ ์ด์ ์์์ผ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค :)
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.
์์ธํ ์ค๋ช ๊ฐ์ฌํฉ๋๋ค. ๋จ์ ์ธ๊ฐ ์ฑ๊ณต, ์คํจ ๋ฟ๋ง ์๋๋ผ ์ฌ๋ฌ ์ผ์ด์ค์ ๋ํด์๋ ๊น์ด ์๊ฐ ๋ชปํ์๋๋ฐ, ๊ณ ๋ฏผํด๋ณผ ํฌ์ธํธ๋ค์!
AuthorizationManager์์๋ ์ธ๊ฐ ํ์ธ, AuthorizationFilter ์ ์ฒด์ ์ธ ๋งฅ๋ฝ์ ํ์ ํ์ฌ ํํฐ๋ง ํด์ฃผ๋ ๊ฒ์ด๊ตฐ์!
|
||
@FunctionalInterface | ||
public interface AuthorizationManager<T> { | ||
AuthorizationDecision check(Authentication authentication, T object); |
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.
์ต๊ทผ spring security ๋ฒ์ ์์๋ check
๋ deprecated๋์์ต๋๋ค. AuthorizationDecision
์ด ์ง๊ธ ๋ง๋ค์ด์ฃผ์ ๊ฒ๊ณผ ๊ฐ์ด ๊ตฌํ์ฒด๋ก ๋์ด์๊ณ , ์ผ๋ฐ์ ์ผ๋ก ์คํ์์ค๋ค์ด ์ปค์ง์๋ก ๋จ์ํ๊ฒ ๊ตฌํํด๋์๋ ๊ตฌํ์ฒด๋ค์ ์ถ์ํํ๋ ํํ๋ก ๊ฐ์ ํด๋๊ฐ๋๋ฐ์.
๊ทธ๋์ ์ต๊ทผ ๋ฒ์ ์์๋ ๊ตฌํ์ฒด๋ฅผ ๋ฐํํ๋ check
๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๊ธฐ๋ณด๋ค๋ deprecatedํด๋ ๋ค ์ด๋ฅผ ์ถ์ํํ AuthorizationResult
๋ฅผ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ฌ ํด๋น ๋ฉ์๋ ์ฌ์ฉ์ ๊ถ์ฅํ๊ณ ์์ต๋๋ค.
spring-projects/spring-security#14712
spring-projects/spring-security#14846
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.
๋ฏธ์
์ ํ๋ค๋ณด๋ ๋ง์ํด์ฃผ์ ๋๋ก AuthorizationDecision ๊ฐ deprecated๋ ์ด์ ๊ฐ ๊ฐ์ ์ ์ผ๋ก ๋๊ปด์ง๋ ๊ฒ ๊ฐ์ต๋๋ค.
RequestMatcherDelegatingAuthorizationManager ์์๋ checkInFilter ๋ณด๋ค๋ verifyInFilter๋ฅผ ์ฌ์ฉํ๋ค๋ฉด
AuthorizationFilter ์์์ ์ธ๊ฐ ์์ฒญ, ์์ธ์ฒ๋ฆฌ ๋๊ฐ์ง ์ญํ ์์ ์ธ๊ฐ ์์ฒญ๋ง ๋จ๊ฒ๋์ด์ ํจ์ฌ ๊น๋ํฉ๋๋ค!
- String๊ณผ Collection String์ ์ง์ํ๋๋ก ๋ณ๊ฒฝ
- AuthorizationDecision ์ ๋์ ์ํ์ฑ๊ณผ ์ฌ์ฉ๋ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ฉด์ ๊ฒฐ๊ณผ์ ๋ํ ์์ธ ์ฒ๋ฆฌ๋ ์ผ๊ด์ฑ์ ๊ฐ์ง์ง ๋ชปํ๊ธฐ์ verify ๋ฅผ ํตํด ๊ฒ์ฆ๊น์ง ์์
โฆ์ธ๊ฐ ์์ ์ ์์ํ๋๋ก ๋ณ๊ฒฝ - SecuredAuthorizationManager ์์๋ Annotation ๊ฐ์ ํตํ ๋์ ์ธ๊ฐ - RequestMatcherDelegatingAuthorizationManager ์์๋ ์ธ๊ฐ ์์ ์ ํ์ง ์์
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 ์ฝ๋ฉํธ ๋๋ dm์ผ๋ก ์์ฒญ ๋ถํ๋๋ฆฝ๋๋ค.
๊ฐ์ฌํฉ๋๋ค ๐โโ๏ธ
public class MvcRequestMatcher implements RequestMatcher { | ||
|
||
private final HttpMethod httpMethod; | ||
private final String[] requestURIs; |
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.
์ค์ ๋ก๋ httpMethod
, requestUri
๊ฐ๊ฐ ํ๋๋ง์ ๊ฐ์ง๋ฏ๋ก String[]
์ผ๋ก ํํ๋์ง ์์ต๋๋ค. ๋ฑ ํ๋๋ก ์ง์ ๋ method, uri๋ง์ ๊ฒ์ฆํ๋ฉด ๋๋๊ฑฐ์ฃ .
์ฌ๋ฌ๊ฐ์ uri๊ฐ ํ์ํ ๊ฒฝ์ฐ๋ผ๋ฉด ํ์ํ ๊ฐ์ฒด๊ฐ RequestMatcher
๋ฅผ ์ฌ๋ฌ๊ฐ๋ก ๋ค๊ณ ์๋ ํํ๊ฐ ๋ ์์ฐ์ค๋ฝ์ต๋๋ค :)
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.
ํ๋์ Matcher์ ํ๋์ URI ๊ฒ์ฆํ๋ ๊ฒ์ด ๋ ์ดํดํ๊ธฐ ์ข๊ฒ ๋ค์!
@@ -0,0 +1,25 @@ | |||
package nextstep.security.authorization; | |||
|
|||
public class AuthorizationDecision { |
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.
๋ฏธ์ ์์๋ spring security์ ๊ธฐ๋ฅ์ด ์ถ์๋์ด true, false๊ฐ ์ ์์๋ฟ์ผ์ค ์ ์์ผ๋ ์ค์ ๋ก๋ ๊ทธ๋ณด๋ค ๋ ๋ค์ํ ๊ตฌํ์ฒด๋ค์ด ์๊ธธ ์ ์์ต๋๋ค.
์๋ ํ์ธ์.
์ธ๊ฐ(Authorization) ๋ฆฌ๋ทฐ ์์ฒญํฉ๋๋ค.
์ด๋ฒ ๋ฏธ์ ์์ ๊ตฌํํ ๋ด์ฉ์ ์ ์์ง๋ง ์ดํด ํ๋ ๊ฒ ์ฝ์ง ์์๋ ๊ฒ ๊ฐ์์.
ํนํ RequestMatcherDelegatingAuthorizationManager๊ฐ ํ๋ฒ์ ์ดํดํ๊ธฐ ์ด๋ ค์ ์ต๋๋ค.
๊ถํ ์ธ๊ฐ ์ฒ๋ฆฌ๋ฅผ ํ ๋์ AOP๋ฅผ ์ด๋ค๋ฉด ์ธํฐ์ ํฐ๋ Aspect๋ฅผ ์ฌ์ฉ๋ ๊ฒ ์ธ๋ฐ, ํํฐ์ ์ฌ์ฉ๋๋ ๊ณณ๋ ์ฒ๋ฆฌ๋ ๋ค๋ฅด๋ค๊ณ ์๊ฐ์ด ํ์ต๋๋ค.
๊ทธ๋์ AuthorizationManager ์ค์ ํํฐ์์ ์ฌ์ฉ๋๋ ์ข ๋ฅ์ ์ธํฐ์ ํฐ์ Aspect์์ ์ฌ์ฉ๋๋ ์ข ๋ฅ๋ฅผ ๊ตฌ๋ถํ์์ต๋๋ค.
์๋ฅผ ๋ค์ด AuthorityAuthorizationManager ์์ ํํฐ ๊ธฐ๋ฐ์ผ๋ก ๊ถํ ์ธ๊ฐ ์ฒ๋ฆฌ๋ฅผ ํ๊ณ , SecuredAuthorizationManager ์ธํฐ์ ํฐ( ์ด๋ ธํ ์ด์ ) ๊ธฐ๋ฐ์ผ๋ก ๋ค์ ํ๋ฒ ๊ถํ ์ธ๊ฐ ์ฒ๋ฆฌ๊ฐ ํ๊ณ ์์ต๋๋ค.
ํ์ง๋ง ๊ถํ ์ธ๊ฐ ์ฒ๋ฆฌ๊ฐ ์ค๋ณต์ด ๋๊ณ ์๋ ๊ฒ ์๋๊ฐ ํ๋ ๊ถ๊ธ์ฆ๋ ์๊ธฐ๋ค์!
๋ฆฌ๋ทฐ ์ ๋ถํ ๋๋ฆฝ๋๋ค.