禁止弹簧启动执行器/关闭端点



我第一次使用 Spring Boot 应用程序时,执行器不安全,因此很容易通过/actuator/shutdown 端点远程关闭。最近,我使用Spring安全性保护了我的执行器,并且它已经起作用了。现在我需要提供 http 基本凭据来访问端点,但现在对/actuator/shutdown 端点的 curl 调用失败并显示禁止错误。我一定在某处配置不正确。

我的卷曲命令:

curl -XPOST -u actuator:password http://host:port/actuator/shutdown -k

我可以调用其他端点,似乎只有关闭端点被禁止。

我的配置:

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.info.git.mode=full
management.endpoint.shutdown.enabled=true
management.server.port=8081
spring.security.user.name=actuator
spring.security.user.roles=ACTUATOR
spring.security.user.password=password

编辑:

@Configuration
public static class ActuatorWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
.anyRequest().hasRole("ACTUATOR")
.and()
.httpBasic();
}
}

解决方案是在 WebSecurityConfigureAdapter 配置方法中禁用 csrf。

http.csrf().disable();

我遇到了同样的问题。无论我做什么,执行器/关闭都会被禁止返回。上面http.csrf().disable();的答案是唯一有效的方法。请找到参考:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-security-csrf

package com.broadridge.services.calendar2.security;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
@Configuration
public class ApiSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests().anyRequest().permitAll();
}
}

最新更新