Datasource to SQL Server does not work in websphere - sql-server

We are using in our project a websphere application server and want to connect to a sql server. In our websphere we have configured a datasource and the testing of the connection in the websphere works. In our deployed application the connection does not work. We get this exception:
Caused by: com.ibm.websphere.ce.cm.StaleConnectionException: Single-Sign-On is only supported on Windows. Please specify a user name.DSRA0010E: SQL State = 08001, Error Code = 0
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
at java.lang.reflect.Constructor.newInstance(Constructor.java:515)
at com.ibm.websphere.rsadapter.GenericDataStoreHelper.mapExceptionHelper(GenericDataStoreHelper.java:608)
at com.ibm.websphere.rsadapter.GenericDataStoreHelper.mapException(GenericDataStoreHelper.java:670)
at com.ibm.ws.rsadapter.AdapterUtil.mapException(AdapterUtil.java:2111)
at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getPooledConnection(WSRdbDataSource.java:2320)
at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1600)
at com.ibm.ejs.j2c.FreePool.createManagedConnectionWithMCWrapper(FreePool.java:2036)
at com.ibm.ejs.j2c.FreePool.createOrWaitForConnection(FreePool.java:1713)
at com.ibm.ejs.j2c.PoolManager.reserve(PoolManager.java:2496)
at com.ibm.ejs.j2c.ConnectionManager.allocateMCWrapper(ConnectionManager.java:1063)
at com.ibm.ejs.j2c.ConnectionManager.allocateConnection(ConnectionManager.java:700)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:668)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:635)
at de.volkswagen.dps.server.core.database.ConnectionFactory.createDatasourceConnection(ConnectionFactory.java:45)
... 40 more
We use this driver: jtds-1.2.4.jar which we got from here
Have anybody an idea how to solve the problem?

Looks like a security issue.
With Test Connection a SSO (Single sign on) can be used, but not in the deployed app.
you need to make sure you setup a JAAS authentication alias and configure your Datasource to use it, that alias comtains the username and password to be used for authentication.
Open the Datasource in the admin console and there is a link on the right to create the JAAS alias and tHere are combo boxes to tell the datasource to use it.

Related

What can cause to a failed database connection for a spring boot application other than incorrect datasource settings?

I have configured my spring boot application to run from a remote sql server database deployed to one of our environments with ip and port configuration below. And it works with no issue.
datasource.common.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
datasource.common.platform=mssql
datasource.common.username=${dbUsurname}
datasource.common.password=${dbPassword}
datasource.common.url=jdbc:sqlserver://10.223.124.566:31567;databaseName=acc
But when I replace my connection to another database instance that is deployed for another environment , even if I supply correct username , password and ip combination I get error.
datasource.common.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
datasource.common.platform=mssql
datasource.common.username=${dbUsurname}
datasource.common.password=${dbPassword}
datasource.common.url=jdbc:sqlserver://10.223.124.211:31567;databaseName=int
I get the following trace.
Cannot open database "int" requested by the login. The login failed.
ClientConnectionId:0dd4761b-8969-4848-96f8-c7ca41c48bf
I can connect to database with which I got error in spring application from mssql management studio with the given configuration and had no issue connecting it. Is there any setting for a database instance that prevents applications from connecting it other than username and password ?

ASP.NET Core Web Api publishing to IIS producing an Sql Server connection error

