java - Spring mvc从登录用户获取用户id

标签 java spring spring-mvc spring-security

我正在使用 Spring Security 来登录和注销,一切正常。

我可以从登录的用户那里获取用户名,但是我需要 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;
}

}

任何人都可以帮我从登录用户那里获取用户 ID

我也尝试过使用

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

但是还是不行

最佳答案

最简单的方法是利用 UserDetailsUserDetailsS​​ervice 接口(interface)。

编写一个简单的 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)
    }
}

让您的 Users 类实现 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;
    }

    //...

}

最后,当您调用此静态方法时,您将收到 Users 对象,您可以从中提取 userId:

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

关于java - Spring mvc从登录用户获取用户id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45461091/

相关文章:

java - Spring 警告 : Request method 'HEAD' not supported

java - 日历客户端忽略 ical4j 生成的 .ics 文件中的 FREEBUSY 属性

Java:18 个字符后的新行和 13 行后的新数组索引

java - 如何在Spring框架中实现UDP

java -/j_spring_security_check HTTP错误404

Spring 在集成测试中 Autowiring HttpServletRequest

java - 使用 Spring 进行 MongoDB 复制

java - 创建所有排列数组的排列算法

java - 正则表达式取代日志记录

java - 确保特定用户只能看到自己的用户详细信息 - 使用 Spring