Access token connection fails after some time with Login failed NT AUTHORITY\ANONYMOUS LOGON error

早过忘川 提交于 2020-01-06 07:55:10

问题


I have a Spring web application on an Azure App service that is connecting to an Azure SQL DB using the Microsoft JDBC driver authenticating using an access token. It initially works find but after some time I start to get SQLServerException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'

I'm following the steps outlined in the MS docs.

    AuthenticationContext context = new AuthenticationContext(mySTSUrl, false, Executors.newFixedThreadPool(1));
    ClientCredential cred = new ClientCredential(myClientId, myClientSecret);

    Future<AuthenticationResult> future = context.acquireToken("https://database.windows.net/", cred, null);
    String accessToken = future.get().getAccessToken();

    // Connect with the access token.
    ds = new SQLServerDataSource();

    ds.setServerName(myServer); // Replace with your server name.
    ds.setDatabaseName(myDB); // Replace with your database name.
    ds.setEncrypt(true);
    ds.setAccessToken(accessToken);
    ds.setHostNameInCertificate("*.database.windows.net");
    ds.setTrustServerCertificate(false);
    ds.setLoginTimeout(30);

This all works for sometime (haven't pegged an exact time or trigger) then starts to fail with:

Caused by:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'. ClientConnectionId:connectionidredacted at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:259) at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:256) at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:108) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4548) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3409) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:85) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3373) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7344) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2713) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2261) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1921) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1762) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1077) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:1031) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:69) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:106)


回答1:


You are using token which does expire, you are not specifying any token lifetime in you code so token lifetime depends on the systems default.



来源:https://stackoverflow.com/questions/54139304/access-token-connection-fails-after-some-time-with-login-failed-nt-authority-ano

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!