Can a Java application running on a Window's Server connect to SQL Server via Windows Authentication

半城伤御伤魂 提交于 2019-12-10 08:12:22

问题


Let me give some background before I ask my question. I’m at a shop that primarily runs Windows. We have several batch applications running on Windows Servers (mostly 2003). Most of these batch applications are written in C# and C++; however we have a handful of applications that are written in Java.

The batch Java applications are connecting to a SQL Server 2005 database using JDBC. Please note we are not using an application server.

Currently we store database connection information (database, username, & password) in the Windows Registry.

Unfortunately these really unfriendly auditors (bad attempt at humor) are not happy with us over the decision to store database connection information in the Windows registry. We are now updating our batch applications to connect to SQL Server using Windows Authentatication.

Using Windows Authentatication for C# and C++ applications is not a problem; however I’m stuck on the direction to take for the Java applications.

Can anyone advise if it is possible to use Windows Authentatication to connect to a SQL Server 2005 database from a Java batch application running on a Windows server? Again we are not using an application server.

If this is possible what are your recommended approaches?

I have a strategy to simply encrypt the password which will make the auditors happy, however I would prefer to have all of my batch applications connect to SQL Server via Windows Authentatication.


回答1:


You can connect to SQL Server from Java programs using windows authentication as follows:

  1. Create a windows account for the application that would be used to run your programs. This account's credentials will be used to connect to the server.
  2. Get Microsoft JDBC Driver for SQL Server from here.
  3. Configure the JDBC URL as follows:

    jdbc:sqlserver://<hostname>;databaseName=<DBName>;integratedSecurity=true
    
  4. Configure the launcher that run the Java programs from command line to include the following JVM parameter:

    -Djava.library.path="<jdbc driver dll location>"
    

    where the location is the directory where the JDBC driver downloaded earlier is installed or extracted. It was C:\Program Files\sqljdbc_4.0.2206.100_enu\sqljdbc_4.0\enu\auth\x64 in my case. As Luke Woodward mentioned in the comments, the dll should be picked based on the JVM used for running these programs.

With the above configuration, the connection established to SQL Server would use the Windows Authentication Credentials of the domain user running the java program/process.




回答2:


The first step is to setup ODBC, you can go to Control panel -> Administrative tools -> ODBC. Add a new DSN to connect MS SQL Server using windows authentication account following wizard setup. The second step is the same as using SQL Server authentication account. But the only change is that the connection string is changed to: jdbc:odbc:dsn-name. There is no need to use username/password anymore, because it is already connected to the server.



来源:https://stackoverflow.com/questions/12480619/can-a-java-application-running-on-a-windows-server-connect-to-sql-server-via-wi

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