Безопасность Spring Boot 2 - токен с предварительной аутентификацией - проверка работоспособности - PullRequest
0 голосов
/ 10 июля 2019

HealthCheck моего Spring Boot Actuator заблокирован из-за отсутствия токена (pre_authenticated).

Доступно много ответов, НО этот вопрос мешает безопасности, прошедшей предварительную проверку подлинности. Насколько я искал, это НЕ дубликат .

Как я могу разрешить проверку работоспособности в предварительно аутентифицированной среде безопасности?

У меня также вопрос, нужно ли мне больше настроек (например, в application.properties)?

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
        provider.setPreAuthenticatedUserDetailsService(new XyzPreAuthenticatedGrantedAuthoritiesUserDetailsService());
        auth.authenticationProvider(provider);
    }

    // Try-1, see below
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.addFilterBefore(xyzTokenRequestHeaderAuthenticationFilter(), RequestHeaderAuthenticationFilter.class)
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/actuator/**").permitAll()
        .anyRequest().authenticated();
    }

    @Bean
    public XyzTokenRequestHeaderAuthenticationFilter xyzTokenRequestHeaderAuthenticationFilter() throws Exception {
        XyzTokenRequestHeaderAuthenticationFilter filter = new XyzTokenRequestHeaderAuthenticationFilter();
        filter.setAuthenticationManager(authenticationManager());
        return filter;
    }
}

Моя вторая попытка была:

@Override
protected void configure(HttpSecurity http) throws Exception {
  http.addFilterBefore(xyzTokenRequestHeaderAuthenticationFilter(), RequestHeaderAuthenticationFilter.class)
    .csrf().disable()
    .authorizeRequests()
    .antMatchers("/actuator/**").permitAll();
}

Ответы [ 2 ]

2 голосов
/ 10 июля 2019

Попробуйте добавить .ignoring() и добавить @EnableGlobalMethodSecurity(prePostEnabled = true), @Configuration в классе

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{

 @Override
    public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
            .antMatchers("/actuator/**");
    }

     @Override
        protected void configure(HttpSecurity http) throws Exception {
          http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/actuator/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilterBefore(xyzTokenRequestHeaderAuthenticationFilter(), RequestHeaderAuthenticationFilter.class);
        }

}
1 голос
/ 11 июля 2019

Кажется, проблема в вашей реализации XyzTokenRequestHeaderAuthenticationFilter. Если вы написали это, расширив RequestHeaderAuthenticationFilter , то для свойства exceptionIfHeaderMissing необходимо установить значение false.

Если вы не расширили этот базовый класс предварительной аутентификации Spring Security, вам нужно показать реализацию.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...