簡體   English   中英

Spring Boot HttpSecurity流利的api命令?

[英]Spring Boot HttpSecurity fluent api order?

Spring 2.0.3.RELEASE

目標:在除/actuator/health/actuator/info/ping (僅返回ResponseEntity.status(HttpStatus.NO_CONTENT).build()的自定義控制器)之外的所有終結點上實現Spring Boot Security(針對啟動程序的基本身份驗證)。

下面給我一個401 任何組合似乎都可以使我完全匿名訪問所有端點,也可以給所有用戶401

我在application.yml設置了spring.security.user.name...password ,它可以正常工作。

我已經實施了...

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(final HttpSecurity http) throws Exception {

        super.configure(http);

        // just trying to get health working for starters
        http.authorizeRequests().antMatchers("/actuator/health").permitAll()
            .anyRequest().authenticated()
            .and().formLogin().permitAll();
    }
}

下面的內容似乎僅限於Actuator的/health/info端點,但同時也開放了我的自定義/ping端點(不在此列表中)。

http.requestMatcher(EndpointRequest.to("health", "info"))
    .authorizeRequests().anyRequest().permitAll();

這個問題最終是Spring Tool Suite中的一個錯誤。 在Gradle項目中使用Boot Dashboard並不總是能夠獲得構建輸出。 似乎正在使用其他目錄,我無法弄清楚。

最終對我有用的HttpSecurity配置是:

@Override
protected void configure(final HttpSecurity http) throws Exception {
    // @formatter:off
    http.
        authorizeRequests().
            antMatchers("/ping", "/actuator/health", "/actuator/info", "/login").permitAll().
            anyRequest().authenticated().and().
        httpBasic().and().
        // CSRF tokens for API access
        csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    // @formatter:on
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM