Spring 安全 + REST 控制器 Post 方法不显示用户名

Spring Security + REST controller Post method not displaying username

我正在使用 spring 引导和 spring 安全性作为我的 Web 应用程序的身份验证。 我有一个控制器,它将 authenticationRequest(POJO) 参数作为@RequestBody

通过以 JSON 格式传递用户名和密码来调用端点/从邮递员进行身份验证 但是当我打印值(用户名和密码)时,我只能看到打印的密码。经过多次尝试找不到用户名未填充的原因。

@RequestMapping(value="/authenticate",method=RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody AuthenticationRequest authenticationRequest) throws Exception{


    System.out.println("authenticationRequest.getUserName()"+authenticationRequest.getUserName());
    System.out.println("authenticationRequest.getPassword()"+authenticationRequest.getPassword());

    authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUserName(), authenticationRequest.getPassword()));

        
}

POJO Class

public class AuthenticationRequest {
    
private String username;
private String password;

public AuthenticationRequest(){
    
}
public AuthenticationRequest(String username, String password) {
    super();
    this.username = username;
    this.password = password;
}
public String getUserName() {
    return username;
}
public void setUserName(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

}

JSON(原始) { "用户名":"用户", “密码”:“通过” }

控制台

authenticationRequest.getUserName()null
authenticationRequest.getPassword()pass

问题出在您的 getter 和 setter:

public String getUserName() {
    return username;
}
public void setUserName(String username) {
    this.username = username;
}

字母 n 应该小,因为您的 JSON 包含小 n 的 username。像这样尝试:

public String getUsername() {
    return username;
}

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