📘 Premium Read: Access my best content on
Medium member-only articles
— deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed.
Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount):
Explore My Udemy Courses
— Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers):
Java Guides on YouTube
In this article, we will discuss different ways to retrieve the currently logged-in user details in Spring Security.
Let's see how programmatic access currently authenticated user.
1. Using SecurityContextHolder + Authentication.getName()
The simplest way to retrieve the currently authenticated principal is via a static call to the SecurityContextHolder:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
return currentUserName;
}
2. Using SecurityContextHolder + UserDetails.getUsername()
The API of the Authentication class is very open so that the framework remains as flexible as possible. Because of this, the Spring Security principal can only be retrieved as an Object and needs to be cast to the correct UserDetails instance:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
// getUsername() - Returns the username used to authenticate the user.
System.out.println("User name: " + userDetails.getUsername());
// getAuthorities() - Returns the authorities granted to the user.
System.out.println("User has authorities: " + userDetails.getAuthorities());
3. Get the User in a Controller
In a @Controller annotated bean, there are additional options. The principal can be defined directly as a method argument and it will be correctly resolved by the framework:
@Controller
public class SecurityController {
@RequestMapping(value = "/username", method = RequestMethod.GET)
@ResponseBody
public String currentUserName(Principal principal) {
return principal.getName();
}
}
Alternatively, we can also use the authentication token:
@Controller
public class SecurityController {
@RequestMapping(value = "/username", method = RequestMethod.GET)
@ResponseBody
public String currentUserName(Authentication authentication) {
return authentication.getName();
}
}
4. How to Get a Current Logged In Username in JSP
5. How to Access User Roles in JSP
6. How to Get Current Logged-In Username in Themeleaf
Related Spring Security Tutorials/Guides:
Core Components of Spring Security
Spring Security: Authentication
Spring Security: Authorization
Spring Security: Principal
Spring Security: Granted Authority
Spring Security: SecurityContextHolder
Spring Security: UserDetailsService
Spring Security: Authentication Manager
Spring Security: Authentication Provider
Spring Security: Password Encoder
AuthenticationEntryPoint in Spring Security
@PreAuthorize Annotation in Spring Security
Spring Security Basic Authentication
Spring Security In-Memory Authentication
Spring Security Form-Based Authentication
Difference Between Basic Authentication and Form Based Authentication
Spring Security Custom Login Page
Spring Security Login Form Example with Database Authentication
Spring Boot Login REST API
Login and Registration REST API using Spring Boot, Spring Security, Hibernate, and MySQL Database
Spring Boot + Spring Security + Angular Example Tutorial
Spring Boot + Angular Login Authentication, Logout, and HttpInterceptor Example
Spring Security In-Memory Authentication Example
Spring Security Hibernate Database Authentication - UserDetailsService
Securing a Spring MVC Application with Spring Security
Spring Boot Security Login REST API Example
Spring Boot Security Login and Registration REST API
Role-based Authorization using Spring Boot and Spring Security
Spring Boot Security JWT Token-Based Authentication and Role-Based Authorization Tutorial
Spring Boot + Spring Security + JWT + MySQL Database Tutorial
Spring Boot JWT Authentication and Authorization Example
Spring Boot Security JWT Example - Login REST API with JWT Authentication
Spring Boot Security JWT Token-Based Authentication and Role-Based Authorization Tutorial
Spring Security - Get Current Logged-In User Details
Spring Security - How to Get Current Logged-In Username in JSP
Spring Security - How to Access User Roles in JSP
Spring Security - How to Get Current Logged-In Username in Themeleaf
Spring Security Tutorial - Registration, Login, and Logout
Spring Boot 2 + Spring MVC + Role-Based Spring Security + JPA + Thymeleaf + MySQL Tutorial
User Registration Module using Spring Boot 2 + Spring MVC + Spring Security + Hibernate 5 + Thymeleaf + MySQL
Registration and Login using Spring Boot, Spring Security, Spring Data JPA, Hibernate, H2, JSP, and Bootstrap
Spring Boot User Registration and Login Example Tutorial
Comments
Post a Comment
Leave Comment