BASE64Encoder上的访问限制警告

时间:2015-12-30 09:31:21

标签: java security base64

我正在加密一些文字,我使用了BASE64Encoder

String encryptedValue = new BASE64Encoder().encode(encVal);

但我收到了警告

The constructor 'BASE64Encoder()' is not API (restriction on required library 'C:\Program Files (x86)\Java\jdk1.8.0_45\jre\lib\rt.jar')
The method 'CharacterEncoder.encode(byte[])' is not API (restriction on required library 'C:\Program Files (x86)\Java\jdk1.8.0_45\jre\lib\rt.jar')
Access restriction: The type 'BASE64Encoder' is not API (restriction on required library 'C:\Program Files (x86)\Java\jdk1.8.0_45\jre\lib\rt.jar')

我可以@SuppressWarnings("restriction")禁止此警告。但我想知道为什么我会收到这个警告。它会在以后产生问题吗?

3 个答案:

答案 0 :(得分:2)

你真的应该避免调用sun.*类:

  

直接调用sun。*包的Java程序不是   保证可以在所有兼容Java的平台上运行。其实这样的   程序不保证即使在未来的版本中也能工作   平台。

Why Developers Should Not Write Programs That Call 'sun' Packages

其他库,如Apache Commons Codec将满足您的需求:

Commons Codec Base64

答案 1 :(得分:1)

你可能正在使用import sun.misc.BASE64Encoder;在1.8

中弃用

你应该使用1.8(https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html)上存在的新base64,或者你可以使用apache common codec lib(我使用的)

import org.apache.commons.codec.binary.Base64;

    String encryptedValue = new Base64().encodeToString(encVal);

答案 2 :(得分:0)

BASE64Encoder驻留在sun.misc包中。正如您使用的是1.8版本,即便是Oracle也不鼓励使用。请在此处阅读http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

该链接摘要: From one release to another, these classes may be removed, or they may be moved from one package to another, and it's fairly likely that their interface (method names and signatures) will change