忘记密码(找回密码)代码实现

说到忘记密码,这种事情我们在生活中经常遇到,毕竟我们不是机器,总有忘的时候。

回想忘记密码(找回密码)的流程一般分如下几个步骤:

1.输入账号(手机号)

2.点击发送验证码,过一会手机就会收到短信验证码

3.填写验证码

4.验证成功,并设置新的登录密码

5.最后恭喜成功”找回密码“

当然这些过程中还需要做一些验证:(1)如账号是否已注册或是否已存在,包括手机格式校验(2)验证码是否已过期(3)输入的验证码和短信验证码是否匹配 (4)设置新密码时,密码格式的校验。

下面展示下我项目中遇到的”忘记密码“具体业务代码:


由于具体业务需求不一,我们的账号密码是每隔3个月就要重置下,否则你就需要走“忘记密码”这个业务喽,在设置新密码的同时,需要将密码过期时间字段修改下(当前系统时间后延3个月),并将密码是否启用标志更新为“可用”状态。最后返回成功信息即可。


  • 7
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是一个简单的示例代码,展示了如何在 Android 应用程序中实现忘记密码功能: 1. 在布局文件中添加一个“忘记密码”按钮。 activity_login.xml: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/edit_text_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名"/> <EditText android:id="@+id/edit_text_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码"/> <Button android:id="@+id/button_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> <Button android:id="@+id/button_forgot_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="忘记密码"/> </LinearLayout> ``` 2. 在 LoginActivity 中处理“忘记密码”按钮的点击事件,跳转到密码重置页面。 LoginActivity.java: ```java public class LoginActivity extends AppCompatActivity { private EditText mEditTextUsername; private EditText mEditTextPassword; private Button mButtonLogin; private Button mButtonForgotPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mEditTextUsername = findViewById(R.id.edit_text_username); mEditTextPassword = findViewById(R.id.edit_text_password); mButtonLogin = findViewById(R.id.button_login); mButtonForgotPassword = findViewById(R.id.button_forgot_password); mButtonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 处理登录逻辑 } }); mButtonForgotPassword.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class)); } }); } } ``` 3. 在 ResetPasswordActivity 中让用户输入其注册时使用的邮箱地址,并添加一个“发送重置链接”的按钮。 activity_reset_password.xml: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/edit_text_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="邮箱地址"/> <Button android:id="@+id/button_send_reset_link" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送重置链接"/> </LinearLayout> ``` ResetPasswordActivity.java: ```java public class ResetPasswordActivity extends AppCompatActivity { private EditText mEditTextEmail; private Button mButtonSendResetLink; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reset_password); mEditTextEmail = findViewById(R.id.edit_text_email); mButtonSendResetLink = findViewById(R.id.button_send_reset_link); mButtonSendResetLink.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 发送重置邮件的逻辑 } }); } } ``` 4. 在发送重置邮件的逻辑中,使用 JavaMail API 发送包含重置密码链接的电子邮件。 ```java public void sendResetEmail(String emailAddress) throws MessagingException { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your_email_address", "your_email_password"); } }); String resetLink = "https://www.example.com/reset_password?email=" + emailAddress; Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your_email_address")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress)); message.setSubject("密码重置链接"); message.setText("请点击以下链接重置您的密码:" + resetLink); Transport.send(message); } ``` 在实际应用中,需要根据具体需求进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纯洁的一笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值