การจัดการความปลอดภัย ข้อผิดพลาด คำเตือน และการบันทึก
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ส่วนนี้จะครอบคลุมถึงหัวข้อต่อไปนี้:
ความปลอดภัย
แหล่งข้อมูลทำงานได้ในโหมดการเข้าถึง 1 ใน 2 โหมดดังต่อไปนี้
-
ในโหมดการเข้าถึงที่จำกัดซึ่งเป็นค่าเริ่มต้น แหล่งข้อมูลจะแสดงเฉพาะคำขอที่มาจากโดเมนเดียวกันกับแหล่งที่มาของข้อมูล โหมดที่จำกัดจะป้องกันการโจมตีด้วยการปลอมแปลงคำขอข้ามเว็บไซต์ (XSRF) จึงมีความปลอดภัยมากกว่าโหมดการเข้าถึงแบบไม่จำกัด เนื่องจากไลบรารีแหล่งข้อมูลมีอินเทอร์เฟซสำหรับการส่งคืนข้อมูลเท่านั้น และไม่ได้มีไว้สำหรับเปลี่ยนสถานะหรือข้อมูลฝั่งเซิร์ฟเวอร์ จึงเป็นไปได้เฉพาะการโจมตี XSRF ที่พยายามขโมยข้อมูล คุณต้องใช้โหมดที่จำกัดร่วมกับการตรวจสอบสิทธิ์ที่ใช้คุกกี้เพื่อให้แหล่งข้อมูลปลอดภัยจากการพยายามขโมยข้อมูล
วิธีตรวจสอบสิทธิ์ผู้ใช้จะขึ้นอยู่กับสภาพแวดล้อมและการติดตั้งใช้งาน
-
ในโหมดการเข้าถึงที่ไม่จำกัด แหล่งข้อมูลจะให้บริการคำขอทั้งหมด
ไม่ว่าจะมาจากแหล่งใด แหล่งข้อมูลที่ทำงานในโหมดที่ไม่จำกัดจะได้รับการปกป้องด้วยการตรวจสอบสิทธิ์ที่ใช้คุกกี้ แต่โปรดทราบว่าแหล่งข้อมูลจะมีช่องโหว่ในการโจมตี XSRF ใช้โหมดที่ไม่จำกัดหากการแสดงภาพบนหน้าเว็บนอกโดเมนของแหล่งข้อมูลจำเป็นต้องเข้าถึงแหล่งข้อมูล หรือหากข้อมูลอยู่ในโดเมนสาธารณะ จึงไม่จำเป็นต้องได้รับการปกป้อง
คำขอการแสดงภาพสามารถระบุรูปแบบการตอบสนอง
ของ JSON, CSV หรือ HTML ได้ รูปแบบการตอบกลับจะกำหนดรูปแบบที่แหล่งข้อมูลแสดงตารางข้อมูล เนื่องจากรูปแบบ CSV และ HTML ไม่มีช่องโหว่ต่อการโจมตี XSRF ไฟล์เหล่านี้จะสามารถเข้าถึงได้จากโดเมนอื่น แม้ว่าจะอยู่ในโหมดจำกัดก็ตาม
หากต้องการระบุโหมดที่ไม่จำกัด ให้ลบล้าง isRestrictedAccessMode()
ดังนี้
@Override
protected boolean isRestrictedAccessMode() {
return false;
}
ตัวอย่างทั้งหมดที่มาพร้อมกับไลบรารีจะทำงานในโหมดการเข้าถึงที่ไม่จำกัดเพื่อความง่าย
ข้อผิดพลาดและคำเตือน
เมื่อไม่สามารถทำได้หรือต้องการส่งคืนตารางข้อมูลที่ถูกต้อง
ไลบรารีจะส่ง DataSourceException
เช่น ในกรณีที่ตรวจสอบสิทธิ์ผู้ใช้ไม่ได้ ไลบรารีจะให้ข้อยกเว้นเหล่านี้
เมื่อข้อผิดพลาดป้องกันไม่ให้สร้างตารางข้อมูล คุณอาจต้องระบุข้อยกเว้นในสถานการณ์เฉพาะตัวสำหรับแหล่งข้อมูลของคุณ หากเป็นเช่นนั้น ให้สร้างประเภทข้อยกเว้นข้อผิดพลาดของคุณเองโดยรับค่าจากคลาส DataSourceException
คุณยังสามารถย้ายชั้นเรียน DataSourceException
โดยตรงได้ด้วย
คลาส DataSourceException
อยู่ในแพ็กเกจ base
โดยใช้พารามิเตอร์ต่อไปนี้
ReasonType
ต้องระบุพารามิเตอร์นี้ ประเภทเหตุผลที่ใช้ได้จะกำหนดไว้ใน enum ReasonType
หากไม่มีประเภทเหตุผลที่เหมาะสม คุณสามารถใช้ Other
หรือ Internal
ได้
MessageToUser
พารามิเตอร์นี้จะกำหนดข้อความแสดงข้อผิดพลาด
ในกรณีส่วนใหญ่ ข้อมูลนี้จะแสดงแก่ผู้ใช้เป็นเคล็ดลับเครื่องมือ จึงไม่ควรใส่ข้อมูลทางเทคนิคหรือข้อมูลลับ
คุณใช้ชุดฟังก์ชันตัวช่วยใน datasource.DataSourceHelper
เพื่อจัดการข้อผิดพลาดได้ ในกรณีนี้ ให้เรียกใช้ฟังก์ชัน 2 รายการซึ่งมีชื่อเดียวกันคือ setErrorServletResponse
เพื่อใช้ DataSourceException
และตั้งค่าข้อผิดพลาดในการตอบกลับของเซิร์ฟเล็ตข้อมูล ฟังก์ชันหนึ่งเหล่านี้ต้องใช้คำขอแหล่งข้อมูล ส่วนอีกฟังก์ชันหนึ่งจะใช้ HttpServlet request
และจะใช้ในกรณีที่สร้าง DataSourceRequest
ไม่สำเร็จ ตัวอย่างการใช้งานจะอยู่ในการกำหนดความสามารถและโฟลว์ของเหตุการณ์
หากแสดงผลตารางข้อมูลไม่ได้ ไลบรารีจะแสดงข้อผิดพลาด หากส่งคืนตารางข้อมูลได้ แต่มีปัญหาในการรายงาน ไลบรารีจะแสดงคำเตือนพร้อมกับตารางข้อมูล
ตัวอย่างเช่น ไลบรารีจะสร้างคำเตือนในสถานการณ์ต่อไปนี้
- หากการแสดงภาพของคำค้นหาให้
LIMIT
ซึ่งส่งผลให้เกิดข้อมูลที่ถูกตัด
- หากการแสดงภาพในการค้นหาขอรูปแบบการจัดรูปแบบที่ไม่ถูกต้องในอนุประโยค
FORMAT
หากต้องการเพิ่มคำเตือนของคุณเอง ให้สร้างอินสแตนซ์ของ base.Warning
และเพิ่มลงในตารางข้อมูลโดยใช้เมธอด addWarning()
การบันทึก
ห้องสมุดนี้ใช้การบันทึกคอมมอนส์จาการ์ตา การบันทึกของ Jakarta Commons นั้นใช้ได้กับระบบการบันทึกทั่วไปที่คุณน่าจะมีอยู่แล้ว คุณอาจต้องเขียนอะแดปเตอร์หากระบบการบันทึกของคุณไม่เป็นไปตามมาตรฐาน
สำหรับรายละเอียดเพิ่มเติม โปรดดูที่หน้าแรก
การบันทึกของจาร์กาตา
เมื่อมีการส่งข้อมูลข้อยกเว้นไปยังบันทึก วิธีเข้าถึงบันทึกจะขึ้นอยู่กับระบบการบันทึกที่ใช้
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-07-10 UTC
[null,null,["อัปเดตล่าสุด 2024-07-10 UTC"],[[["\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."]]