charminseok
[Spring Security] SecurityContextHolder 본문
스프링 시큐리티는 표준 servlet Filter를 이용한 프레임워크로 Servlet Container를 통해 기능을 제공받습니다. 그렇기 때문에 서블릿 컨테이너를 실행하는 모든 어플리케이션에서도 사용할 수 있습니다.
이중에서도 인증을 담당하는 모델의 중심에는 SecurityContextHolder가 있습니다.
그림에서와 같이 SecurityContextHolder는 SecurityContext를 포함하고 또 그 안에는 사용자 정보인 Authentication을 가지고 있습니다.
사용자 인증정보를 저장하는 간단한 방법으로 SecurityContextHolder를 설정하는 것인데, 여기서 중요한 점은 SecurityContextHolder.getContext().setAuthentication(authentication); 이 코드를 통해 SecurityContext를 가져오는게 아니라 빈 컨텍스트를 생성해야한다. 아니면 멀티스레드 환경에서 문제가 생길 수 있다.
처음 아무것도 몰랐을때는 SecurityContextHolder.getContext().setAuthentication(authentication);로 썼는데 바꿔야 한다....
사용자 인증을 했다면 SecurityContextHolder.getContext().getAuthentication()에서 Authentication 타입 객체를 가져올 수 있고 여기에 사용자 정보가 들어있다.
이 중에서 Principal은 Object principal = authentication.getPrincipal(); 로 가져올 수 있는데 UserDetails 객체로 받아온다.
principal - username/password로 인증할 땐 보통 UserDetails 인스턴스로 사용한다.
credentials - 주로 비밀번호로 사용되며, 인증한 이후 유출되지 않도록 없앤다.
authorities - ROLE_USER, ROLE_ADMIN 등 Principal 이 가지고 있는 권한을 나타낸다.
Reference
'Spring' 카테고리의 다른 글
[Spring Boot] 자동 설정 (0) | 2021.02.12 |
---|---|
[Spring Boot] 의존성 관리 (0) | 2021.02.10 |
[Spring Boot] (0) | 2021.01.31 |
[Spring] AOP (0) | 2021.01.26 |
[spring] SpEL (0) | 2021.01.26 |