SecurityContext제공, 기본적으로 ThreadLocal을 사용한다
ThreadLocal
한 Thread 내에서 공유하는 저장소, 파라미터 없이 데이터를 넣거나 참조할 수 있다
Authentication 제공

Principal과 GrantAuthority 제공, Principal이라는 정보를 Authentication이라는 객체에 담아서 관리
인증이 되면 Authentication 객체를 만들고 이를 SecurityContextHolder안에 넣어서 관리
Principal
인증된 사용자의 정보, 누구에 해당하는 정보
UserDetailsService에서 리턴한 그 객체
객체는 UserDetails 타입
GrantAuthority
"ROLE_USER", "ROLE_ADMIN"등 Principal이 가지고 있는 권한을 나타낸다
인증 이후, 인가 및 권한 확인 할 때 이 정보를 참조한다
UserDetails
애플리케이션이 가지고 있는 유저 정보와 스프링 시큐리티가 사용하는 Authentication 객체 사이의 어댑터
UserDetailsService
유저정보를 UserDetails 타입으로 가져오는 DAO 인터페이스, UserDetailsService가 인증을 하는것은 아니다. AuthenticationManager가 인증을 수행
SimpleService를 만들고 dashboard에서 메소드를 호출한다
@Service
public class SampleService {
public void dashboard() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Object principal = authentication.getPrincipal(); //사용자
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); //사용자가 가지고 있는 권한
Object credentials = authentication.getCredentials();
boolean authenticated = authentication.isAuthenticated();
}
}
@GetMapping("/dashboard")
public String dashboard(Model model, Principal principal) {
model.addAttribute("message", "Hello " + principal.getName());
sampleService.dashboard();
return "dashboard";
}
authntication의 경우 loginform으로 인증하면 UsernamePasswordAuthentication 타입이 되고 principal은 UserDetailsService에서 리턴한 타입이 된다