Base64: java.lang.IllegalArgumentException: Illegal character

后端 未结 5 1932
醉梦人生
醉梦人生 2020-11-30 11:09

I\'m trying to send a confirmation email after user registration. I\'m using the JavaMail library for this purpose and the Java 8 Base64 util class.

I\'m encoding us

5条回答
  •  天涯浪人
    2020-11-30 11:41

    Just use the below code to resolve this:

    JsonObject obj = Json.createReader(new ByteArrayInputStream(Base64.getDecoder().decode(accessToken.split("\\.")[1].
                            replace('-', '+').replace('_', '/')))).readObject();
    

    In the above code replace('-', '+').replace('_', '/') did the job. For more details see the https://jwt.io/js/jwt.js. I understood the problem from the part of the code got from that link:

    function url_base64_decode(str) {
      var output = str.replace(/-/g, '+').replace(/_/g, '/');
      switch (output.length % 4) {
        case 0:
          break;
        case 2:
          output += '==';
          break;
        case 3:
          output += '=';
          break;
        default:
          throw 'Illegal base64url string!';
      }
      var result = window.atob(output); //polifyll https://github.com/davidchambers/Base64.js
      try{
        return decodeURIComponent(escape(result));
      } catch (err) {
        return result;
      }
    }
    

提交回复
热议问题