-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d4add2f
feat: add /members/me API
parkjun5 bc949f3
ref: refactoring CheckAuthenticationFilter to AuthorizationFilter
parkjun5 6d5c693
feat: add AuthorizationManager for authorize
parkjun5 646c495
feat: add request matcher for find AuthorizationManager
parkjun5 432239c
feat: add AuthorizationManagers for delegate Authorities
parkjun5 e301ed4
ref: change test and Add SecureAnnotation to Get /members API
parkjun5 8b70fdd
feat: Filter And Interceptor delegate authorization control to Authorโฆ
parkjun5 1f37f27
feat: add RequestMatcherDelegatingAuthorizationManager and config chaโฆ
parkjun5 a0c8b51
docs: ์๊ตฌ ์ฌํญ ์ ๋ฆฌ
parkjun5 b83d2a2
polishing
parkjun5 c1a2089
ref: Filter ์์ ์ฌ์ฉํ๋ ์ธ๊ฐ์ Interceptor ์์ ํ์ธํ๋ ์ธ๊ฐ ๋ถ๋ฆฌ
parkjun5 0179bf5
polishing
parkjun5 fbbf311
ref:
parkjun5 bb7a79e
polishing
parkjun5 9bf5f29
polishing
parkjun5 8126c74
ref: Change AuthorityAuthorizationManager to Generic
7e23368
ref: use verify not check method
7a213b0
ref: AuthorizationDecision ์ static ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
0e5d010
ref: mvc match logic not Stream change to ForEach
5c49d3e
ref: SecuredAuthorizationManager ์์ AuthorityAuthorizationManager ์๊ฒ โฆ
fc947f5
test: ์ธ์ฆ ์คํจ์์๋ AccessDeniedException ์ธ๊ฐ ์คํจ์๋ ForbiddenException ๋๋๋ก ๋ณ๊ฒฝ
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
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 +1,39 @@ | ||
# spring-security-authorization | ||
|
||
## ์ค์ต | ||
|
||
1. [x] GET /members/me ์๋ํฌ์ธํธ ๊ตฌํ ๋ฐ ํ ์คํธ ์์ฑ | ||
2. [x] ๊ถํ ๊ฒ์ฆ ๋ก์ง์ AuthorizationFilter๋ก ๋ฆฌํฉํฐ๋ง | ||
|
||
## ๐ 1๋จ๊ณ - AuthorizationManager๋ฅผ ํ์ฉ | ||
|
||
์๊ตฌ์ฌํญ | ||
|
||
- [x] AuthorizationManager ๋ฅผ ํ์ฉํ์ฌ ์ธ๊ฐ ๊ณผ์ ์ถ์ํ | ||
- [x] ์ธ๊ฐ๋ฅผ ์ฒ๋ฆฌํด์ค AuthorizationManager ์์ฑ | ||
- [x] RequestMatcherDelegatingAuthorizationManager ๋ฅผ ํตํ AuthorizationManager ํ๋ฒ์ ๊ด๋ฆฌ? | ||
- [x] ์ธ๊ฐ ๊ณผ์ ์ ์ถ์ํํ AuthorizationManager ๋ฅผ ์์ฑํ๋ค. ์ด ๋ ํ์ํ AuthorizationDecision๋ ํจ๊ป ์์ฑํ๋ค. (์ค์ AuthorizationManager์๋ | ||
verify๋ ์๋๋ฐ ์ด ๋ถ๋ถ์ ๋ํ ๊ตฌํ์ ์ ํ) | ||
- [x] SecuredMethodInterceptor์ Authorization Filter์์ ์์ฑ๋ ์ธ๊ฐ ๋ก์ง์ AuthorizationManager๋ก ๋ฆฌํฉํฐ๋ง ํ๋ค. | ||
|
||
## ๐ 2๋จ๊ณ - ์์ฒญ๋ณ ๊ถํ ๊ฒ์ฆ ์ ๋ณด ๋ถ๋ฆฌ | ||
|
||
์๊ตฌ์ฌํญ | ||
|
||
- [x] ์์ฒญ๋ณ ๊ถํ ๊ฒ์ฆ ์ ๋ณด๋ฅผ ๋ณ๋์ ๊ฐ์ฒด๋ก ๋ถ๋ฆฌํ์ฌ ๊ด๋ฆฌ | ||
- [x] RequestMatcherRegistry์ RequestMatcher๋ฅผ ์์ฑํ๊ณ , RequestMatcher์ ๊ตฌํ์ฒด๋ฅผ ์์ฑํ๋ค. | ||
- [x] AnyRequestMatcher: ๋ชจ๋ ๊ฒฝ์ฐ true๋ฅผ ๋ฆฌํดํ๋ค. | ||
- [x] MvcRequestMatcher: method์ pattern(uri)๊ฐ ๊ฐ์์ง ๋น๊ตํ์ฌ ๋ฆฌํดํ๋ค. | ||
- [x] RequestMatcherEntry์ T entry๋ ์๋์ ํด๋น๋๋ ๊ฐ ์์ฒญ๋ณ ์ธ๊ฐ ๋ก์ง์ ๋ด๋นํ๋ AuthorizationManager๊ฐ ๋๋ค. | ||
- [x] /login์ ๋ชจ๋ ์์ฒญ์ ๋ฐ์ ์ ์๋๋ก PermitAllAuthorizationManager๋ก ์ฒ๋ฆฌ | ||
- [x] /members/me๋ ์ธ์ฆ๋ ์ฌ์ฉ์๋ง์๊ฒ๋ง ๊ถํ์ ๋ถ์ฌํ๊ธฐ ์ํด AuthenticatedAuthorizationManager๋ก ์ฒ๋ฆฌ | ||
- [x] /members๋ "ADMIN" ์ฌ์ฉ์๋ง์๊ฒ๋ง ๊ถํ์ ๋ถ์ฌํ๊ธฐ ์ํด HasAuthorityAuthorizationManager๋ก ์ฒ๋ฆฌ | ||
- [x] ๊ทธ ์ธ ๋ชจ๋ ์์ฒญ์ ๊ถํ์ ์ ํํ๊ธฐ ์ํด DenyAllAuthorizationManager๋ก ์ฒ๋ฆฌ | ||
|
||
์๋ ๊ฐ์ฒด์ ์ํ๋ฆฌํฐ ์ฝ๋ ํ์ธ | ||
// SpEL | ||
// Role Authority | ||
// Role Hierarchy | ||
// AuthoritiesAuthorizationManager | ||
// SecureMethodSecurityConfiguration | ||
// SecuredAuthorizationManager |
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
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
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
8 changes: 8 additions & 0 deletions
8
src/main/java/nextstep/security/authorization/AccessDeniedException.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package nextstep.security.authorization; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.FORBIDDEN) | ||
public class AccessDeniedException extends RuntimeException { | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/nextstep/security/authorization/AuthorizationDecision.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nextstep.security.authorization; | ||
|
||
public class AuthorizationDecision { | ||
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. ๋ฏธ์ ์์๋ spring security์ ๊ธฐ๋ฅ์ด ์ถ์๋์ด true, false๊ฐ ์ ์์๋ฟ์ผ์ค ์ ์์ผ๋ ์ค์ ๋ก๋ ๊ทธ๋ณด๋ค ๋ ๋ค์ํ ๊ตฌํ์ฒด๋ค์ด ์๊ธธ ์ ์์ต๋๋ค. |
||
|
||
private static final AuthorizationDecision GRANTED = new AuthorizationDecision(true); | ||
private static final AuthorizationDecision DENIED = new AuthorizationDecision(false); | ||
|
||
private final boolean isGranted; | ||
|
||
public static AuthorizationDecision granted() { | ||
return GRANTED; | ||
} | ||
|
||
public static AuthorizationDecision denied() { | ||
return DENIED; | ||
} | ||
|
||
protected AuthorizationDecision(final boolean isGranted) { | ||
this.isGranted = isGranted; | ||
} | ||
|
||
public boolean isDenied() { | ||
return !isGranted; | ||
} | ||
} |
36 changes: 13 additions & 23 deletions
36
...horization/CheckAuthenticationFilter.java โ ...ty/authorization/AuthorizationFilter.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,38 +1,28 @@ | ||
package nextstep.security.authorization; | ||
|
||
import nextstep.security.authentication.Authentication; | ||
import nextstep.security.context.SecurityContextHolder; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import nextstep.security.authentication.Authentication; | ||
import nextstep.security.authorization.manager.RequestMatcherDelegatingAuthorizationManager; | ||
import nextstep.security.context.SecurityContextHolder; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
public class CheckAuthenticationFilter extends OncePerRequestFilter { | ||
private static final String DEFAULT_REQUEST_URI = "/members"; | ||
public class AuthorizationFilter extends OncePerRequestFilter { | ||
|
||
private final RequestMatcherDelegatingAuthorizationManager authorizationManager; | ||
|
||
public AuthorizationFilter(RequestMatcherDelegatingAuthorizationManager authorizationManager) { | ||
this.authorizationManager = authorizationManager; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
if (!DEFAULT_REQUEST_URI.equals(request.getRequestURI())) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication == null || !authentication.isAuthenticated()) { | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
return; | ||
} | ||
|
||
Set<String> authorities = authentication.getAuthorities(); | ||
if (!authorities.contains("ADMIN")) { | ||
response.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
return; | ||
} | ||
|
||
authorizationManager.verifyInFilter(request, authentication); | ||
filterChain.doFilter(request, response); | ||
} | ||
} |
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
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
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
17 changes: 17 additions & 0 deletions
17
src/main/java/nextstep/security/authorization/manager/AuthenticatedAuthorizationManager.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package nextstep.security.authorization.manager; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import nextstep.security.authentication.Authentication; | ||
import nextstep.security.authorization.AuthorizationDecision; | ||
|
||
public class AuthenticatedAuthorizationManager implements AuthorizationManager<HttpServletRequest> { | ||
|
||
@Override | ||
public AuthorizationDecision check(Authentication authentication, HttpServletRequest object) { | ||
if (authentication == null || !authentication.isAuthenticated()) { | ||
return AuthorizationDecision.denied(); | ||
} | ||
|
||
return AuthorizationDecision.granted(); | ||
} | ||
} |
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.
์ ์ ํ๊ฒ ์ ์ถ๊ฐํด์ฃผ์ จ๋ค์ :)