首頁  >  文章  >  避免在 spring-boot 中向客戶端暴露後端詳細信息

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

王林
王林轉載
2024-02-22 12:28:06563瀏覽

近日,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刪除