java - TOTP 二维码适用于 Android 但不适用于 iOS 设备?

标签 java spring-mvc spring-boot qr-code google-authenticator

我已经使用 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/

相关文章:

java - Tomcat 运行 Twitter 流监控应用程序?

java - Maven原型(prototype):create-from-project does not include other language folder

java - spring mvc 绑定(bind)/验证

java - 如何在 Spring Boot JUnit 测试中排除 *AutoConfiguration 类?

java - 在这种情况下,为什么我会收到错误“"Neither BindingResult nor plain target object for bean name ' user' available as request attribute”?

java - 连接多个文件

java - 如何使用@Async 注释在方法中获取 hibernate session

java - 如何使用 Spring 的 AnnotationMethodHandlerAdapter 实现发布后重定向表单?

java - 在spring boot中进行单元测试之前,通过data.sql文件在h2数据库中插入数据

firebase - Firestore 和 Spring Boot