领事健康检查通过Spring安全过滤器

时间:2016-01-29 08:26:57

标签: spring spring-security spring-cloud consul

我使用consul作为服务发现/注册表创建了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();

}

通常情况下,使用此过滤器弹簧检查健康检查不会通过此过滤器(公共访问)。

但是领事健康检查领事通过过滤器。

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

 https://localhost:8181/health

1 个答案:

答案 0 :(得分:0)

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

步骤:

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

    例如

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

  • 暴露端点

    例如

    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
相关问题