【问题标题】:How to set a data for a particular row in springboot如何在spring boot中为特定行设置数据
【发布时间】:2022-08-09 18:10:19
【问题描述】:

我创建了一个登录页面,我正在获取用户电子邮件 ID,然后我正在生成一个 otp 并发送给用户邮件。现在我想通过使用他的 mailid 将数据库中生成的 otp 保存到特定用户。但问题是当我将 otp 保存在数据库中时,它没有存储在给定的 mailid 中,我无法弄清楚。

我的代码:

SendOtp 代码:

public String sendOtp(@RequestParam(\"email\") String email) {
        
    //  int otp = random.nextInt(999999);

        String otp = RandomString.make(8);
        
        String subject = \"OTP from session-handling-proj By Harshit\";
        String toEmail = email;
        String body = \"OTP :  \" + otp;
        
             
        SessionHandling sessionHandling = new SessionHandling();
        sessionHandling.setOneTimePassword(otp);
        repo.save(sessionHandling);
        
           this.emailService.sendMail(toEmail, subject, body);
          return (\"success\");

数据库:

Id | password | emailid             | name    | Cpass      |  otp              | time

8 | 123        | shy@gmail.com      | shymm   | 123        | NULL              | NULL               
9 | NULL       | NULL               | NULL    | NULL       | Wi0ioiuZ          | NULL   
10 | NULL       | NULL               | NULL    | NULL       | R98RT1OZ          | NULL


当我尝试存储 otp 时,它创建了新行,即 9 和 10。

  • 你认为new SessionHandling() 和随后的repo.save(sessionHandling); 会做什么?您认为存储库如何知道它应该更新特定记录而不是添加新记录? - 您可以做的一件事是首先通过邮件 ID 读取记录,然后添加 OTP 并再次保存。或者使用更新查询,这样您就不必阅读了。
  • 就是这样,当您创建一个新对象(新SessionHandling )时,会创建一个新行。您使用 JPA 存储库吗?
  • 顺便说一句,您可能出于学习目的而这样做,所以请保持简单。但是在生产环境中这样做时,您会在该设计中遇到一个很大的安全漏洞:OTP(和任何其他密码)应该绝不以纯文本形式存储,而是进行散列和加盐处理,以便只有用户知道。
  • @Jedupont 是的,我做了 Repository 类
  • @Thomas 现在出于学习目的,我直接存储密码。

标签: java spring spring-boot


【解决方案1】:

正如@Thomas 已经解释的那样,您应该始终在生产中散列您的密码。

但是,这里是使用 JPA 存储库更新 otp 的示例:

@Repository
public interface SessionHandlingRepository extends JpaRepository<SessionHandling, Long> {
    @Transactional
    @Modifying
    @Query(value = "UPDATE SessionHandling s SET s.otp = :otpCode WHERE s.id = :id")
    void updateOtp(@Param("id") Long whereId, @Param("otpCode") String otp);
}

【讨论】:

    【解决方案2】:

    你有2个案例:情况1: 你是只保存一个邮件ID记录还是每次登录操作可能有很多记录。 根据您的业务将创建查询
    如果只有一条记录: 并且您确定电子邮件存在于 Db 中,请使用此代码

    @Repository
    public interface SessionHandlingRepository extends JpaRepository<SessionHandling, Long> {
        @Transactional
        @Modifying
        @Query(value = "UPDATE SessionHandling s SET s.otp = :otpCode WHERE s.emailid= :emailId")
        void updateOtp(@Param("emailId") String mailId, @Param("otpCode") String otp);
    }
    

    如果您不确定,

    因此,您必须为所有详细信息保存新记录

    案例2:邮件允许的许多记录: 因此,您还必须为电子邮件和新 Otp 的 SessionHandling 创建新记录

    【讨论】:

      【解决方案3】:

      首先,您必须将数据存储在与该特定 emailId 相关的会话处理类中,然后设置 otp 并保存它。

      使用方法制作存储库类findByEmailID()在里面。

      xyzrepo.class

      @Query(value = "Select * from sessionhandling where emailId=?1",nativeQuery = true)
      SessionHandling findByEmailId(String emailId);
      

      你的方法 SendOtp 看起来像:

      //Autowired xyzrepo
      
      public String sendOtp(@RequestParam("email") String email) {
              
          //  int otp = random.nextInt(999999);
      
              String otp = RandomString.make(8);
              
              String subject = "OTP from session-handling-proj By Harshit";
              String toEmail = email;
              String body = "OTP :  " + otp;
              
                   
              SessionHandling sessionHandling = xyzrepo.findByEmailId(email);
              sessionHandling.setOneTimePassword(otp);
              repo.save(sessionHandling);
              
                 this.emailService.sendMail(toEmail, subject, body);
                return ("success");
      

      【讨论】:

        猜你喜欢
        • 2016-05-03
        • 2021-05-12
        • 1970-01-01
        • 1970-01-01
        • 2020-03-18
        • 1970-01-01
        • 2014-11-01
        • 2015-10-15
        • 2017-10-28
        相关资源
        最近更新 更多