Spring Security相同的URL不同的提供者

时间:2019-06-27 23:24:47

标签: java spring spring-boot spring-security

我的Spring Security中有2个不同的AuthenticationProvider,它们都是JWT,但来自应用程序的不同部分(因此需要2个不同的PRIVATE_KEYS)。 另外,我还有一条必须为“ permitAll()”的路由,因此,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上进行认证。

insiderInternalAuthenticationProviderapi/v1/reviews/token中唯一必须在/api/v1/reviews/token中进行身份验证的路由。所有其他路由(除了一条,我将在下面解释)都必须在insiderAuthenticationProvider中进行身份验证。

此外,提供者必须是互斥的,这意味着如果路由期望insiderAuthenticationProvider,则不能通过任何方式发送insiderInternalAuthenticationProvider的必要(甚至有效)令牌对它进行身份验证。

最后,必须可用的路由为/api/v1/token/(permitAll),因此这两个提供程序都不能挡路。

如何在我的配置中完成此操作?

谢谢!

0 个答案:

没有答案