Windows authentication for SQL server using SQlServerDriver and Springboot JDBCTemplate

I want to connect to SQL server data base using windows authentication. I am able to connect with SQL server authentication, but the password is getting changed very frequently and my application is failing

i have following dependencies for JDBCTemplate and ms-sql driver in pom.xml
my parent is spring-boot-starter-parent of version 2.7.18 and i use java 11

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><dependency>
<artifactid>org.springboot</artifactid>
<groupid>spring-jdbc</groupid>
</dependency>
<dependency>
<artifactid>com.microsoft.sqlserver</artifactid>
<groupid>mssql-jdbc</groupid>
</dependency>
</code>
<code><dependency> <artifactid>org.springboot</artifactid> <groupid>spring-jdbc</groupid> </dependency> <dependency> <artifactid>com.microsoft.sqlserver</artifactid> <groupid>mssql-jdbc</groupid> </dependency> </code>
<dependency>
<artifactid>org.springboot</artifactid>
<groupid>spring-jdbc</groupid>
</dependency>

<dependency>
<artifactid>com.microsoft.sqlserver</artifactid>
<groupid>mssql-jdbc</groupid>
</dependency>

In My application.yml file i have like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>spring:
datasource:
url: jdbc:sqlserver://hostName:portNumber;databaseName=dbName;authenticationScheme=NTLM;integratedSecurity=true;trustServerCertfication=true;
username:username
password:password
</code>
<code>spring: datasource: url: jdbc:sqlserver://hostName:portNumber;databaseName=dbName;authenticationScheme=NTLM;integratedSecurity=true;trustServerCertfication=true; username:username password:password </code>
spring:
  datasource:
     url: jdbc:sqlserver://hostName:portNumber;databaseName=dbName;authenticationScheme=NTLM;integratedSecurity=true;trustServerCertfication=true;
     username:username
     password:password

in my database configuration

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Value("${spring.datasource.url}")
String url;
@Value("${spring.datasource.username}")
String username;
@Value("${spring.datasource.password}")
String password;
@Bean
public Datasource dataSource(){
DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
dataSource.setUrl(url);
dataSource.setUserName(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public JDBCTemplate jdbcTemplate(DataSource dataSource){
return new JDBCTemplate(dataSource);
}
</code>
<code>@Value("${spring.datasource.url}") String url; @Value("${spring.datasource.username}") String username; @Value("${spring.datasource.password}") String password; @Bean public Datasource dataSource(){ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SqlServerDriver"); dataSource.setUrl(url); dataSource.setUserName(username); dataSource.setPassword(password); return dataSource; } @Bean public JDBCTemplate jdbcTemplate(DataSource dataSource){ return new JDBCTemplate(dataSource); } </code>
@Value("${spring.datasource.url}")
String url;
@Value("${spring.datasource.username}")
String username;
@Value("${spring.datasource.password}")
String password;
@Bean 
public Datasource dataSource(){
DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
dataSource.setUrl(url);
dataSource.setUserName(username);
dataSource.setPassword(password);
return dataSource;
}

@Bean 
public JDBCTemplate jdbcTemplate(DataSource dataSource){
return new JDBCTemplate(dataSource);
}

using the above the code is not working the authentication is failing with Error Failed to obtain JDBC connection .
nested Exception is com.microsoft.sqlserver.jdbc.SQLServerException : Login failed.
the login is from untrusted domain and cannot be used with Windows Authentication.

when i use SQLserver authentication by changing my YML file to below the code is working as expected but failing when the password changes.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>spring:
datasource:
url: jdbc:sqlserver://hostName:portNumber;databaseName=dbName;trustServerCertfication=true;
username:username
password:password
</code>
<code>spring: datasource: url: jdbc:sqlserver://hostName:portNumber;databaseName=dbName;trustServerCertfication=true; username:username password:password </code>
spring:
  datasource:
     url: jdbc:sqlserver://hostName:portNumber;databaseName=dbName;trustServerCertfication=true;
     username:username
     password:password

we have other application where we don’t use JDBCTemplate and gets connection by using

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Connection getConnection(){
try{
class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
Connection=DriverManeger.getConnection("jdbc:sqlserver://hostName:portNumber;databaseName=dbName;authenticationScheme=NTLM;integratedSecurity=true;trustServerCertfication=true;", username,password);
}
catch(Exception e){
log.error("Exception occurred : {}",e.getMessage());
}
}
with this connection is successful and able to query the result set. But we are using JDBCTemplate and want to use windows Authentication.
</code>
<code>public Connection getConnection(){ try{ class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver"); Connection=DriverManeger.getConnection("jdbc:sqlserver://hostName:portNumber;databaseName=dbName;authenticationScheme=NTLM;integratedSecurity=true;trustServerCertfication=true;", username,password); } catch(Exception e){ log.error("Exception occurred : {}",e.getMessage()); } } with this connection is successful and able to query the result set. But we are using JDBCTemplate and want to use windows Authentication. </code>
public Connection getConnection(){
try{
   class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
 Connection=DriverManeger.getConnection("jdbc:sqlserver://hostName:portNumber;databaseName=dbName;authenticationScheme=NTLM;integratedSecurity=true;trustServerCertfication=true;", username,password);
}
 
catch(Exception e){
 log.error("Exception occurred : {}",e.getMessage());

  } 
}

with this connection is successful and able to query the result set. But we are using JDBCTemplate and want to use windows Authentication.


Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật