<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>We already used the functionality of fetched username from the Spring Security. In this tutorial we will only discuss about the ways of how to fetching the logged in username. we show you three ways to get current logged in username in Spring Security.</p>
<div id="ads-id" align="center"></div>
<p><b>1. SecurityContextHolder &#8211; Authentication.getName()</b></p>
<pre class="highlight">import org.springframework.security.core.Authentication; 
import org.springframework.security.core.context.SecurityContextHolder; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
 
@Controller 
public class LoginController { 
 
 @RequestMapping(value="/login", method = RequestMethod.GET) 
 public String printUser(ModelMap model) { 
 
 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
 String name = auth.getName(); //get logged in username 
 model.addAttribute("message", "Welcome To Login Form Based Spring Security Example!!!"); 
 model.addAttribute("author", name); 
 return "welcome"; 
 
 } 
 //... 
 
</pre>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>2. SecurityContextHolder &#8211; User.getUsername()</b></p>
<pre class="highlight">import org.springframework.security.core.context.SecurityContextHolder; 
import org.springframework.security.core.userdetails.User; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
 
@Controller 
public class LoginController { 
 
 @RequestMapping(value="/login", method = RequestMethod.GET) 
 public String printUser(ModelMap model) { 
 
 User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
 String name = user.getUsername(); //get logged in username 
 
 model.addAttribute("author", name); 
 model.addAttribute("message", "Welcome To Login Form Based Spring Security Example!!!"); 
 return "welcome"; 
 
 } 
 //... 
</pre>
<p><b>3. UsernamePasswordAuthenticationToken-</b><br />
This is more elegant solution, in runtime, Spring will inject &#8220;<b>UsernamePasswordAuthenticationToken</b>&#8221; into the &#8220;<b>Principal</b>&#8221; interface.</p>
<pre class="highlight">import java.security.Principal; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
 
@Controller 
public class LoginController { 
 
 @RequestMapping(value="/login", method = RequestMethod.GET) 
 public String printWelcome(ModelMap model, Principal principal ) { 
 
 String name = principal.getName(); //get logged in username 
 model.addAttribute("author", name); 
 model.addAttribute("message", "Welcome To Login Form Based Spring Security Example!!!"); 
 return "welcome"; 
 
 } 
 //... 
</pre>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/custom4.png" width="600" height="155" border="0" /></div>
<p><b>Download Source Code-</b><br />
<a href="https://sites.google.com/site/dinesh9582486434/my-forms/SpringSecurityCustomErrorMsg.zip?attredirects=0&;d=1" target="_blank" rel="noopener"><b>SpringSecurityFetchedUsernameExample.zip</b></a><br />
<b><br />
</b> <b><br />
</b> <b>References-</b><br />
<b><a href="https://dineshonjava.com/spring-security-form-based-login-example/" target="_blank" rel="noopener"><b>https://dineshonjava.com/spring-security-form-based-login-example/</b></a><br />
<a href="http://static.springsource.org/spring-security/site/" target="_blank" rel="noopener"><b>Spring Security</b></a></b></p>
<p><b><b> </b> </b></p>
</div>
<p> ;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Security Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-hello-world-example/"><span style="color: red;">Spring Security XML Based Hello World Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-form-based-login-example/"><span style="color: red;">Spring Security form-based login example </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-login-form-using/"><span style="color: red;">Spring Security Login Form Based Example Using Database</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-http-basic/"><span style="color: red;">Spring Security Authentication Example Using HTTP Basic </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-authorized-access/"><span style="color: red;"> Spring Security Authorized Access Control Example </span></a></b></li>
<li><b><a href="https://dineshonjava.com/customize-http-403-access-denied-page/"><span style="color: red;">Spring Security Customized Access Denied Page</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-custom-error-message/"><span style="color: red;">Spring Security Custom Error Message</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-logout-example/"><span style="color: red;"> Spring Security Logout Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-fetch-logged-in-username/"><span style="color: red;">Spring Security Fetch Logged in Username</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-password-hashing/"><span style="color: red;">Spring Security Password Hashing</span></a></b></li>
</ul>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: #ff99cc;"> <b> <;<;<a href="https://dineshonjava.com/spring-security-logout-example/">previous</a><;<; || <a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/">index </a>|| >;>;<a href="https://dineshonjava.com/spring-security-password-hashing/">next</a>>;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-security-logout-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '570'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…