简体   繁体   English

Spring MVC从登录的用户获取用户ID

[英]Spring mvc getting user id from logged user

I am using spring security to login and logout, eveything works fine. 我正在使用Spring Security进行登录和注销,一切正常。

I can get username from logged user fine, however i need userID, 我可以从登录的用户名中获取用户名,但是我需要userID,

I would like to know how can i get user as an object from logged in user or how could i get userID 我想知道如何从登录用户中获取用户作为对象或如何获取用户ID

 @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 谁能帮我从登录的用户那里获取用户ID

I have also tried using 我也尝试使用

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

but it still did not work 但是它仍然没有用

The simplest approach would be to leverage the UserDetails and UserDetailsService interfaces. 最简单的方法是利用UserDetailsUserDetailsService接口。

Write a simple UserDetailsService: 编写一个简单的UserDetailsS​​ervice:

@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: 让您的Users类实现UserDetails接口:

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对象,可以从中提取userId:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM