我已经使用 TOTP 和 Java 中的 QR 代码实现了 2 因素身份验证,这在 Android 上运行得很好。然而,当我尝试在任何 iOS 设备上扫描二维码时,我遇到了一个非常奇怪的错误,并且到目前为止我还无法找到其原因。
看来该问题可能是由于编码不当造成的,iOS 版本的 Google Authenticator 应用不允许这种情况,但这只是猜测。
我收到的错误消息是:
the barcode is not a valid authentication token barcode
这是代码
public String generateQRUrl() {
String secret = "hXYmnYsVWoEAXjKuAZcj";
String QR_PREFIX = "https://chart.googleapis.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=";
try {
return QR_PREFIX + URLEncoder.encode(
String.format("otpauth://totp/%s:%s?secret=%s&issuer=%s",
"Test X", "test@gmail.com", secret, "Issuer Y"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
我已经为 URLEncoder 尝试了多种不同的组合,但到目前为止,没有一个有效。在我看来,iOS 应用程序不喜欢某种特殊字符,正如此 C# 线程 ( click ) 中所建议的那样,但有人知道如何解决此问题吗?
谢谢!
最佳答案
生成的 QR 码和 Google 的 iOS Authenticator 应用程序也存在同样的问题。双重编码为我解决了这个问题:
@Test
void useUriUtils() throws UnsupportedEncodingException {
String encodedString = UriUtils.encodeFragment("HELLO WORLD", "UTF-8");
assertEquals("HELLO%20WORLD", encodedString);
String doubleEncoded = UriUtils.encodeFragment(encodedString, "UTF-8");
assertEquals("HELLO%2520WORLD", doubleEncoded);
}
或者作为 URL 示例,而不是将字符串 HELLO WORLD
中的空格编码为 HELLO%20WORLD
,如以下 URL 所示:
https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=otpauth://totp/HELLO%20WORLD%3Aalexander.pietsch%40foo.com%3Fsecret%3DJFWMPPNUIPG7CAOL%26issuer%3DHELLO%20WORLD
将其双重编码为HELLO%2520WORLD
:
https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=otpauth://totp/HELLO%2520WORLD%3Aalexander.pietsch%4040foo.com%3Fsecret%3DYLCDNEHV5MSIOI5A%26issuer%3DHELLO%2520WORLD
最后一个将被 iOS Google Authenticator 应用程序接受。
关于java - TOTP 二维码适用于 Android 但不适用于 iOS 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51113066/