使用用户名或电子邮件地址登录spring boot webflux

使用用户名或电子邮件地址登录spring boot webflux,spring,cassandra,spring-webflux,project-reactor,Spring,Cassandra,Spring Webflux,Project Reactor,我是spring boot webflux的新手,正在使用Cassandra作为我的数据库。我不知道如何使用用户名或电子邮件登录。我有三张桌子 用户表 Create Table user ( userid bigint, username text, password text, name text, email text, phone text, birthday text, biography text, PRIMARY KEY (use

我是spring boot webflux的新手,正在使用Cassandra作为我的数据库。我不知道如何使用用户名或电子邮件登录。我有三张桌子

用户表

Create Table user (
   userid bigint,
   username text,
   password text,
   name text,
   email text,
   phone text,
   birthday text,
   biography text,
   PRIMARY KEY (userid)
)
public class User {
    //My Constructors
    public User(UserUsername userUsername) {
      System.out.println("Userid " + userUsername.getUserId());/I am getting the userId
      User user = new User();
      BeanUtils.copyProperties(userUsername, user);
      System.out.println("New Userid " + user.getUserId()); //I am getting the userId
    }

    public User(UserEmail userEmail) {
      System.out.println("Userid " + userEmail.getUserId()); /I am getting the userId
      User user = new User();
      BeanUtils.copyProperties(userEmail, user);
      System.out.println("New Userid " + user.getUserId()); /I am getting the userId
    }
}
用户名表

Create Table user (
   username text,
   userid bigint,
   password text,
   name text,
   email text,
   phone text,
   birthday text,
   biography text,
   PRIMARY KEY (username)
)
用户电子邮件表

Create Table user (
   email text,
   username text,
   userid bigint,
   password text,
   name text,
   phone text,
   birthday text,
   biography text,
   PRIMARY KEY (email)
)

@PostMapping("/signin")
public Mono<ResponseEntity<?>> login(@RequestBody AuthLoginRequest authLoginRequest) {
   return userService.findByUsernameOrEmail(authLoginRequest.getUsernameOrEmail()).map((user) -> {
      if(passwordEncoder.encode(authLoginRequest.getPassword()).equals(user.getPassword())) {
          return ResponseEntity.ok(new ApiResponse(tokenProvider.generateToken(user)));
      }else {
          return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
      }
   }.defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
}
我的帖子映射

这里的用户是空的

@PostMapping("/signin")
public Mono<ResponseEntity<?>> login(@RequestBody AuthLoginRequest authLoginRequest) {
   return userService.findByUsernameOrEmail(authLoginRequest.getUsernameOrEmail()).map((user) -> {
   if(passwordEncoder.encode(authLoginRequest.getPassword()).equals(user.getPassword())) {
          return ResponseEntity.ok(new ApiResponse(tokenProvider.generateToken(user)));
      }else {
          return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
      }
   }.defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
}
@PostMapping(/signin)

public Mono您需要查询
user\u username
表,如果有条目,请将其映射到
user
类并返回它。如果没有,您需要查询
user\u email
表,并将其映射到
UserUsername
类(如果存在条目)

最简单的方法是使用存储库:

public interface UserUsernameRepository extends ReactiveCassandraRepository<UserUsername, String> {
}


public interface UserEmailRepository extends ReactiveCassandraRepository<UserEmail, String> {
}
或在带有
BeanUtils
的维修方法图中:

public Mono<User> findByUsernameOrEmail(String usernameOrEmail) {
  return userUsernameRepository.findById(usernameOrEmail)
     .map(userUsername -> {
                System.out.println("Checking name "+userUsername.getName());// i printed out the name of the user
                User user = new User();
                BeanUtils.copyProperties(userUsername, user);
                return user;
            }).switchIfEmpty(userEmailRepository.findById(usernameOrEmail)
                .map(userEmail -> {
                        System.out.println("Checking name " +userEmail.getName());// i printed out the name of the user
                        User user = new User();
                        BeanUtils.copyProperties(userEmail, user);
                        return user;
                    }));
}
public Mono-findByUsernameOrEmail(字符串usernameOrEmail){
返回UsernameRepository.findById(usernameOrEmail)
.map(userUsername->{
System.out.println(“Checking name”+userUsername.getName());//我打印出了用户的名称
用户=新用户();
copyProperties(userUsername,user);
返回用户;
}).switchIfEmpty(userEmailRepository.findById(usernameOrEmail)
.map(用户电子邮件->{
System.out.println(“Checking name”+userEmail.getName());//我打印出了用户的名称
用户=新用户();
copyProperties(用户电子邮件、用户);
返回用户;
}));
}

谢谢你的回答。我照你说的做了,我查询了user_username和user_email表,检查是否有条目,它是否有效。我的问题是要将它们映射到用户表。如果有一个条目是我做的,但我从用户表中得到一个空的Mono,我需要获取保存的密码并将其与输入的密码匹配。我在用户类构造函数中使用了SpringBeanutils.copyProperties(源、目标)。调试反应式代码非常困难,所以很难说为什么会得到空mono。您确定表中有用于测试此代码的用户名或电子邮件的数据吗?能否在这两个map语句中添加log语句或System.out.print in findByUsernameOrEmail方法,并查看存储库方法返回的内容?请根据您的asnwer@ChristianIbanibo根据你的新代码添加到我的答案中。噩梦结束了。我很高兴谢谢你,我很感激。
public interface UserUsernameRepository extends ReactiveCassandraRepository<UserUsername, String> {
}


public interface UserEmailRepository extends ReactiveCassandraRepository<UserEmail, String> {
}
@Service
public class UserService() {

   @Autowired
   private UserUsernameRepository userUsernameRepository;

   @Autowired
   private UserEmailRepository userEmailRepository;

   public Mono<User> findByUsernameOrEmail(String usernameOrEmail) {
       return userUsernameRepository.findById(usernameOrEmail)
               .switchIfEmpty(userEmailRepository.findById(usernameOrEmail)
                   .map(userEmail -> new UserUsername(userEmail))) // or some other way to map properties to UserUsername class
                   .map(userUsername -> new User(userEmail)) // or some other way to map properties to wanted User class
       }
    }
public User(UserUsername userUsername) {
      System.out.println("Userid " + userUsername.getUserId()); //I am getting the userId
      this.email = userUsername.getEmail();
      this.username = userUsername.getUsername();
      ...
}
public Mono<User> findByUsernameOrEmail(String usernameOrEmail) {
  return userUsernameRepository.findById(usernameOrEmail)
     .map(userUsername -> {
                System.out.println("Checking name "+userUsername.getName());// i printed out the name of the user
                User user = new User();
                BeanUtils.copyProperties(userUsername, user);
                return user;
            }).switchIfEmpty(userEmailRepository.findById(usernameOrEmail)
                .map(userEmail -> {
                        System.out.println("Checking name " +userEmail.getName());// i printed out the name of the user
                        User user = new User();
                        BeanUtils.copyProperties(userEmail, user);
                        return user;
                    }));
}