I've created a simple web api in asp.net core 2.0 which uses localdb as the backing datastore. It works completely as expected in debug - I'm able to send requests from postman (or browser) and step through my code. Here is my connection string:
"Server=(localdb)\\MSSQLLocalDB;Database=UniversityDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
I now want this to be running constantly so that I can consume it from a Xamarin.Forms project so I have tried publishing the web app using the Visual studio wizard.
1.
2.
UniversityApi then appears in IIS under Default Web Site but not as an application. I choose to 'Convert to Application' which shows me this dialog:
3.
The NetCoreApps Application Pool uses No Managed Code.
Error:
{
"error": [
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.\r\n)"
]}
The error seems to describe a problem with connecting to my Sql Server instance which is strange because it works perfectly while debugging in VS. Any ideas?
After searching for a while I stumbled onto this question:
SQL won't connect after deploying
One of the answers did the trick for me, credit to Robotron.
IIS Manager
Application Pools
Find the pool your app belongs to (for me it was .NET v4.5)
Right click -> Advanced Settings
Scroll down to Identity
Change from whatever (for me it was ApplicationPoolIdentity, same as you have) into LocalSystem.
Add the "LOCAL SERVICE"(Windows account) to SQL Server.
Change from "ApplicationPoolIdentity" to "LocalService" in (IIS Application Pools Advanced Settings)
NOTE. ADD "LOCAL SERVICE"(Windows account) need Permission into SQL Server.
Thanks a lot for the answers. I have similar problem. My .net core web api is hosted in AWS. It was working before when I was just accessing the api via ip address (sample https://xx.xx.xxxx/swagger) but when I integrated it to domain (https://xx.sampledomain.com/swagger) I ran into the same problem. These two steps solved the error.
1st I used the LocalService account for IIS Application Pool, then added the NT Authority/Local Service account in the MS SQL Server 2016.
enter image description here
On the Publish screen > Database > DefaultConnection, I ticked on the 'Use this connection string at runtime' checkbox.
enter image description here

JDBC URL connection to SQL Server using Windows Authentication in JIRA plugin

I am currently at my wits end. I am trying to connect a JIRA plugin (Atlassian Java SDK) to a SQL Server 2012 database. It's required that I use Windows Authentication, but JIRA is logging:
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.
I've read that this can be combatted by adding the sqljdbc_auth.dll file to the program's library path, but there are no references specifically for JIRA plugins on how to do this other than for the base JIRA dbconfig.xml, which I am not using. This is a connection made within the plugin using the code:
String url = String.format("jdbc:sqlserver://%s:1433;databaseName=%s;integratedSecurity=true;domain=XXXX;", server, database);
I have tried to add the dll file to the following locations, all to no avail:
C:/{JIRA INSTALL PATH}/bin
C:/{JIRA INSTALL PATH}/jre/bin
C:/{JAVA INSTALL PATH}/jre_8.x.x/bin
What gives?
UPDATE:
I have gone in and added the JDBC JAR/DLL to the classpath/library path respectively in the JIRA Windows service configuration tool. Still getting the same error.
UPDATE 2:
I have switched to using the JTDS driver to see if JIRA was just not able to use the standard JDBC driver for some reason, but getting a similar error:
java.sql.SQLException: I/O Error: SSO Failed: Native SSPI library not loaded. Check the java.library.path system property.
I have also created a new Java app to test this in a controlled environment, but do not get this error after adding the ntlmauth.dll file to the C:\Windows\System32 directory. This leads me to believe this may be a bug within JIRA or I have something configured improperly.
Here is my own conclusion after many weeks of searching:
It is (currently) impossible to do this without the use of a password (which is sometimes the entire point of using Windows/Active Directory credentials). You can however use the JTDS driver in junction with a domain, username, and password to authenticate to servers that require windows authentication. This does not solve the core issue, but from what I can tell is the only current solution close enough to it.

How to connect to sql DB from soapUI using connection string?

I would like to ask someone who already tried to connect to SQL database from soapUI opensource version.
I have a test step JDBC request where I try to test and connect to my sql database. I use opensource version of soapUI. I read how it should be set up but still experience some problems.
This is what I do:
I installed java 8 and jdbc driver version 4.1. Added jdbc jar file to SoapUi/jre/bin folder as it is said in documentation for soapUI. This version of driver should work with Java 8.
Then in Driver field I write
com.microsoft.sqlserver.jdbc.SQLServerDriver
In connection string:
jdbc:sqlserver://Sservername:portnumber;databaseName=Consumption;username=alyona.kovalyova;password=mypassword
I get error saying
Can't get the Connection for specified properties; com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'alyona.kovalyova'. ClientConnectionId...
I use my domain account because in our company the policy does not allow us to use database user. Because of this I tried to add integratedSecurity parameter to the string which should allow to login with windows account like this:
jdbc:sqlserver://S23-AS:1433;databaseName=Consumption;integratedSecurity=true
I also get error:
Can't get the Connection for specified properties; com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId..
Does anyone encountered the same issue before? Could you give some recommendation?
For integrated authentication to work you'll need sqljdbc_auth.dll matching your environment (32bit / 64bit) in your library path (putting it in bin as well might help already)

Authenticating to a SQL Server instance as a Windows User via JDBC

I'm having to support multiple database types for my tenant-enabled web application. Among others, I have successfully supported Microsoft's SQL Server, by using the net.sourceforge.jtds.jdbc.Driver class with a connection String like "jdbc:jtds:sqlserver://192.168.1.189:1433/ApplicationName". This works, but it requires that the user explicitly defines a user in the SQL Server instance and enables SQL Server authentication.
Now, inevitably, requirements changed, and we're supposed to support connecting to SQL Server via Windows Authentication. Evidently this requires some sort of change to the connection string, since the data base server must somehow be able to distinguish whether the credentials passed into the data base connection are for a user defined in the SQL Server installation or in the Windows OS. But what is it?
Acting on advice from the internet, if progressed as far as extending the connection string with ;useNTLMv2=true;domain=WORKGROUP. That seems to make the data base server aware that I want to authenticate as a Windows user, but the actual log-in fails with
The login is from an untrusted domain and cannot be used with Windows authentication. (code 18452, state 28000)
Now im my testing set-up, both the J2EE app and the SQL server instance are in fact on the same machine (although in production they may not be), and still this computer isn't trusted enough to log on to itself? Evidently I'm missing a big part of the puzzle here. What does one have to do to convince an SQL Server instance that the user who started it can in fact log on to it via JDBC?
Edit
Since we have already sunk too much unsuccessful effort trying to integrate our web application with a full Microsoft infrastructure stack (SQL Server, Active Directory, Domain Name Service...), I have to restrict this question:
Does anyone know a way to access an SQL Server installation with a user account defined as a "Windows User" via JDBC form a J2EE application, without having to use Active Directory, a Windows machine running the web application and a proprietary DLL? The bounty is for any solution of that sub-problem. The entire problem is clearly too broad to be answered in one forum post.
I ran into the error
The login is from an untrusted domain and cannot be used with Windows
authentication
when a 2012 SQL Server DB instance was recently upgraded to 2016. In order to use AD based authentication with the JTDS driver and SQL Server 2016, it seems necessary to specify both the useNTLMv2=true and the domain=example.com suffix in order to establish a connection. The name of the domain is absolutely necessary and I confirmed that through testing. This is with JTDS driver version 1.3.1.
Example of a working connection string using AD based authentication to SQL Server 2016 DB with JTDS 1.3.1:
jdbc:jtds:sqlserver://sqlserver2016db.example.com/MY_DB_NAME;domain=example.com;prepareSQL=2;useNTLMv2=true
UPDATE
Recently (due to the pandemic lockdown), I found myself also having to connect to SQL Server using Windows authentication from a non-domain computer (over VPN). This can be accomplished by starting the Windows process initiating the SQL Server connection, e.g. Eclipse / Spring Tool Suite, with the following command:
C:\Windows\System32\runas.exe /netonly /user:domain\user "path_to_executable.exe"
Source: https://www.mssqltips.com/sqlservertip/3250/connect-to-sql-servers-in-another-domain-using-windows-authentication/
In discovering that gem, I also discovered that encryption needed to be used. Here are the settings I'm using (in addition to now running the executable with /netonly and a domain account):
spring.datasource.url=jdbc:jtds:sqlserver://fqdn_of_server_including_domain/DBNAME;domain=mydomain;useNTLMv2=true;ssl=require;prepareSQL=2;
spring.datasource.username=domainaccountname_without_domain_prefix
spring.datasource.password=password
spring.datasource.testOnBorrow=true
spring.datasource.hikari.connection-test-query=SELECT 1
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
What you describe certainly appears to be feasible. I have SQL Server 2008 R2 Express running on a stand-alone server and I was able to connect using a Windows username/password on that server via jTDS 1.3.1 from a separate Windows machine and from an Xubuntu 14.04 box.
On the machine running SQL Server I created a Windows user named 'kilian'. In SQL Server itself I created a SQL Login for NT AUTHORITY\Authenticated Users. Then in the database (named 'myDb') I created a User named 'AuthenticatedUsers' for that SQL Login. Just to keep things simple I gave that user db_owner rights on the database.
There is no SQL Login for 'kilian' and no database User with that name.
Then, from the other two machines (the Windows workstation and the Xubuntu box) I just ran this:
package com.example.jtdstest;
import java.sql.*;
public class JtdsTestMain {
public static void main(String[] args) {
try (Connection con = DriverManager.getConnection(
"jdbc:jtds:sqlserver://192.168.1.137:52865/myDb" +
";domain=whatever",
"kilian",
"4theBounty")) {
try (Statement s = con.createStatement()) {
String sql = "SELECT LastName FROM Clients WHERE ID=1";
try (ResultSet rs = s.executeQuery(sql)) {
rs.next();
System.out.println(rs.getString("LastName"));
}
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
Additional notes:
I did not have to include useNTLMv2=true. I was able to connect with or without that parameter.
I did have to include domain= to tell the SQL Server not to use SQL authentication, but the actual value I supplied made no difference. (I literally used 'whatever', which was not the name of the server or the name of the workgroup to which it belongs.)
Alternative Method
The alternative solution is to utilize integrated security. This enables your application to connect to the database as the user in which the application is currently running as. This is enabled by adding integratedSecurity=true; into the connection string properties. If you run into any trouble, make sure the sqljdbc_auth.dll is accessible via classpath or within your app library.
Security Note
You're probably already aware, but just have to say make sure not to grant access to "Authenticated Users" to your database as previously suggested as part of the demonstration. Identify which user account your application runs as and grant access to only that specific user in your database server.
Sources / Additional Info
MSDN Doc on JDBC Connection String Configuration (http://technet.microsoft.com/en-us/library/ms378428(v=sql.110).aspx)
The main problem is the windows authentication with a full java solution (no DLL). So you could use one of the libs below:
NTLM authentication: http://ioplex.com/jespa.html
spring based Kerberos authentication: http://projects.spring.io/spring-security-kerberos/
another integrated windows auth lib is SPNEGO (don't know much about this one)
So once your app is authenticated with one of the lib above, your JDBC should run fine using "integratedSecurity=true;" and if needed "authenticationScheme=JavaKerberos".
Firstly you should write the jdbc connection like this:
String url ="jdbc:sqlserver://PC01\inst01;databaseName=DB01;integratedSecurity=true";
then
you need to enable the SQL Server TCP/IP Protocol in Sql Server Configuration Manager app. You can see the protocol in SQL Server Network Configuration.
I can see two possibilities,
1. You are using a local system account which the server won't understand
In this case, switch to a domain account.
Windows authentication has different credential requirements and you might not be meeting those.
In this case try changing the password to match the requirements.
It is very well possible that both are happening.
see this other SO post that describes how to connect to a SQL Server with Windows Authentication from a Linux machine through JDBC
This is my NiFi setup for jTDS driver:
Database Connection URL: jdbc:jtds:sqlserver://192.168.1.189:1433;DOMAIN=domain_name
I didn't need to add useNTLMv2=true, but most people need to, so if it doesn't work you can try also:
jdbc:jtds:sqlserver://192.168.1.189:1433;DOMAIN=domain_name;useNTLMv2=true
Database Driver Class Name: net.sourceforge.jtds.jdbc.Driver
Database User: domain_user_name (**without** #domain)
Password: domain_password
Validation query: select 1
One of the possible reasons for this error to appear is when you configure you data source to use windows authentication and SQL Server is using Extended Protection mode together with SSL (i'm not sure if SSL is required though). This mode requires the client to send additional information - signed service principal name (SPN) and channel binding token (CBT). See more information about Extended Protection Mode here. Currently both JTDS JDBC and Microsoft JDBC drivers do not support this mode. I couldn't find an official statement from JTDS, but there is an open ticket for Microsoft drivers.
In order to configure Extended Protection mode, go to SQL Server Configuration Manager, select properties on SQL Server Network Configuration -> Protocols for %your instance% and change Extended Protection option.

Resources