【问题标题】:spring-boot-actuator disable csrfspring-boot-actuator 禁用 csrf
【发布时间】:2016-06-19 18:26:20
【问题描述】:

我使用了具有不同端口的 spring-boot-actuator,如下所示

server.port=8080
management.port=8989

在应用程序中,我想使用enable-csrf=true,但我不想在执行器端口中使用csrf。因为我想对 jolokia 使用批量 POST 请求。

只排除/actuator 不聪明。

http.csrf().ignoringAntMatchers("/actuator/**");

喜欢以下属性对我有好处(bt management.security.enable-csrf 不存在)。

security.enable-csrf=true
management.security.enable-csrf=false

有什么好的解决办法吗?

【问题讨论】:

标签: spring-security csrf spring-boot-actuator


【解决方案1】:

由于您有不同的管理端口,您可以简单地为此禁用 CSRF:

@Configuration
public class MySecurityConfiguration extends WebSecurityConfigurerAdapter {

    private static RequestMatcher allOf(RequestMatcher... requestMatchers) {
        return new AndRequestMatcher(requestMatchers);
    }

    private static RequestMatcher not(RequestMatcher requestMatcher) {
        return new NegatedRequestMatcher(requestMatcher);
    }

    private final ManagementServerProperties managementServerProperties;

    public MySecurityConfiguration(ManagementServerProperties managementServerProperties) {
        this.managementServerProperties = Objects.requireNonNull(managementServerProperties);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().requireCsrfProtectionMatcher(
                allOf(CsrfFilter.DEFAULT_CSRF_MATCHER, not(accessingManagementPort())));
        // other configuration
    }

    private RequestMatcher accessingManagementPort() {
        return httpServletRequest -> httpServletRequest.getLocalPort() == managementServerProperties.getPort();
    }

}

【讨论】:

    猜你喜欢
    • 2017-02-21
    • 2016-05-03
    • 2018-07-05
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多