两个WebSecurityConfigurerAdapters无法一起使用

时间:2018-09-23 17:02:54

标签: spring-boot spring-security gmail-api spring-security-oauth2

我正在研究SaaS产品。基于jwt和令牌一起工作的产品的安全性,仅此而已,一切正常。现在,我想与OAuth2添加gmail api集成,并且在为OAuth2添加安全配置时遇到问题/误解。 (事实证明,使用一个WebSecurityConfigurerAdapter是不可能的)

我的第一个用于jwt的WebSecurityConfigurerAdapter(通常的身份验证/授权)是这样的:

  @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers(HttpMethod.POST, "/user/save").permitAll()
                .antMatchers(HttpMethod.POST, "/company/add").permitAll()
                .antMatchers("/myapp/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(getJWTAuthenticationFilter())
                .addFilter(getJWTAuthorizationFilter())
                .headers()
                .frameOptions().sameOrigin()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

用于Gmail的新WebSecurityConfigurerAdapter是:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/login/gmail")
                .authenticated()
                .anyRequest()
                .permitAll();
    }

再看第二个HttpSecurity,我仅创建一个授权的URL来登录Gmail。

第一种方法::Gmail HttpSecurity获得最小的@Order值,并检查端点是否为 / login / gmail (如果不是),则调用gmail身份验证,只需跳过基于JWT的主要HttpSecurity

缺点:是不可能的。如果我使用两个WebSecurityConfigurerAdapter终结点中的任意一个allowAll(),它将始终跳过两个实例。

第二种方法:我头脑中没有第二种方法。我不知道如何解决问题。

---- 已更新 ----

我尝试忽略JWT安全配置中的 / login / gmail ,但没有帮助。第一次过滤后,它完全跳过,没有进入gmail配置。

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

我打开@EnableWebSecurity的调试,这是stacktrace:

  

安全过滤器链:[WebAsyncManagerIntegrationFilter
  SecurityContextPersistenceFilter HeaderWriterFilter CorsFilter
  LogoutFilter OAuth2ClientAuthenticationProcessingFilter
  RequestCacheAwareFilter SecurityContextHolderAwareRequestFilter
  AnonymousAuthenticationFilter SessionManagementFilter
  ExceptionTranslationFilter FilterSecurityInterceptor]

这是我对gmail WebSecurityConfigurerAdapters的付款较低的时候。 如果我对它进行排序,那么jwt WebSecurityConfigurerAdapters将从链中消失。

简而言之,它永远无法协同工作。正确吗?

0 个答案:

没有答案