当前位置: 首页 > 知识库问答 >
问题:

无法访问Spring护套执行器endpoint

柳均
2023-03-14

我尝试在Spring启动中使用执行器endpoint。应用程序运行平稳。我的pom文件如下所示:

<?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.7.3</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.luv2code.springboot</groupId>
    <artifactId>thymeleafdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>thymeleafdemo</name>
    <description>Ab Jove principium</description>
    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>

        <!-- umumi bağımlılıklar -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId> 
        </dependency>
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.11.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

这是应用程序的内容。属性文件:

spring.datasource.url=DATABASE_URL
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect

# Spring Data JPA properties
spring.data.jpa.repository.packages=com.yok.springboot.thymeleafdemo.dao
spring.data.jpa.entity.packages-to-scan=com.yok.springboot.thymeleafdemo.entity
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.hibernate.ddl-auto=create

#
# JDBC properties
#
app.datasource.jdbc-url=DATABASE_URL
app.datasource.username=USERNAME
app.datasource.password=PASSWORD

#
# Hikari properties
spring.datasource.hikari.maximumPoolSize=10
spring.datasource.hikari.idleTimeout=2000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=20000
spring.datasource.hikari.connectionTimeout=30000

# Actuator properties 
  
# expose all endpoints:
management.endpoints.web.exposure.include=*  
management.endpoints.beans.enabled=true
management.endpoints.web.exposure.include=info,env
management.endpoint.env.enabled=true
management.endpoint.info.enabled=true 
management.endpoints.enabled-by-default=true

这是我的Spring Boot应用程序的开始:

package com.yok.springboot.thymeleafdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThymeleafdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThymeleafdemoApplication.class, args);
    }
}

每当我尝试通过键入连接/health、/Info或/metricsendpoint时http://localhost:8080/health,HTTP请求传输到http://localhost:8080/showMyLoginPage.我无法到达终点。我怎样才能解决这个问题?提前谢谢。

编辑-1法提赫先生要求我观察结果“http://localhost:8080/actuator这张照片揭示了:

下面是应用程序的控制台输出:

https://drive.google.com/file/d/1zYP1qe-Ohbcan93ZO6rqjxX9LqlGiIIg/view?usp=sharing

Edit-2
问题已经部分解决。应用程序登录后,执行器可用。但问题是,登录页面之后,首页就出现了。所有的执行器都在工作,但是,每当我点击http://localhost:8080/actuator/health URL,屏幕上就会出现{"status":"DOWN"}。以下是在此操作过程中获得的控制台输出:

控制台输出:(引发了异常)

java.lang.非法描述异常:数据源或数据源类名称或 jdbcUrl 是必需的。在 com.zaxxer.hikari.hikariConfig.validate(HikariConfig.java:1029) ~[HikariCP-4.0.3.jar:na] 在 org.java.springframework.jdbc.datasources.doGetConnection(数据源.java.java:117) ~[SpringriCP-4.0.3.3.jar:na] 在 org.springframework.jdbc.datasources.datasources.datasource.doGetConection(DataSourceis.java:117) ~[Spring-jdbc-5.jar.3.22.jar:5.3.22]5.3.22] 在组织Spring框架.java.jdframework.jdbc.core.Jdbc.Jdbc.数据源指示符.get(数据源生成器.jar.java:122) ~[Spring-jdbc模板.java:330) ~[Spring-jdbc-5.3.22.jar:5.3.22] 在组织Spring框架.boot.actuate.jdbc.数据源指示符.getProduct(数据源健康指标.java:122) ~[Spring-启动执行器-2.7.3.jar:2.7.3] 在组织Spring框架.boot.actuate.jdbc.DataSourceHealth 指标.doDataSourceHealthCheck(数据源健康指标.java:105) ~[Spring启动执行器-2.7.3.jar:2.7.3] 在 org.springframework.boot.actuate.jdbc.DataSourceHealth指标.doHealthCheck(数据源运行状况指示器.java:100) ~[Spring启动致动器-2.7.3.jar:2.7.3]

Edit-3
Fatih先生指出了WebSecurityConfiguration的一些更改。我已经更改了代码,我收到了这个错误:

java.lang.非法状态例外: permitAll 仅适用于 HttpSecurity.authorizeRequests() 或 HttpSecurity.authorizeHttpRequestss()。请定义一个或另一个,但不能同时定义两者。

这是我所做的改变:

/*
* import section have omitted for brevity
*/

@Configuration
@EnableWebSecurity
public class DemoSecurityConfig {

    /*
     * other codes have omitted for brevity
     */

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        http.authorizeHttpRequests(
                (authz) -> authz.antMatchers("/actuator/**").permitAll().anyRequest().authenticated());

        http.authorizeRequests(
                configurer -> configurer.antMatchers("/**").hasRole("ADMIN").antMatchers("/**").hasRole("USER"))

                .formLogin(configurer -> configurer.loginPage("/showMyLoginPage")
                        .loginProcessingUrl("/authenticateTheUser").permitAll())

                .logout(configurer -> configurer.permitAll())

                .exceptionHandling(configurer -> configurer.accessDeniedPage("/access-denied"));

        return http.build();

    }

}

下面是控制台输出:https://drive.google.com/file/d/1CtjRBHXVRqirZ0Vt_3FEhx_N9oEwyfFZ/view

共有2个答案

岳和泽
2023-03-14

请考虑使用一个版本作为您的依赖项,因为我记得1.9.5 RELEASE或1.9.5可能在这种情况下有所帮助,我一年前遇到了同样的问题。

惠志
2023-03-14

您正在使用spring-Security包来实现应用程序安全。因此,当您想要访问您的/执行器endpoint时,您需要先登录。如果您想要在不登录的情况下访问您的/执行器endpoint,您必须配置安全配置。通过以下配置,您可以从安全中排除所有以/执行器开头的endpoint。

@EnableWebSecurity
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/actuator/**").permitAll().anyRequest().authenticated();
    }
}

由于WebSecurityConfigrerAdapter已被弃用,您也可以这样做。

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
        .authorizeHttpRequests(autz -> autz
                .mvcMatchers("/actuator/**").permitAll()
                .anyRequest().authenticated()
        );
    return http.build();
    }
}

**是一个通配符定义,允许您在不登录的情况下访问此endpoint,无论执行器部分之后是什么。

 类似资料: