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

drkbr07n  于 2021-06-10  发布在  Cassandra
关注(0)|答案(1)|浏览(425)

我对springbootwebflux是个新手,我使用cassandra作为我的数据库。我不知道如何用用户名或电子邮件登录。我有三张table
用户表

Create Table user (
   userid bigint,
   username text,
   password text,
   name text,
   email text,
   phone text,
   birthday text,
   biography text,
   PRIMARY KEY (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());
}

这就是问题所在,我将如何返回用户mono。如果他们使用用户名登录,我必须查询user\u username表,或者如果他们使用电子邮件地址登录,我必须查询user\u email表。

@Service
public class UserService() {
   public Mono<User> findByUsernameOrEmail(String usernameOrEmail) {

   }
}

我需要压缩user\u username和user\u email类吗?请我需要一个解决方案,我还没有看到任何有关这个问题。我需要一个有效的解决方案。
根据你的回答@nikolab,我编辑了这个问题来展示我到目前为止所做的一切

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
                return new User(userUsername);
            }).switchIfEmpty(userEmailRepository.findById(usernameOrEmail)
                .map(userEmail -> {
                        System.out.println("Checking name " +userEmail.getName());// i printed out the name of the user
                        return new User(userEmail);
                    }));
}

一切正常。。。
我的用户表

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
    }
}

我的帖子Map
这里的用户是空的

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

x33g5p2x1#

您需要查询 user_username 表,如果有条目,则将其Map到 User 上课并归还。如果没有,您需要查询 user_email 表并将其Map到 UserUsername 如果存在条目,则初始化。
最简单的方法是使用存储库:

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
       }
    }

如果两个查询都没有返回结果(空),那么服务方法将返回 Mono.empty() 这正是你需要的。
请注意,必须在中实现属性Map new UserUsername 以及 new UserUsername 施工人员。
编辑
好吧,我想我知道问题出在哪里了,在用户构造函数中,你正在创建一个新的 User 示例和Map UserUsername/UserEmail 示例属性 User 示例,而事实上该示例未绑定到构造函数返回的示例。在这些构造函数中手动设置字段,如下所示:

public User(UserUsername userUsername) {
      System.out.println("Userid " + userUsername.getUserId()); //I am getting the userId
      this.email = userUsername.getEmail();
      this.username = userUsername.getUsername();
      ...
}

或者在服务方法Map中 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;
                    }));
}

相关问题