로그인한 사용자 아이디
String id = SecurityContextHolder.getContext().getAuthentication().getName();
로그인한 사용자의 권한 구하기
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
Iterator<? extends GrantedAuthority> iter = authorities.iterator();
GrantedAuthority auth = iter.next();
String role = auth.getAuthority();
위 방식으로 로그인한 사용자가 쓴 글인지 일치여부를 확인할 수 있고,
특정 접근 권한이 있는 사용자만 특정 페이지에 접근할 수 있도록 설정할 수 있다.
'Back-end' 카테고리의 다른 글
[JPA] JPA에서 On Delete Cascade 제약조건 사용하기 (1) | 2024.06.03 |
---|---|
[UnitTest] JUnit과 Mockito 기반 Spring 단위테스트 (2) | 2024.05.31 |
[JPA] 최신순으로 목록 조회 (0) | 2024.05.30 |
[JPA] 양방향 매핑 (0) | 2024.05.13 |
Code block test (0) | 2024.05.13 |