Spring Security - множественная аутентификация не работает - PullRequest
0 голосов
/ 09 июля 2020

Благодаря помощи здесь я создал фильтр для перехвата данных shibboleth для аутентификации пользователя.

Это отлично работает (мой фильтр называется независимо от конечной точки):

@Configuration
@EnableWebSecurity
@Import(ShibbolethFilterRegistrar.class)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private ShibbolethAuthFilter shibbolethAuthFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(shibbolethAuthFilter, AbstractPreAuthenticatedProcessingFilter.class)
        .authorizeRequests()
        .antMatchers("/url1/**").hasAuthority(Permission.AUTHORITY1.toString())
        .antMatchers("/url2/**").hasAuthority(Permission.AUTHORITY2.toString())
        .anyRequest().hasAuthority(Permission.AUTHORITY3.toString())
        .and().csrf().disable();
        http.authorizeRequests();
        http.headers().frameOptions().sameOrigin().cacheControl().disable();
    }

    ...
}

Теперь я пытаюсь использовать аутентификацию http basi c для одной из моих конечных точек и использовать фильтрацию shibboleth для всех других конечных точек.

Что-то вроде этого не работает (я могу получить доступ к конечной точке '/' без фильтрация и без http basi c аутентификации, как если бы не было безопасности):

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Configuration
    @Order(1)
    public static class FirstSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
            .authorizeRequests()
            .antMatchers("/url1/**").hasAuthority(Permission.AUTHORITY1.toString())
            .and().httpBasic()
            .realmName("myApp")
            .and().csrf().disable();
            http.authorizeRequests();
            http.headers().frameOptions().sameOrigin().cacheControl().disable();
        }
    }

    @Configuration
    @Order(2)
    @Import(ShibbolethFilterRegistrar.class)
    public static class OtherSecurityConfiguration extends WebSecurityConfigurerAdapter {
        @Autowired
        private ShibbolethAuthFilter shibbolethAuthFilter;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.addFilterBefore(shibbolethAuthFilter, AbstractPreAuthenticatedProcessingFilter.class)
            .authorizeRequests()
            .antMatchers("/url2/**").hasAuthority(Permission.AUTHORITY2.toString())
            .anyRequest().hasAuthority(Permission.AUTHORITY3.toString())
            .and().csrf().disable();
            http.authorizeRequests();
            http.headers().frameOptions().sameOrigin().cacheControl().disable();
        }
    }
}

Я надеялся, что ".anyRequest ()", который связан с конфигурацией с фильтром, позволит использовать фильтр, но мой фильтр не вызывается, и страница отображается без какой-либо защиты.

Что не так?

Заранее спасибо за вашу помощь.

Изменить:

Я пробовал это, и похоже, что это работает:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Configuration
    @Order(1)
    public static class FirstSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .antMatcher("/url1/**")
        .authorizeRequests()
        .anyRequest().hasAuthority(Permission.AUTHORITY1.toString())
        .and().httpBasic()
        .realmName("myApp")
        .and().csrf().disable();
        http.authorizeRequests();
        http.headers().frameOptions().sameOrigin().cacheControl().disable();
    }
    }

    @Configuration
    @Order(2)
    @Import(ShibbolethFilterRegistrar.class)
    public static class OtherSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private ShibbolethAuthFilter shibbolethAuthFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .antMatcher("/**")
        .addFilterBefore(shibbolethAuthFilter, AbstractPreAuthenticatedProcessingFilter.class)
        .authorizeRequests()
        .antMatchers("/url2/**").hasAuthority(Permission.AUTHORITY2.toString())
        .anyRequest().hasAuthority(Permission.AUTHORITY3.toString())
        .and().csrf().disable();
        http.authorizeRequests();
        http.headers().frameOptions().sameOrigin().cacheControl().disable();
    }
}

Фактически, Spring Security применяет первую конфигурацию только к конечной точке "/trigger/**". Если конечная точка не соответствует "/trigger/**" (или чему-то еще), тогда рассматривается вторая конфигурация.

...