Spring通过域身份验证管理到MS Sql server的数据源连接

时间:2014-01-09 19:50:52

标签: java sql sql-server spring

尝试使用Spring Config中的JTDS驱动程序创建与MS SQL服务器的数据源连接时出错。我正在使用非Windows机器的域身份验证。

ERROR:

Caused by: java.sql.SQLException: Login failed. 
The login is from an untrusted domain and cannot be used with Windows authentication.

Spring-Datasource.xml:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
    <property name="url" value="jdbc:jtds:sqlserver://MS_SERVER/ms_db;domain=myDomain;integrated security=false"/>
    <property name="username" value="myUser"/>
    <property name="password" value="myPassword"/>
</bean>

我可以手动创建连接并且工作完美如下:

public static void main(String[] args) {
    System.out.println("Starting Connection test");

    Connection connection;

    String url = "jdbc:jtds:sqlserver://MS_SERVER/ms_db;domain=myDomain;integrated security=false";
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        connection = DriverManager.getConnection(url, "myUser","myPassword"));
        System.out.println("Connection successful");
    }
    catch (Exception e) {
        System.err.println("Cannot connect to database server");
        e.printStackTrace();
    }
}

任何建议都将不胜感激

2 个答案:

答案 0 :(得分:3)

试试这个: -

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
    <property name="url" value="jdbc:jtds:sqlserver://MS_SERVER/ms_db;domain=myDomain;integrated security=false"/>
    <property name="username" value="myUser"/>
    <property name="password" value="myPassword"/>
</bean>

DBCP的依赖关系: -

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

答案 1 :(得分:0)

根据http://jtds.sourceforge.net/faq.html,您需要传递useNTLMv2 = true和domain = yourdomain。

尝试使用此连接字符串

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
    <property name="url" value="jdbc:jtds:sqlserver://MS_SERVER/ms_db;USENTLMV2=true;domain=myDomain"/>
    <property name="username" value="myUser"/>
    <property name="password" value="myPassword"/>
</bean>