Spring Security же URL разных провайдеров - PullRequest
0 голосов
/ 28 июня 2019

У меня в Spring Security 2 разных AuthenticationProvider с, оба JWT, но из разных частей приложения (следовательно, нужно 2 разных PRIVATE_KEYS). Кроме того, у меня есть другой маршрут, который должен быть «allowAll ()», поэтому ни один из AuthenticationProvider не будет блокировать любой запрос к этому маршруту.

Моя конфигурация выглядит так:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class AuthenticationConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(final WebSecurity web) {
        web.ignoring().antMatchers(
                "/actuator/**",
                "/api/v1/reviews/pending",
                "/v2/api-docs",
                "/configuration/ui",
                "/swagger-resources",
                "/configuration/security",
                "/swagger-ui.html",
                "/webjars/**");
    }

    @Configuration
    @Order(2)
    public static class JwtSecurityConfig extends WebSecurityConfigurerAdapter {

        @Autowired
        private InsiderAuthenticationProvider insiderAuthenticationProvider;

        @Override
        protected void configure(final HttpSecurity http) throws Exception {
            http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().authorizeRequests().antMatchers("/api/v1/**").authenticated()
                .and().csrf().disable().anonymous().disable()
                .addFilterBefore(new AuthenticationFilter(), BasicAuthenticationFilter.class);
        }

        @Override
        public void configure(final AuthenticationManagerBuilder authentication) {
            authentication.authenticationProvider(insiderAuthenticationProvider);
        }

    }

    @Configuration
    @Order(1)
    public static class JwtInternalSecurityConfig extends WebSecurityConfigurerAdapter {

        @Autowired
        private InsiderInternalAuthenticationProvider insiderInternalAuthenticationProvider;

        @Override
        protected void configure(final HttpSecurity http) throws Exception {
            http.sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and().authorizeRequests().antMatchers("/api/v1/reviews/*/token").authenticated()
                    .and().authorizeRequests().antMatchers(HttpMethod.PUT, "/api/v1/reviews/token").authenticated()
                    .and().csrf().disable().anonymous().disable()
                    .addFilterBefore(new AuthenticationFilter(), BasicAuthenticationFilter.class);
        }

        @Override
        public void configure(final AuthenticationManagerBuilder authentication) {
            authentication.authenticationProvider(insiderInternalAuthenticationProvider);
        }
    }
}

Проблема в том, что /api/v1/reviews/{type}/pending должен быть аутентифицирован в insiderAuthenticationProvider, а не в insiderInternalAuthenticationProvider.

В insiderInternalAuthenticationProvider должны быть авторизованы только 2 маршрута: api/v1/reviews/token и /api/v1/reviews/token. Все остальные маршруты (кроме одного, я объясню ниже) должны быть авторизованы в insiderAuthenticationProvider.

Кроме того, поставщики должны быть взаимоисключающими, то есть, если маршрут ожидает insiderAuthenticationProvider, он не может быть аутентифицирован любым способом, посылая необходимый (даже действительный) токен insiderInternalAuthenticationProvider.

Наконец, должен быть доступен маршрут /api/v1/token/ (allowAll), поэтому ни один из 2 провайдеров не должен мешать ему.

Как мне это сделать в моей конфигурации?

Спасибо!

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