0

我有一个带有安全配置的 Spring Boot Web 应用程序,可以将所有未经授权的请求转发到 /login。我设置了一个不同于我的应用程序端口的 spring boot 管理端口。当我转到管理端口并尝试访问 /health 时,它会尝试将我发送到该端口上的 /login 并且我得到以下响应:

''' {"timestamp":1435680239995,"status":404,"error":"Not Found","message":"No message available","path":"/login"} '''

我发现了这个问题,但我无法让它在我的应用程序中工作: Spring Boot Management security works different with port set

在尝试设置单独的管理端口时,使这个非常基本的 Spring Security 配置与 Spring Boot 一起工作的正确方法是什么?

这是我的 spring 安全配置的相关部分:

```

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()       //temporary
        .authorizeRequests()
        .antMatchers("/public/**").permitAll()
        .antMatchers("/private*/**").access("hasRole('ADMIN')")
        .antMatchers("/**").access("hasRole('USER')");

    http
      .formLogin().failureUrl("/login?error")
      .defaultSuccessUrl("/")
      .loginPage("/login")
      .permitAll()
      .and()
      .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
      .permitAll();
}
}

```

谢谢!

更新:

我无法使上述解决方案起作用,但我通过将我的管理端点置于 Spring Security 认为的公共(permitAll)路线下找到了一种解决方法。然后将其暴露在不同的端口后面。这适用于我的目的,即能够在仅暴露给 ELB 的端口上向我的 ELB 公开健康检查。

management: port: 8081 context-path: /public security: enabled: false

4

1 回答 1

1

在您发布的另一个问题之后,我设法找到了我的解决方案,它与提供的基本相同。

因此,您将有 2 个类实现此 WebSecurityConfigurerAdapter 并比较这些请求路径并使每个请求都经过身份验证。确保使用@Order(0),因为会有冲突。

@Order(0)
@Configuration
public class ManagementSecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Autowired
  private ManagementServerProperties managementProperties;

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().formLogin().disable()
        .httpBasic()
        .authenticationEntryPoint(new NoPopupBasicAuthenticationEntryPoint())
        .and()
        .requestMatchers()
        .requestMatchers(new RequestMatcher() {
          @Override
          public boolean matches(HttpServletRequest request) {
            return managementProperties.getServlet().getContextPath().equals(request.getContextPath());
          }
        })
        .and()
        .authorizeRequests()
        .anyRequest().hasRole("ADMIN")
        .and()
        .sessionManagement().maximumSessions(1);
  }

}
于 2020-06-24T20:01:32.657 回答