我要在春季启动时休息一下。因此,我创建了User
实体和UserLoginDTO
进行登录。但似乎在运行时不起作用
我的DTO具有validation()
功能,用于验证用户名和密码。但这很简单:
public boolean validate(String username, String password) {
if (username == this.username && password == this.password) {
return true;
}
return false;
}
UserLoginDTO:
@Data
public class UserLoginDTO {
private String username;
private String password;
public boolean validate(String username, String password) {
if (username == this.username && password == this.password) {
return true;
}
return false;
}
}
用户:
@Entity
@Table(name = "users")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "username", nullable = false, unique = true)
private String username;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "joined_date", nullable = false, unique = true)
@CreatedDate
private Date joinedDate;
@Column(name = "password")
private String password;
@Column(name = "bio")
private String bio;
@Column(name = "email", nullable = false, unique = true)
private String email;
}
@GetMapping("/login")
public @ResponseBody String login(@RequestParam("username") String username, @RequestParam("password") String password) {
User user = repo.queryUserLogin(username, password);
UserLoginDTO dto = new UserLoginDTO();
dto.setUsername(user.getUsername());
dto.setPassword(user.getPassword());
if(dto.validate(username, password)) {
return "Login successfully!";
}
return "Login failed!";
}
但是当我运行代码并尝试从URL获取结果时(我使用邮递员)。但这不能正常工作:
答案 0 :(得分:2)
您应该使用equals
方法而不是==