2015-09-17 17 views
1

我在將Spring Boot從1.1.12升級到1.2.5時遇到問題,但在1.2.x的所有版本中都有相同的問題。 Actuator提供的端點/health現在將401未授權返回到曾經工作的集成測試。升級依賴關係時沒有代碼發生變化。升級到引導1.2後無法訪問Spring Boot執行器運行狀況端點

這裏的測試用例:

@Test 
public void testNoUserForStatusEndpoint() throws Exception { 
    HttpEntity<String> entity = new HttpEntity<>(null, headers); 
    ResponseEntity<String> response = template 
      .exchange(base + "/health", HttpMethod.GET, entity, String.class); 
    assertEquals(HttpStatus.OK, response.getStatusCode()); 
    assertEquals("{\"status\":\"UP\"}", response.getBody()); 
} 

我希望看到的基本"UP"狀態,但沒有進一步的細節,因爲用戶是匿名的,未經過身份驗證。

在application.properties中設置management.security.enabled=false會導致端點返回完整的健康信息。這是不可取的。

設置endpoints.health.sensitive=false什麼都不做。

安全配置沒有改變。它基於Apache終止和證書白名單,這也沒有改變。

@Override 
public void configure(HttpSecurity http) throws Exception { 
    http.csrf().disable(); 
    http.addFilterBefore(new CustomAuthenticationFilter(authenticationManager(), Environment.getUserWhitelist()), BasicAuthenticationFilter.class); 
} 

我該如何通過測試?

更新:

原本在application.properties中定義的相關設定只有endpoints.health.enabled=true

management.health.status.order=UP,DOWN,OUT_OF_SERVICE,UNKNOWN添加到application.properties沒有區別。

使用所有這三個屬性導致401(不工作):

endpoints.health.enabled=true 
endpoints.health.sensitive=false 
management.health.status.order=UP,DOWN,OUT_OF_SERVICE,UNKNOWN 

主要類只是一個剝離下來的春天啓動的應用程序啓動:

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

我曾嘗試加入http.authorizeRequests().antMatchers("/health**").permitAll();添加到上面詳述的安全配置方法的第一行。這沒有什麼區別。

根據Spring Boot issue 2120,使用自定義安全性時將忽略endpoints.health.sensitive屬性。我在文檔中沒有發現這一點。

+0

類似[這個問題](http://stackoverflow.com/questions/32121635/how-to-re-enable-anonymous-access-to-spring -boot-health-endpoint),但安全細節不同。 – IanGilham

回答

2

我已經找到了解決辦法。

@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)添加到我的自定義身份驗證篩選器(在問題中稱爲CustomAuthenticationFilter)會更改定義安全篩選器bean的語義。

如果沒有註釋,Spring Boot假定我想覆蓋所有的過濾器,並且單獨使用我的自定義過濾器。

通過註釋,Spring Boot將我的過濾器添加到預定義的過濾器列表中。這允許/health端點再次工作。詳細信息請參閱Spring Boot issue 2120

+0

你是什麼意思「對自定義認證過濾器」。哪個對象?我有同樣的問題。請參閱:http://stackoverflow.com/questions/32957553/spring-boot-request-endpoints-return-404?noredirect=1#comment53757469_32957553 – emoleumassi

+0

@emoleumassi它表示用於身份驗證的'javax.servlet.Filter'實例。我爲此使用了一個自定義類。 – IanGilham

+0

hummm。你能否爲我的問題提供一些建議:-(。謝謝你的幫助 – emoleumassi

0

請嘗試這種在application.properties或application.yml: -

management.health.status.order=UP,DOWN,OUT_OF_SERVICE,UNKNOWN 
+0

謝謝。這沒有什麼區別。 – IanGilham

+0

請嘗試以下操作: - endpoints.health.enabled = true&endpoints.health.sensitive = false – Avis

+0

我已經這樣做了。在我的測試案例中,我仍然收到了401迴應。 – IanGilham

3

或者添加management.security.enabled=false 到application.properties

相關問題