處理安全性、錯誤、警告和記錄
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本節包含下列主題:
安全性
資料來源可在以下兩種存取模式中運作:
-
在限制存取模式 (根據預設) 中,資料來源只會處理資料來源與資料來源所在網域發出的要求。受限模式可防止跨網站偽造要求 (XSRF) 攻擊,因此比無限制存取模式更加安全。由於資料來源程式庫所提供的介面只能傳回資料,無法變更伺服器端的狀態或資料,因此只能採用試圖竊取資料的 XSRF 攻擊。為了防止資料來源遭到竊取,受限制模式必須與 Cookie 型驗證搭配使用。驗證使用者的方式取決於您的環境和實作方式。
-
在無限制存取模式下,無論來源為何,資料來源都會處理所有要求。在無限制模式下執行的資料來源,可能受到 Cookie 型驗證的保護,但請注意,資料來源將容易遭受 XSRF 攻擊。如果圖表位於資料來源網域外的網頁需要存取資料來源,或是資料位於公有領域而不需要保護,請使用無限制模式。
視覺化要求可指定 JSON、CSV 或 HTML 的回應格式。回應格式會決定資料來源傳回資料表的格式。由於 CSV 和 HTML 格式不受 XSRF 攻擊,因此即使在受限模式下,仍可從其他網域存取。
如要指定無限制模式,請按照下列方式覆寫 isRestrictedAccessMode()
:
@Override
protected boolean isRestrictedAccessMode() {
return false;
}
為求簡單起見,程式庫提供的所有範例都會以無限制存取模式執行。
錯誤和警告
如果無法或希望傳回有效的資料表,
程式庫會擲回 DataSourceException
。例如,無法驗證使用者。當程式庫發生錯誤,因而無法建立資料表時,就會擲回這些例外狀況。因此,建議您在資料來源特有的情況下擲回例外狀況。如果是的話,請沿用 DataSourceException
類別,自行建立錯誤例外狀況類型。您也可以直接擲回 DataSourceException
類別。
DataSourceException
類別位於 base
套件中,該類別會採用下列參數:
ReasonType
此為必要參數。可用原因類型已在 ReasonType
列舉中定義。如果所有可用原因類型都不適合,您可以使用 Other
或 Internal
。
MessageToUser
這個參數定義錯誤訊息的文字。在大多數情況下,都會以工具提示的形式向使用者顯示,因此請勿加入技術或機密資訊。
您可以使用 datasource.DataSourceHelper
中的一組輔助函式來處理錯誤。在此情況下,呼叫兩個名稱相同的 setErrorServletResponse
的函式以採用 DataSourceException
,並設定資料 ++ 回應的錯誤。其中一個函式接受資料來源要求,另一個函式會採用 HttpServlet request
,如果無法建立 DataSourceRequest
,就會使用這個函式。如需實作範例,請參閱定義功能和事件流程。
如果無法傳回資料表,程式庫會傳回錯誤。如果可以傳回資料表,但回報有問題,程式庫就會連同資料表傳回警告。舉例來說,程式庫會在下列情況建立警告:
- 表示查詢視覺化提供的
LIMIT
導致資料遭到截斷。
- 表示查詢視覺化呈現在
FORMAT
子句中要求無效的格式模式。
如要自行新增警告,請建立 base.Warning
的例項,並使用 addWarning()
方法將其新增至資料表。
記錄
程式庫會使用雅加達常用記錄功能。雅加達通用記錄可以用於您可能已有的常見記錄系統。如果您的記錄系統非標準,您可能需要編寫轉接器。詳情請參閱 Jakarta 常用記錄首頁。
擲回例外狀況的資訊會傳送至記錄。存取記錄的方式取決於您使用的記錄系統。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-07-10 (世界標準時間)。
[null,null,["上次更新時間:2024-07-10 (世界標準時間)。"],[[["\u003cp\u003eThis documentation provides information about security considerations, error handling, and logging within the data source library.\u003c/p\u003e\n"],["\u003cp\u003eData sources operate in restricted access mode by default to prevent XSRF attacks, but unrestricted mode can be enabled for cross-domain access.\u003c/p\u003e\n"],["\u003cp\u003eThe library uses \u003ccode\u003eDataSourceException\u003c/code\u003e for errors, with helper functions available to handle these errors in servlet responses.\u003c/p\u003e\n"],["\u003cp\u003eWarnings are used to indicate problems without preventing data table return, like truncated data due to limits or invalid formatting.\u003c/p\u003e\n"],["\u003cp\u003eLogging is handled through Jakarta commons logging, which can be adapted to various logging systems.\u003c/p\u003e\n"]]],[],null,["# Handling Security, Errors, Warnings, and Logging\n\nThis section covers the following topics:\n\n- [Security](#access)\n- [Errors and Warnings](#error)\n- [Logging](#logging)\n\nSecurity\n--------\n\nA data source can operate in one of two access modes as follows:\n\n- In\n restricted access mode, which is the default, a data source serves only\n those requests that originate from the same domain as that in which the\n data source is located. Restricted mode prevents [cross-site\n request forgery](http://en.wikipedia.org/wiki/Cross-site_request_forgery) (XSRF) attacks and so is more secure than unrestricted\n access mode. Because the data source library provides an interface for\n returning data only, and not for changing state or data on the server side,\n only XSRF attacks\n that attempt to steal data are possible. To\n make your data source secure against attempts to steal data, restricted\n mode must be used in conjunction with cookie-based authentication.\n The way that you authenticate users depends on your environment and implementation.\n\n- In unrestricted access mode, a data source serves all requests\n regardless of their origin. A data source that runs in unrestricted mode\n can be protected by cookie-based authentication, but note that\n the data source will be vulnerable to\n XSRF attacks. Use unrestricted mode if visualizations on web pages outside the data source's\n domain need to access the data source, or if the data is in the public\n domain and so does not need to be protected.\n\nA visualization request can specify a response\nformat of JSON, CSV, or HTML. The response format determines the format\nin which a data source returns a data table. Because CSV and HTML formats\nare not vulnerable to XSRF attacks, these can be accessed from other domains,\neven in restricted mode.\n\nTo specify unrestricted mode, override `isRestrictedAccessMode()` as\nfollows: \n\n```transact-sql\n @Override\n protected boolean isRestrictedAccessMode() {\n return false;\n }\n```\n\nFor simplicity, all the examples provided with the library run in unrestricted\naccess mode.\n\nErrors and Warnings\n-------------------\n\nWhen it is not possible, or desirable, to return a valid data table, the\nlibrary throws a `DataSourceException`. For example if\nthe user cannot be authenticated. The library throws these exceptions\nwhen errors prevent it from creating a data table. You may want to throw\nexceptions in situations unique to your data source. If so,\ncreate your own error exception types by inheriting from the `DataSourceException`\nclass. You can also throw the `DataSourceException` class\ndirectly.\n\nThe `DataSourceException` class\nis located in the `base` package, it takes the following parameters: \n\n- `ReasonType` \n This parameter is mandatory. Available reason types are defined in the `ReasonType` enum. If none of the available reason types are suitable, you can use `Other` or `Internal`. \n- `MessageToUser` \n This parameter defines the text of the error message. In most cases, it is displayed to the user as a tooltip, so it is important not to include technical or confidential information.\n\nYou can use the set of helper functions\nin `datasource.DataSourceHelper` to handle\nerrors. In this case call two functions both with same name of `setErrorServletResponse` to\ntake a `DataSourceException` and set an error on the data\nservlet response. One of these functions takes a data source request, the\nother takes an `HttpServlet request` and is used in cases\nwhere there is a failure to create a `DataSourceRequest`. An\nexample implementation is provided in [Defining Capabilities and the Flow of Events](/chart/interactive/docs/dev/dsl_httpservlet).\n\nIf it is not possible to return a data table, the library returns an\nerror. If it is possible to return a data table, but there is a problem\nto report, the library returns a warning together with the data table.\nFor example, the library\ncreates a warning in the following situations:\n\n- if a querying visualization provides a `LIMIT` that results in truncated data.\n- if a querying visualization requests an invalid formatting pattern in a `FORMAT` clause.\n\nTo add your own warning,\ncreate an instance of `base.Warning` and add it to your\ndata table using the `addWarning()` method.\n\nLogging\n-------\n\nThe library uses Jakarta commons logging. Jakarta commons logging can\nbe used with most common logging systems that you might already have in\nplace. You might need to write an adapter if your logging system is non-standard.\nFor more details, see the [Jakarta\ncommons logging home page](http://commons.apache.org/logging/).\n\nWhen an exception is thrown information is sent to the log. The way\nthat you access the log depends on the logging system you use."]]