登录DTO在Spring Controller中不起作用

时间:2019-02-14 09:48:09

标签: java spring spring-boot

我要在春季启动时休息一下。因此,我创建了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获取结果时(我使用邮递员)。但这不能正常工作:

postman error page

1 个答案:

答案 0 :(得分:2)

您应该使用equals方法而不是==

比较字符串

您可以在此处阅读有关https://javarevisited.blogspot.com/2012/12/difference-between-equals-method-and-equality-operator-java.html

的信息