首頁 > Java > 主體

避免在 spring-boot 中向客戶端暴露後端詳細信息

王林
發布: 2024-02-22 12:28:06
轉載
590 人瀏覽過

近日,php小編西瓜為您帶來了一篇關於Java方面的問答文章,重點討論瞭如何在spring-boot中避免向客戶端暴露後端詳細信息的問題。在開發過程中,如何處理異常訊息、錯誤提示等敏感訊息,避免洩露敏感數據,是開發者需要重點關注的問題之一。本文將為您解答這些疑惑,幫助您更好地保護應用程式的安全性。

問題內容

當遇到錯誤/不存在的 spring-boot 端點時。代碼層級的類別詳細資訊被公開。這可能會被標記為安全問題。

範例

請求

localhost:8500/api/1.0/service/../msc -> this is a bad formatted endpoint, which does not exist.
登入後複製

回應

{
    "timestamp": "2024-01-31t08:33:44.321+0000",
    "status": 400,
    "error": "bad request",
    "message": "failed to find lookuppath '/api/1.0/msc' within requesturi '/api/1.0/service/../msc'. this could be because the path has invalid encoded characters or isn't normalized.; nested exception is org.springframework.web.servlet.resource.resourceurlencodingfilter$lookuppathindexexception: failed to find lookuppath '/api/1.0/msc' within requesturi '/api/1.0/service/../msc'. this could be because the path has invalid encoded characters or isn't normalized.",
    "path": "/api/1.0/service/../msc"
}
登入後複製

只需查看錯誤訊息,我們就可以了解到有一個 spring-boot 應用程式在後台運行,由於訊息中公開了程式碼層級的詳細信息,因此它可能是一個漏洞。

我們如何向客戶端發送通用訊息而不是整個異常詳細資訊?

我也嘗試過使用@controlleradvice,但其中沒有捕獲異常。看起來這個問題甚至在到達控制器本身之前就已經出現了。

@ExceptionHandler(Exception.class)
public ResponseEntity handleException(Exception ex) {
log.error("Exception in flow", ex);
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error");
}
登入後複製

解決方法

您可以使用多種方法的組合來處理該異常並且不洩漏它,請參閱https://www.php.cn/link/41fa3925a7ec42ce029c43d6676e4b2c 以檢查不同類型的處理程序。

以上是避免在 spring-boot 中向客戶端暴露後端詳細信息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!