java程序错误_验证码,使用错误的验证码进行身份验证时发生异常

I'm using Firebase Phone Authentification to verify users accounts. When I try to authenticate with a wrong verification code, I get an IllegalArgumentException. Is there any way to fix that without using try catch blocks?

Here is the exception message :

java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, or temporaryProof.

at com.google.android.gms.common.internal.zzbq.checkArgument(Unknown Source)

at com.google.firebase.auth.PhoneAuthCredential.(Unknown Source)

at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source)

at com.example.myApp.testFragment$3$4.onClick(testFragment.java:316)

at android.view.View.performClick(View.java:5197)

at android.view.View$PerformClick.run(View.java:20926)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:145)

at android.app.ActivityThread.main(ActivityThread.java:5951)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

And here is the line where I get the error :

PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCode, inputCode);

I'm intentionally using wrong verification code in inputCode and hoping to get an error message instead of the exception.

解决方案Wraps phone number and verification information for authentication purposes.

So getCredential() cannot return an error message. Instead it throws an exception if there is a problem with the verification/credential information you provided.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于Java版本验证验证码生成的问题,我可以为您提供一些参考。 Java版本验证可以通过以下代码来实现: ``` String version = System.getProperty("java.version"); if (version.startsWith("1.8")) { // Java 8 版本 } else if (version.startsWith("1.7")) { // Java 7 版本 } else { // 其他版本 } ``` 验证码生成可以使用Java的随机数生成器和图片处理库来实现。以下是一个简单的验证码生成示例: ``` import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.util.Random; public class CaptchaGenerator { private static final Random RANDOM = new Random(); private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; private static final int WIDTH = 200; private static final int HEIGHT = 50; private static final int FONT_SIZE = 30; private static final int CHARACTERS_COUNT = 6; public static BufferedImage generateCaptcha() { BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); StringBuilder sb = new StringBuilder(); for (int i = 0; i < CHARACTERS_COUNT; i++) { char c = CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())); sb.append(c); g.setColor(new Color(RANDOM.nextInt(256), RANDOM.nextInt(256), RANDOM.nextInt(256))); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, FONT_SIZE)); g.drawString(String.valueOf(c), i * WIDTH / CHARACTERS_COUNT, HEIGHT / 2 + FONT_SIZE / 2 - RANDOM.nextInt(HEIGHT / 4)); } for (int i = 0; i < 20; i++) { g.setColor(new Color(RANDOM.nextInt(256), RANDOM.nextInt(256), RANDOM.nextInt(256))); g.drawLine(RANDOM.nextInt(WIDTH), RANDOM.nextInt(HEIGHT), RANDOM.nextInt(WIDTH), RANDOM.nextInt(HEIGHT)); } g.dispose(); return image; } public static String generateCaptchaText() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < CHARACTERS_COUNT; i++) { char c = CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())); sb.append(c); } return sb.toString(); } } ``` 这个示例中,我们使用Java的随机数生成器来生成验证码字符,并且使用Java的图片处理库来生成验证码图片。验证码图片的大小为200x50,字体大小为30,字符数量为6。同,我们还添加了一些干扰线来提高验证码的安全性。 希望这些代码能够对您有所帮助。如果您有其他问题,欢迎随向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值