What are SecurityContext and SecurityContextHolder in Spring Security?

SecurityContext holds the authentication details of the currently logged-in user, and SecurityContextHolder is the static access point that stores and retrieves that SecurityContext for the duration of a request.

Key Points

Example

Inside a service method, calling SecurityContextHolder.getContext().getAuthentication().getName() returns the currently logged-in username without needing it passed down from the controller.

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String currentUsername = auth.getName();
Collection<? extends GrantedAuthority> roles = auth.getAuthorities();

Interview Tip

A concise interview answer is

SecurityContext holds the current user's Authentication—their principal, credentials, and authorities—while SecurityContextHolder is the static, thread-local access point Spring Security uses to store and retrieve that context. It's how any code in the request can access the current user without it being explicitly passed around.

Last updated: 11 November 2026

Previous question: Explain Cross-Origin Resource Sharing (CORS) and how you would configure it in a Spring Boot application.

Next question: What is the OAuth2 authorization code grant type?

More Spring Security - Level I interview questions

All Spring Security - Level I questions