【问题标题】:Spring Boot HttpSecurity fluent api order?Spring Boot HttpSecurity fluent api命令?
【发布时间】:2018-12-18 19:44:17
【问题描述】:

春季 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();

【问题讨论】:

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


【解决方案1】:

这个问题最终成为 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
}

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2014-12-25
    • 2021-05-02
    • 2020-02-11
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2017-10-18
    相关资源
    最近更新 更多