私は個々のアスペクトをコメントアウトすると動作するように見えますが、Spring Securityのルールを組み合わせると動作しません。 regexMatcher
またはantMatcher
ではなく、ルールが組み合わせて適用されています。ここで Spring Security複数の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-Origin
、Access-Control-Allow-Methods
とAccess-Control-Allow-Headers
ヘッダ
を追加しますが、ヘッダのFor all urls
ルールアンコメントなしで表示されます。
これはなぜでしょうか? Spring Securityで複数のルールセットを追加し、既存のルールセットを新しいもので上書きするにはどうすればよいですか?
私は再び組み合わせて、それ自身の上で動作するように表示されますがない
http.antMatcher("/widget")
.headers()
.frameOptions()
.disable()
を試してみました。
ありがとうございます!
上書きルールは追加できません。また、最初の試合のみが適用されるので、最初の試合だけが勝つので、同じパターン(または可能性のある一致パターン)の複数のルールを使用することはできません。ルールは定義された順に処理されます。 '/ **'が最初に定義されているので、あなたの他のルールはほとんど役に立たないものです。参照ガイドの中では、 '/ **'が常にチェーン内の最後のものであるべきであることは、明らかに(そして正しくリコールすれば太字で)わかります。 –
@ M.Deinum最後の宣言として 'すべてではなく/ widgetおよび/ assistedSearch'ルールセットを移動しました。それは、そのルールが正しく適用されますが、/ widgetおよび/ assistedSearchルールは正しく適用されないということです。 追加していると言っていることを理解しましたが、技術的には/ widgetと/ assistedSearchに1つのルールセットしか追加していないので、ヘッダーが何も表示されないということはなぜですか? –
この質問は格下げされるのはかなりばかげている。それは有効なものです。ここでは相互排他的なルールセットを指定しています。相互排他的なルールセットを使用できない場合、なぜ正規表現の力を提供するのですか? 誰もがこの問題を回避するためのベストプラクティスを認識していますか?代わりにDSLを使用してインターセプタを処理するのではなく、インターセプタを指定する必要がありますか? –