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)
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.