2016-09-12 6 views
-1

私は個々のアスペクトをコメントアウトすると動作するように見えますが、Spring Securityのルールを組み合わせると動作しません。 regexMatcherまたはantMatcherではなく、ルールが組み合わせて適用されています。ここで Spring Security複数のURLルールセットが連携していません

は私の春のセキュリティクラスである:すべてのURLで

package com.driver.website.config; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.builders.WebSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.web.csrf.CsrfTokenRepository; 
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; 
import org.springframework.security.web.header.writers.StaticHeadersWriter; 
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter; 
import org.springframework.security.web.util.matcher.RequestMatcher; 

import javax.servlet.http.HttpServletRequest; 
import java.security.AccessControlContext; 

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

    @Value("${widget.headers.xframeoptions.domains.allowed}") 
    private String allowedXFrameOptions; 

    @Value("${widget.headers.origins.allowed}") 
    private String allowedOrigins; 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 

     http.exceptionHandling().accessDeniedPage("/login") 
       .and() 
       .formLogin().loginPage("/login").defaultSuccessUrl("/myaccount", true).permitAll() 
       .and() 
       .authorizeRequests() 
       .antMatchers("/**").permitAll(); 

     http.regexMatcher("^((?!(/widget|/assistedSearch)).)*$") 
       .headers().frameOptions().disable() 
       .regexMatcher("^((?!(/widget|/assistedSearch)).)*$") 
       .headers() 
       .xssProtection() 
       .contentTypeOptions() 
       .addHeaderWriter(new StaticHeadersWriter("X-FRAME-OPTIONS", "SAMEORIGIN")); 

     http.antMatcher("/widget") 
      .headers() 
      .frameOptions() 
      .disable() 
      .antMatcher("/widget") 
      .headers() 
      .addHeaderWriter(new StaticHeadersWriter("X-FRAME-OPTIONS", "ALLOW-FROM " + allowedXFrameOptions)); 

     http.requestMatchers().antMatchers("/assistedSearch", "/widget") 
      .and() 
      .headers() 
      .addHeaderWriter(new StaticHeadersWriter("Access-Control-Allow-Origin", allowedOrigins)) 
      .addHeaderWriter(new StaticHeadersWriter("Access-Control-Allow-Methods", "GET, POST")) 
      .addHeaderWriter(new StaticHeadersWriter("Access-Control-Allow-Headers", "Content-Type")); 

     // @formatter:on 
    } 
} 

ルールがあるべき...

  • ではなく、/ウィジェットと/ assistedSearch我々はSAMEORIGIN X-フレーム - 追加する必要がありますオプションは、我々はX-フレーム・オプションを追加する必要があります/widgetエンドポイントの場合
  • ヘッダ:ALLOW-FROMヘッダ
  • /widgetとについてのエンドポイントは、我々は、私は上記のように、私はその後、一斉に他の二つの作業をFor all urlsルールセットをコメントアウトした場合Access-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-Headersヘッダ

を追加しますが、ヘッダのFor all urlsルールアンコメントなしで表示されます。

これはなぜでしょうか? Spring Securityで複数のルールセットを追加し、既存のルールセットを新しいもので上書きするにはどうすればよいですか?

私は再び組み合わせて、それ自身の上で動作するように表示されますがない

http.antMatcher("/widget") 
    .headers() 
    .frameOptions() 
    .disable() 

を試してみました。

ありがとうございます!

+2

上書きルールは追加できません。また、最初の試合のみが適用されるので、最初の試合だけが勝つので、同じパターン(または可能性のある一致パターン)の複数のルールを使用することはできません。ルールは定義された順に処理されます。 '/ **'が最初に定義されているので、あなたの他のルールはほとんど役に立たないものです。参照ガイドの中では、 '/ **'が常にチェーン内の最後のものであるべきであることは、明らかに(そして正しくリコールすれば太字で)わかります。 –

+0

@ M.Deinum最後の宣言として 'すべてではなく/ widgetおよび/ assistedSearch'ルールセットを移動しました。それは、そのルールが正しく適用されますが、/ widgetおよび/ assistedSearchルールは正しく適用されないということです。 追加していると言っていることを理解しましたが、技術的には/ widgetと/ assistedSearchに1つのルールセットしか追加していないので、ヘッダーが何も表示されないということはなぜですか? –

+0

この質問は格下げされるのはかなりばかげている。それは有効なものです。ここでは相互排他的なルールセットを指定しています。相互排他的なルールセットを使用できない場合、なぜ正規表現の力を提供するのですか? 誰もがこの問題を回避するためのベストプラクティスを認識していますか?代わりにDSLを使用してインターセプタを処理するのではなく、インターセプタを指定する必要がありますか? –

答えて

2

あなたは、あなたの以前のmatcherを上書きHttpSecurity.html#antMatcherを参照してください。

antMatcher(String)を呼び出すと、mvcMatcher(String)}requestMatchers()antMatcher(String)regexMatcher(String)、およびrequestMatcher(RequestMatcher)の以前の呼び出しを上書きします。

HttpSecurity.html#regexMatcherregexMatcher(String)を起動

mvcMatcher(String)}requestMatchers()antMatcher(String)regexMatcher(String)、およびrequestMatcher(RequestMatcher)の以前の呼び出しを上書きします。

あなたがHttpSecurityの複数の構成を使用する場合は、Spring Security Reference次を参照してください。私たちは、複数の<http>ブロックを持つことができると同じように、複数のHttpSecurityインスタンスを設定することができます

を。鍵はWebSecurityConfigurationAdapterを何度も拡張することです。たとえば、次の例は、URLが/api/で始まる別の構成を持つ例です。

@EnableWebSecurity 
public class MultiHttpSecurityConfig { 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) { 1 
     auth 
      .inMemoryAuthentication() 
       .withUser("user").password("password").roles("USER").and() 
       .withUser("admin").password("password").roles("USER", "ADMIN"); 
    } 

    @Configuration 
    @Order(1)              2 
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .antMatcher("/api/**")        3 
       .authorizeRequests() 
        .anyRequest().hasRole("ADMIN") 
        .and() 
       .httpBasic(); 
     } 
    } 

    @Configuration             4 
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .authorizeRequests() 
        .anyRequest().authenticated() 
        .and() 
       .formLogin(); 
     } 
    } 
} 
+0

この回答に感謝します。私は自分のルールごとに静的クラスを実装し、それぞれに対して正しい@Orderを使ってそれらを適用しました。 Spring Securityがどのようにバックグラウンドで動作するかを頭で表現すると、すべて意味があります。 –

関連する問題