我该如何在Java Spring Boot中不尝试启动数据库连接?

发布于 2025-01-22 22:25:35 字数 3182 浏览 0 评论 0原文

我需要知道Java Spring引导中是否有一种方法不尝试在启动时尝试连接。我要做的是集成的SQL Server连接。我正在尝试生成具有集成连接的.jar,而不是使用SQL凭据。作为开发人员,我没有数据库的访问权限,但是运行.jar的服务帐户确实可以。 问题是,如果我尝试建立具有集成身份验证的连接字符串,它将尝试使用我的凭据登录,并且永远无法登录,因此它将无法生成.jar 目前,我的应用程序可以使用SQL凭据正常工作,但是我需要迁移到集成的凭据。

我的pom.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>X</groupId>
    <artifactId>X</artifactId>
    <version>1.0.0</version>
    <name>X</name>
    <description>X</description>
    <properties>

        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

和我的应用程序

spring.datasource.url=jdbc:sqlserver://X.X.X.X:1433;database=X
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=X
spring.datasource.password=X
server.port=8080

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

I need to know if there is a way in Java Spring boot not to try a connection at startup. What I am trying to do is an integrated SQL server connection. I am trying to generate a .jar with integrated connection and not with SQL credentials. As a developer, I don't have access permissions to the database, but the service account that runs the .jar does.
The thing is that if I try to establish a connection string with integrated authentication it will try to log in with my credentials and it will never be able to, so it will not be able to generate the .jar
At the moment my app is working fine with sql credentials, but I need to migrate to integrated credentials.

My pom.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>X</groupId>
    <artifactId>X</artifactId>
    <version>1.0.0</version>
    <name>X</name>
    <description>X</description>
    <properties>

        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

And my application.properties:

spring.datasource.url=jdbc:sqlserver://X.X.X.X:1433;database=X
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=X
spring.datasource.password=X
server.port=8080

I also try adding this line in application.properties with no success:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2025-01-29 22:25:35

在主类上使用这种毒管解决所有问题

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

而不是

@SpringBootApplication()

Using this anotation on the main class resolve everything

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

Instead of

@SpringBootApplication()
燕归巢 2025-01-29 22:25:35

启动JAR时,服务帐户是否使用特殊的春季配置文件?
然后,您可以在您的应用程序中执行此操作。Properties:

#---
spring.config.activate.on-profile=serviceProfile 
spring.datasource.url=jdbc:sqlserver://${DB_HOST};database=${DB_NAME} 
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver 
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD} 
server.port=8080

服务将像这样启动JAR:

java -jar -Dspring.profiles.active=serviceProfile -DDB_HOST=X.X.X.X:1433 -DDB_NAME=X -DDB_USER=X -DDB_PASSWORD=X application.jar

Is the service account useing a special spring profile when starting the jar?
Then you could do this in you application.properties:

#---
spring.config.activate.on-profile=serviceProfile 
spring.datasource.url=jdbc:sqlserver://${DB_HOST};database=${DB_NAME} 
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver 
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD} 
server.port=8080

The service would start the jar like this:

java -jar -Dspring.profiles.active=serviceProfile -DDB_HOST=X.X.X.X:1433 -DDB_NAME=X -DDB_USER=X -DDB_PASSWORD=X application.jar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文