【问题标题】:Consul health check pass by Spring security filterConsul 健康检查通过 Spring 安全过滤器
【发布时间】:2016-05-06 22:00:51
【问题描述】:

我使用 consul 作为服务发现/注册创建了 Spring 云应用程序。 我已将我的 spring 安全配置如下:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .exceptionHandling()
            .authenticationEntryPoint(myEntryPoint());
    http
            .authorizeRequests()
            .antMatchers("/images/**").permitAll()
            .antMatchers("/modules/**").permitAll()         
            .antMatchers("/vendor/**").permitAll()
            .antMatchers("/views/**").permitAll()
            .antMatchers("/index.html").permitAll()
            .regexMatchers("/health").permitAll()// consul check health
            .antMatchers("/api/**").authenticated()
            .antMatchers("/**").permitAll();

    http    // login configuration
            .addFilterAfter(springSecurityFilter(), BasicAuthenticationFilter.class);

    http    //logout configuration
            .logout()
            .logoutSuccessHandler(logoutHandler());

    http.csrf().disable();

}

通常,使用此过滤器 spring consul 健康检查不会通过此过滤器(公共访问)。

但是 consul 健康检查 consul 会通过过滤器。

如果我使用以下网址,我将被重定向到身份验证页面:

 https://localhost:8181/health

【问题讨论】:

  • 有什么原因为什么这个 Matcher 是正则表达式匹配器,而不是 ant 匹配器?
  • 并添加为什么复杂性?仅使用 antMatchers("/api/**).authenticated().antMatchers("/**").permitAll() 会产生相同的结果(/** 是全部,它也将涵盖所有先前提到的 URL,例如 /images/** 等)。
  • 当我起飞时 .antMatchers("/**").permitAll() 我有同样的结果
  • 你设置管理端口了吗? Spring boot 有文档说明如何禁用健康检查安全性。

标签: spring spring-security spring-cloud consul


【解决方案1】:

默认情况下来自spring-cloud-starter-consul-discovery 的领事使用/actuator/health 来检查健康状况。
使用 spring-security 时,应提供并允许该路径。

步骤:

  • 在 spring-security 中允许 /actuator/health,然后 consul 可以执行健康检查。

    例如

    httpSecurity.csrf().disable() 
        .authorizeRequests().antMatchers("/hello", "/authenticate", "/actuator/health")
        .permitAll().
    
  • 添加执行器依赖项,如果还没有的话。

  • 暴露端点

    例如

    management:
      endpoints:
        web:
          exposure:
            include: "*"
    

【讨论】:

    猜你喜欢
    • 2017-05-27
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2015-10-27
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多