์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- ์ฐํ ์ฝ 7๊ธฐ
- ํ๋ฆฌ์ฝ์ค
- ํ ๋งํ
- ์ฐํ ์ฝ
- BFS
- ๋์์ธ ํจํด
- ํ๋ก๊ทธ๋๋จธ์ค
- C
- SQL
- Java
- ๋ก๊ทธ์ธ
- ๋ธ๋ก๊ทธ
- c#
- permissionevaluator
- lv2
- C++
- 7๊ธฐ
- ํ๊ณ
- 2์ฐจ์ ๋ฐฐ์ด
- ๋ฐฑ์ค
- c์ธ์ด
- 2455
- Spring
- DBMS
- JDBC
- OCP
- cpp
- dailelog
- 11660
- ๋์์ธํจํด
- Today
- Total
Dailelog
PermissionEvaluator์ ๊ถํ ์ธ๊ฐ์ฒ๋ฆฌ,@PreAuthorize ๋ํด ๋ณธ๋ฌธ
PermissionEvaluator์ ๊ถํ ์ธ๊ฐ์ฒ๋ฆฌ,@PreAuthorize ๋ํด
Daile 2024. 6. 28. 02:47์๋ก
<aside> ๐ก Controller์์ ์ธ๊ฐ์ฒ๋ฆฌํ๋ ๋ถ๋ถ์ ๊ณต๋ถํ๋ฉด์ @Secured(),@PreAuthorize, @PostAuthorize 3๊ฐ์ ์ด๋ ธํ ์ด์ ์ ์ด์ฉํด์ ์ธ๊ฐ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ๋ฉด ๋๋ค๋ ๊ฒ์ ์๊ณ ์ด๋ฒ์ @PreAuthorize์ ๋ํด์ ์ง์คํด์ ์ด์ผ๊ธฐ ํด๋ณผ ์๊ฐ์ด๋ค.
</aside>
@PreAuthorize
@PreAuthorize("hasRole('ROLE_ADMIN')&&hasPermission(#postId,'POST','DELETE')")
@DeleteMapping("/posts/{postId}")
public void delete(@PathVariable(name = "postId") Long postId) {
PostResponse response = postService.get(postId);
postService.delete(response.getId());
}
์ ์ฝ๋๋ ์์ ์ด ์์ฑํ ๊ฒ์๊ธ์ ์ญ์ ํ๋ ๊ธฐ๋ฅ์์ post ์ ์์ฑ์ ์ ๋ณด์ ์ญ์ ์์ฒญ ์ ๋ณด์ ์ ์ ์ ๋ณด์ ๊ถํ์ ํ์ธํ๊ณ ์ธ๊ฐ์ฒ๋ฆฌ๋ฅผ ์์ฒญํ๋ ๋ฉ์๋์ด๋ค.
• @PreAuthorize: ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์ ์ธ์ฆ์ ๊ฑฐ์น๋ค.
Secured ์ ๋ ธํ ์ด์ ๊ณผ๋ ๋ค๋ฅด๊ฒ ํจ์๋ฅผ String ํํ๋ก ๋ฃ์ด์ค ์ ์๊ณ , and๋ or ๋ฑ ๋ ผ๋ฆฌ ์ฐ์ฐ์๋ ๋ฃ์ด์ค ์ ์๋ค.
์ ๋ ธํ ์ด์ ๋ด์์ ์ฌ์ฉ๊ฐ๋ฅํ ํจ์/๊ธฐ๋ฅ๋ค
- hasRole([role]) : ํ์ฌ ์ฌ์ฉ์์ ๊ถํ์ด ํ๋ผ๋ฏธํฐ์ ๊ถํ๊ณผ ๋์ผํ ๊ฒฝ์ฐ true
- hasAnyRole([role1,role2 ...]) : ํ์ฌ ์ฌ์ฉ์์ ๊ถํ ํ๋ผ๋ฏธํฐ๋ค์ ๊ถํ ์ค ์ผ์นํ๋ ๊ฒ์ด ์๋ ๊ฒฝ์ฐ true
- principal: ์ฌ์ฉ์๋ฅผ ์ฆ๋ช ํ๋ ์ฃผ์๊ฐ์ฒด(User)๋ฅผ ์ง์ ์ ๊ทผํ ์ ์๋ค.
- authentication : SecurityContext์ ์๋ authentication ๊ฐ์ฒด์ ์ ๊ทผ ํ ์ ์๋ค.
- permitAll : ๋ชจ๋ ์ ๊ทผ ํ์ฉ
- denyAll : ๋ชจ๋ ์ ๊ทผ ๋นํ์ฉ
- isAnonymous() : ํ์ฌ ์ฌ์ฉ์๊ฐ ์ต๋ช (๋น๋ก๊ทธ์ธ)์ธ ์ํ์ธ ๊ฒฝ์ฐ true
- isRememberMe() : ํ์ฌ ์ฌ์ฉ์๊ฐ RememberMe ์ฌ์ฉ์๋ผ๋ฉด true
- isAuthenticated() : ํ์ฌ ์ฌ์ฉ์๊ฐ ์ต๋ช ์ด ์๋๋ผ๋ฉด (๋ก๊ทธ์ธ ์ํ๋ผ๋ฉด) true
- isFullyAuthenticated() : ํ์ฌ ์ฌ์ฉ์๊ฐ ์ต๋ช ์ด๊ฑฐ๋ RememberMe ์ฌ์ฉ์๊ฐ ์๋๋ผ๋ฉด true
hasPermission
hasPermission์ permission ๊ถํ์ ํ์ธํ๋ ์ญํ์ ํ๋ ๋ฉ์๋๋ก PermissionEvaluator ์ด๋ผ๋ ์ธํฐํ์ด์ค์ ๋ฉ์๋์ด๋ค.
์๋์ ์ฝ๋๋ PermissionEvaluator์ ์ฝ๋์ด๋ค.
public interface PermissionEvaluator extends AopInfrastructureBean {
boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission);
boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission);
}
์์ PermissionEvaluator์ ๊ตฌํํ๋ ๋ณ๋์ ๊ตฌํ์ฒด๋ฅผ ๋ง๋ค์๋ค.
PermissionEvaluator ๊ตฌํ์ฒด
@Slf4j
@RequiredArgsConstructor
public class DailelogPermissionEvaluator implements PermissionEvaluator {
private final PostRepository postRepository;
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
return false;
}
@Override
public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
var principal = (UserPrincipal) authentication.getPrincipal();
Post post = postRepository.findById((Long) targetId).orElseThrow(PostNotFound::new);
if(!post.getUserId().equals(principal.getUserId())){
log.error("[์ธ๊ฐ ์คํจ] ํด๋น ์ฌ์ฉ์๊ฐ ์์ฑํ ๊ธ์ด ์๋๋๋ค. targetId = {}",targetId);
return false;
}
return true;
}
}
๊ฐ ํ๋ผ๋ฏธํฐ๋ฅผ ์ด์ฉํด์ ์ธ์ฆ๊ฐ์ฒด์์ Principal๋ฅผ ๊ฐ์ ธ์ ๋์ ๋น์ฆ๋์ค ๋ก์ง์ ๋ง๋ UserPrincipal๋ก ๊ฐ์ ธ์์
targetId๋ฅผ ํตํด ํ๊ฒ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์์ ๋ด๋ถ์ ์์ฑ์ ์์ด๋์ UserPrincipal์ ์์ด๋๋ฅผ ๋น๊ต ํ ๊ทธ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ์ธ๊ฐ ์น์ธ ๊ฒฐ๊ณผ๋ฅผ boolean์ผ๋ก ๋๋ ค์ค๋ค.
'JAVA > JAVA SPRING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
application.yml ์ปค์คํ ์ค์ ๊ฐ ์ฌ์ฉ (0) | 2024.06.12 |
---|---|
JAVA Spring API - ์กฐํ API ์ง์ฐ ๋ก๋ฉ๊ณผ ์ฑ๋ฅ ์ต์ ํ (0) | 2024.03.31 |