Spring Security Fetch Logged in Username

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.

1. SecurityContextHolder - Authentication.getName()
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";
 
  }
  //...


2. SecurityContextHolder - User.getUsername()
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";
 
  }
  //...

3. UsernamePasswordAuthenticationToken-
This is more elegant solution, in runtime, Spring will inject "UsernamePasswordAuthenticationToken" into the "Principal" interface.

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";
 
  }
  //...




Download Source Code-
SpringSecurityFetchedUsernameExample.zip


References-
http://www.dineshonjava.com/2013/02/spring-security-form-based-login-example.html
Spring Security


 


                             <<previous<<             || index  ||         >>next>>

2 comments:

  1. Dear Sir,
    Can you please post an example hot get the first name and last name from database when the USER table has columns like: userid, first name, last name, active. And password is in another table (a table PASSWORD table has columns: userid, password).

    Thanks,
    Sudhir

    ReplyDelete
    Replies
    1. Hi Sudhir,

      Could you please send your requirement to the "admin@dineshonjava.com" then we will reply to your same mail id with attached application as well as later we will post same application on "dineshonjava.com"

      Thanks
      Dinesh

      Delete