renan gado
renan gado

Reputation: 59

Spring mvc getting user id from logged user

I am using spring security to login and logout, eveything works fine.

I can get username from logged user fine, however i need userID,

I would like to know how can i get user as an object from logged in user or how could i get userID

 @RequestMapping("/contato")
 public String contato(Model model, Principal principal ){

    String userName = principal.getName();
    model.addAttribute("userName",userName);
    System.out.println(userName);
    return "contato";
}

Bean

import java.sql.Date;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

public class Users {
private int user_id;
@NotBlank
@Size(min=1, max=100, message="Name must be between 1 and 100 characters")
private String firstname;
@NotBlank
private String surname;
@NotNull
private Date dob;
@NotBlank
@Email
private String username;
@NotBlank
private String telephone;
@NotBlank
private String address;
@NotBlank
private String city;
@NotBlank
private String country;
@NotBlank
private String postcode;
@NotBlank
@Size(min=6, message="Password must be have more than 6 characters")
private String password;
private boolean enabled = false;
private String authority;


public Users() {

}


   public Users(int user_id, String firstname, String surname, Date dob, String username, String telephone,
        String address, String city, String country, String postcode, String password, boolean enabled,
        String authority) {
    super();
    this.user_id = user_id;
    this.firstname = firstname;
    this.surname = surname;
    this.dob = dob;
    this.username = username;
    this.telephone = telephone;
    this.address = address;
    this.city = city;
    this.country = country;
    this.postcode = postcode;
    this.password = password;
    this.enabled = enabled;
    this.authority = authority;
}





public int getUser_id() {
    return user_id;
}


public void setUser_id(int user_id) {
    this.user_id = user_id;
}


public String getFirstname() {
    return firstname;
}


public void setFirstname(String firstname) {
    this.firstname = firstname;
}


public String getSurname() {
    return surname;
}


public void setSurname(String surname) {
    this.surname = surname;
}


public Date getDob() {
    return dob;
}


public void setDob(Date dob) {
    this.dob = dob;
}


public String getUsername() {
    return username;
}


public void setUsername(String username) {
    this.username = username;
}


public String getTelephone() {
    return telephone;
}


public void setTelephone(String telephone) {
    this.telephone = telephone;
}


public String getAddress() {
    return address;
}


public void setAddress(String address) {
    this.address = address;
}


public String getCity() {
    return city;
}


public void setCity(String city) {
    this.city = city;
}


public String getCountry() {
    return country;
}


public void setCountry(String country) {
    this.country = country;
}


public String getPostcode() {
    return postcode;
}


public void setPostcode(String postcode) {
    this.postcode = postcode;
}


public String getPassword() {
    return password;
}


public void setPassword(String password) {
    this.password = password;
}


public boolean isEnabled() {
    return enabled;
}


public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}


public String getAuthority() {
    return authority;
}


public void setAuthority(String authority) {
    this.authority = authority;
}

}

Can anyone please help me to get user id from logged user

I have also tried using

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Users user =(Users)authentication.getPrincipal();

but it still did not work

Upvotes: 0

Views: 1732

Answers (1)

Kyle Anderson
Kyle Anderson

Reputation: 7051

The simplest approach would be to leverage the UserDetails and UserDetailsService interfaces.

Write a simple UserDetailsService:

@Service
public class CustomUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return findUserByUsername(username); //load the user from somewhere (e.g. Database)
    }
}

Have your Users class implement the UserDetails interface:

public class Users implements UserDetails {
    private String username;
    private String userId;
    private String password;
    private String role;

    public Users(String username, String userId, String password, String role) {
        this.username = username;
        this.userId = userId;
        this.password = password;
        this.role = role;
    }

    //...

}

Finally, when you call this static method you'll receive the Users object from which you can extract the userId:

Users user = (Users) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

Upvotes: 1

Related Questions