SonarLint | Weak SSL/TLS protocols should not be used (java:S4423)

這個問題會發生在使用SSLContext.getInstance傳入一個安全性較弱的protocol種類,以我們的案例來說,我們使用了SSL:

final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

這個問題也屬於find-sec-bug中的SSL_CONTEXT pattern的SSLContext needs to be compatible with TLS 1.2。在SonarLint中,建議將Protocol設定為TLSv1.2,而spotBugs建議為TLS;由於JDK會自己對應TLS的預設值,所以我們選擇設定TLS,在JDK8中預設值為TLSv1.2:

final SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());