spring security+rest控制器post方法不显示用户名

mv1qrgav  于 2021-09-30  发布在  Java
关注(0)|答案(1)|浏览(361)

我使用SpringBoot和SpringSecurity作为我的web应用程序的身份验证。我有一个控制器,它将authenticationrequest(pojo)参数作为@requestbody
通过以json格式传递用户名和密码来调用端点/从 Postman 处进行身份验证,但当我打印值(用户名和密码)时,我只能看到密码被打印出来。多次尝试后,无法找到用户名未填充的原因。

@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()));

}

波乔班

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
lnxxn5zx

lnxxn5zx1#

问题在于您的getter和setter:

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

字母n应该很小,因为json包含 username 与小n。试着这样做:

public String getUsername() {
    return username;
}

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

相关问题