{"id":2401,"date":"2025-10-19T16:05:08","date_gmt":"2025-10-19T08:05:08","guid":{"rendered":"https:\/\/xbtzone.com\/blog\/?p=2401"},"modified":"2025-10-19T16:10:02","modified_gmt":"2025-10-19T08:10:02","slug":"how-to-implement-springboot-druid-sqlserver-entra-id-authentication","status":"publish","type":"post","link":"https:\/\/xbtzone.com\/blog\/how-to-implement-springboot-druid-sqlserver-entra-id-authentication\/","title":{"rendered":"SpringBoot + Druid + SQL Server \u5b9e\u73b0 Entra ID \u8ba4\u8bc1"},"content":{"rendered":"\n<p>\u7684\u786e\u5feb\u6709\u597d\u4e00\u6bb5\u65f6\u95f4\u6ca1\u6709\u5728\u535a\u5ba2\u91cc\u5199\u6587\u4e86\u3002\u4eca\u5e74\u5927\u91cf\u7684\u7cbe\u529b\u90fd\u5fd9\u5728\u4e86\u5b8c\u6210\u5de5\u4f5c\u4efb\u52a1\uff0c\u800c\u4e14\u6211\u7684\u56e2\u961f\u5728\u8fc7\u53bb\u4e00\u6bb5\u65f6\u95f4\u7ecf\u5386\u6218\u7565\u8f6c\u53d8\u3001\u88c1\u5458\u548c\u9879\u76ee\u8c03\u6574\uff0c\u5341\u5206\u52a8\u8361\u3002\u4e34\u8fd1 Holiday Season\uff0c\u4e1a\u52a1\u57fa\u672c\u90fd\u505c\u4e86\u4ee5\u6c42\u7a33\u5b9a\uff0c\u6211\u603b\u7b97\u662f\u95f2\u4e0b\u6765\u4e86\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u5fae\u8f6f\u8fd9\u516c\u53f8\u91cc\u9762\u505a\u5f00\u53d1\uff0c\u7edd\u5bf9\u7ed5\u4e0d\u5f00\u7684\u5c31\u662f Azure \u4e91\u73af\u5883\u3002\u6211\u4eec\u4ece OLTP \u5230 OLAP\uff0c\u51e0\u4e4e\u6240\u6709\u9879\u76ee\u7684\u6570\u636e\u5e93\u65b9\u6848\u90fd\u4f7f\u7528 SQL Server\u3002\u53ef\u60dc\u8fd9\u4e2a\u6570\u636e\u5e93\u4ea7\u54c1\u5728\u56fd\u5185\u79d1\u6280\u516c\u53f8\u5e76\u6ca1\u6709\u4ec0\u4e48\u5e02\u573a\u3002\u8d44\u6599\u5f88\u5c11\u5e76\u4e14\u9ad8\u5ea6\u4f9d\u8d56\u5fae\u8f6f\u7ed9\u7684\u6280\u672f\u6587\u6863\u3002<\/p>\n\n\n\n<p>\u6211\u7ef4\u62a4\u7684\u4e00\u5757\u540e\u7aef\u670d\u52a1\u672c\u57fa\u4e8e\u67d0\u5f00\u6e90\u5de5\u7a0b\uff0c\u5176\u8bbe\u8ba1\u65f6\u91c7\u7528\u7684\u662f\u7ecf\u5178\u7684 SpringBoot + MyBatis + MySQL \u67b6\u6784\uff0c\u90e8\u7f72\u5230 Azure \u4e91\u73af\u5883\u9700\u8981\u5bf9\u6301\u4e45\u5c42 MySQL \u505a\u6539\u9020\uff0c\u6362\u6210 Azure SQL Database \u4ea7\u54c1\u3002<\/p>\n\n\n\n<p>\u6309\u7167 JDBC \u7684\u89c4\u8303\uff0cDruid \u662f\u80fd\u517c\u5bb9 SQL Server \u4ea7\u54c1\u7684\uff0c\u4f46\u67d0\u4e9b\u5b9a\u5236\u5316\u7684\u529f\u80fd\uff0c\u5982 Entra ID \u8ba4\u8bc1 \uff08\u539f Azure Active Directory \u8ba4\u8bc1\uff09\uff0c\u5e76\u4e0d\u539f\u751f\u652f\u6301\u3002<\/p>\n\n\n\n<p>\u6211\u6240\u5728\u7684\u56e2\u961f\u4ece\u4e24\u5e74\u524d\u5f00\u59cb\u5df2\u7ecf\u660e\u786e\u7981\u6b62\u672c\u5730\u8ba4\u8bc1\uff08\u542b\u5bc6\u7801\u3001\u5bc6\u94a5\u548c\u8bc1\u4e66\u7b49\uff09\uff0c\u82e5\u8981\u5bf9 SQL Server \u505a\u8ba4\u8bc1\u5fc5\u987b\u4f7f\u7528 Entra ID \u767b\u9646\u3002\u7efc\u4e0a\uff0c\u6211\u9700\u8981\u5bf9 Druid + SQL Server \u505a Entra ID \u8ba4\u8bc1\u7684\u6539\u9020\u548c\u9002\u914d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u67e5\u9605\u5fae\u8f6f\u7ed9\u7684\u5b98\u65b9\u8d44\u6599\uff0c\u6709\u8fd9\u4e48\u4e00\u4e2a\u529f\u80fd\u5f15\u8d77\u6211\u7684\u6ce8\u610f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><em>accessTokenCallbackClass<\/em><\/td><\/tr><tr><td>(Version 12.4+) The name of the callback-implementing class to be used with access token callback.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6784\u5efa SQL Server JDBC Connection \u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u83b7\u53d6 Access Token \u7684\u56de\u8c03\u51fd\u6570\u3002\u5728 JDBC \u9a71\u52a8\u5f00\u59cb\u8fde\u63a5\u5230\u6570\u636e\u5e93\u65f6\uff0c\u4f1a\u89e6\u53d1\u8fd9\u4e2a\u51fd\u6570\uff0c\u901a\u8fc7\u56de\u8c03\u8fd4\u56de\u503c\u5411\u7a0b\u5e8f\u8fd4\u56de\u4e00\u4e2a Entra ID \u7684 Access Token\uff0c\u5b8c\u6210\u767b\u9646\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u56e0\u6b64\u5f97\u5230\u4e86\u8fd9\u4e48\u4e00\u4e2a\u65b9\u6848\uff1a\u8bbe\u7f6e Druid \u7684 Connection URL \u914d\u7f6e\uff0c\u6307\u5b9a\u56de\u8c03\u51fd\u6570\uff0c\u5728\u56de\u8c03\u51fd\u6570\u91cc\u501f\u4f4f Azure SDK\uff0c\u83b7\u53d6\u4e00\u4e2a Entra ID Credential\uff0c\u4e4b\u540e\u518d\u4ece\u8be5 Credential \u53d6\u4e00\u4e2a\u767b\u9646 SQL Server \u4f7f\u7528\u7684 Access Token\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86 Druid \u7684 Entra ID \u767b\u9646\/\u8ba4\u8bc1\u3002<\/p>\n\n\n\n<p>Druid \u914d\u7f6e\uff1a<\/p>\n\n\n\n<p><em>application-sqlserver.yaml<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>spring:\n  datasource:\n    druid:\n      url: jdbc:sqlserver:\/\/${example.database.ip}:${example.database.port};databaseName=${example.database.name};accessTokenCallbackClass=com.example.druid.EntraIdLoginCallback;encrypt=${example.database.useSSL};trustServerCertificate=true\n      username: \"\"\n      password: \"\"\n      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver<\/code><\/pre>\n\n\n\n<p><em>* \u4f7f\u7528 Entra ID + Access Token \u767b\u9646\u7684\u65f6\u5019\uff0cJDBC \u7528\u6237\u540d\u548c\u5bc6\u7801\u5b57\u6bb5\u5fc5\u987b\u4e3a null\/\u7a7a\u767d\u3002<\/em><\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u914d\u7f6e\u8bbe\u5b9a\u4e86 <em>com.example.druid.EntraIdLoginCallback<\/em> \u7c7b\u4f5c\u4e3a Access Token \u7684\u56de\u8c03\u51fd\u6570\u7c7b\u3002\u63a5\u4e0b\u6765\u9700\u8981\u5728\u5305\u4e2d\u5b9e\u73b0\u8fd9\u4e2a\u7c7b\u4ee5\u53ca\u5b83\u7684 <em>getAccessToken()<\/em> \u51fd\u6570\u3002<\/p>\n\n\n\n<p><em>EntraIdLoginCallback.java<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package example;\n\nimport com.microsoft.sqlserver.jdbc.SQLServerAccessTokenCallback;\nimport com.microsoft.sqlserver.jdbc.SqlAuthenticationToken;\n\nimport java.util.Date;\n\npublic class EntraIdLoginCallback implements SQLServerAccessTokenCallback {\n\n    @Override\n    public SqlAuthenticationToken getAccessToken(String spn, String stsurl) {\n        \n        \/\/ TODO: Acquire an access token (Azure SDK)\n        \n        String token = \"&lt;-- Set your access token here -->\";\n        String date = new Date().toString(); \/\/ &lt;-- Set your token expiry date here -->\n\n        return new SqlAuthenticationToken(token, date);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u53ea\u9700\u8981\u5728\u56de\u8c03\u51fd\u6570\u91cc\u5b9e\u73b0\u83b7\u53d6\u4e00\u4e2a Access Token \u903b\u8f91\uff0c\u5982\u501f\u52a9 azure-identity SDK\uff0c\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u5c31\u5b9e\u73b0\u4e86 Entra ID \u7684\u767b\u9646\u8ba4\u8bc1\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u8c03\u8bd5\u65e5\u5fd7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2025-10-18 04:09:38,205 &#91;ERROR] c.a.d.p.DruidDataSource.init - init datasource error, url: jdbc:sqlserver:\/\/xxxxxxxx.database.windows.net:1433;databaseName=master;accessTokenCallbackClass=<strong>com.example.druid.EntraIdLoginCallback<\/strong>;encrypt=false;trustServerCertificate=true\ncom.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user <strong>'&lt;token-identified principal&gt;'<\/strong>. ClientConnectionId:54a6b821-aa84-4a56-8ee8-0902a1ab8ae5\n\tat com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:278) ~&#91;mssql-jdbc-13.2.0.jre8.jar:?]<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u5173\u952e\u8bcd <strong>&lt;token-identified principal&gt;<\/strong>\uff0c\u8bf4\u660e\u6b64\u65f6 SQL Server JDBC \u9a71\u52a8\u5728\u8fde\u63a5\u5230\u670d\u52a1\u5668\u4f7f\u7528 Token \u8ba4\u8bc1\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h1>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/connect\/jdbc\/connecting-using-azure-active-directory-authentication?view=sql-server-ver17\">Connect using Microsoft Entra authentication &#8211; Microsoft Learn<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/connect\/jdbc\/setting-the-connection-properties?view=sql-server-ver17\">Setting the connection properties &#8211; Microsoft Learn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7684\u786e\u5feb\u6709\u597d\u4e00\u6bb5\u65f6\u95f4\u6ca1\u6709\u5728\u535a\u5ba2\u91cc\u5199\u6587\u4e86\u3002\u4eca\u5e74\u5927\u91cf\u7684\u7cbe\u529b\u90fd\u5fd9\u5728\u4e86\u5b8c\u6210\u5de5\u4f5c\u4efb\u52a1\uff0c\u800c\u4e14\u6211\u7684\u56e2\u961f\u5728\u8fc7\u53bb\u4e00\u6bb5\u65f6\u95f4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/posts\/2401"}],"collection":[{"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/comments?post=2401"}],"version-history":[{"count":2,"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/posts\/2401\/revisions"}],"predecessor-version":[{"id":2406,"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/posts\/2401\/revisions\/2406"}],"wp:attachment":[{"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/media?parent=2401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/categories?post=2401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xbtzone.com\/blog\/wp-json\/wp\/v2\/tags?post=2401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}