处理安全性、错误、警告和日志记录
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本部分包含以下主题:
安全
数据源可以在以下两种访问模式下运行,如下所示:
-
在受限访问模式(默认)下,数据源仅处理与数据源所在网域相同的请求。受限模式可以防止跨站请求伪造 (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
并在数据 WebView 响应中设置错误。其中一个函数接受数据源请求,另一个函数接受 HttpServlet request
,用于 DataSourceRequest
创建失败的情况。定义功能和事件流中提供了一个实现示例。
如果无法返回数据表,该库会返回错误。如果可以返回数据表,但存在要报告的问题,该库将会返回警告以及数据表。例如,该库会在以下情况下创建警告:
- 查询可视化图表提供的
LIMIT
会导致数据被截断。
- 如果查询可视化在
FORMAT
子句中请求无效的格式模式,则会发生此错误。
如需添加您自己的警告,请创建一个 base.Warning
实例,并使用 addWarning()
方法将其添加到数据表中。
日志记录
该库使用 Jakarta Commons 日志记录。Jakarta Commons 日志记录功能都可以与您现有的大多数常见日志记录系统搭配使用。如果您的日志记录系统不是标准系统,则可能需要编写适配器。如需了解详情,请参阅 Jakarta Commons Logging 首页。
抛出异常时,系统会向日志发送相关信息。您访问日志的方式取决于您使用的日志记录系统。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-07-10。
[null,null,["最后更新时间 (UTC):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."]]