2016-04-01 9 views
1

私はスプリングブートの自動設定をよりよく理解しようとしています。特に、CORS要求を動作させるために、HTTP OPTIONS verbの認証を無効にするためにカスタムのスプリングセキュリティ設定を追加する必要があります。カスタムHttpSecurityコンフィグレーションでSpringBootWebSecurityConfigurationを拡張する

デフォルトでカスタム設定を行わないと、SpringBootWebSecurityConfigurationがSpringブートの自動設定によって読み込まれます。

私がしたいのは、この自動設定を引き続き使用することですが、追加のHTTP設定を追加することです。私はこれを試みた:

@Configuration 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    Logger logger = LogManager.getLogger (SecurityConfiguration.class); 
    @Override 
    protected void configure (HttpSecurity http) throws Exception { 
     logger.info ("--- ALLOW all HTTP OPTIONS Requests"); 
     http.authorizeRequests().antMatchers (HttpMethod.OPTIONS, "*//**").permitAll(); 
    } 
} 

しかし、これは例外として動作しません。 SpringBootWebSecurityConfigurationと上記のコードを使ってデバッグすると、configure-methodとsprings auto-configurationの両方が実行されていますが、私のhttpの設定が優先されるように見えます。

これは、自動設定がでのみ利用可能であることを意味します。種類の方法ですか?または、自動構成を使用できますか?それでもカスタムAntMatcherで拡張できますか?

このシナリオのベストプラクティスは何ですか?

+0

は、あなたのSecurityConfigurationクラスは、同様の@ EnableWebSecurity' 'でアノテートされていないでしょうか? –

答えて

0

新しいサーブレットフィルタを作成して、Springセキュリティフィルタの前に配置することができます。その後、

@Component("CORSFilter") 
public class CORSFilter implements Filter { 

    public void doFilter(
     ServletRequest req, 
     ServletResponse res, 
     FilterChain chain) throws IOException, ServletException { 

     HttpServletRequest request = (HttpServletRequest) req; 

     HttpServletResponse response = (HttpServletResponse) res; 
     response.setHeader("Access-Control-Allow-Credentials", "true"); 
     response.setHeader("Access-Control-Allow-Origin", "THE_HOST_YOU_WANT_TO_ALLOW_HERE"); 
     response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, PATCH, OPTIONS"); 
     response.setHeader("Access-Control-Max-Age", "3600"); 
     response.setHeader("Access-Control-Allow-Headers", 
       "origin, content-type, accept, x-requested-with, authorization"); 

     if (request.getMethod().equals("OPTIONS")) { 
      try { 
       response.getWriter().print("OK"); 
       response.getWriter().flush(); 
      } catch (IOException e) { 
       //... 
      } 
     } else { 
      chain.doFilter(req, res); 
     } 

    } 

    public void init(FilterConfig filterConfig) { 
     //... 
    } 

    public void destroy() { 
     //... 
    } 

} 

し、あなたのweb.xmlに以下を追加(またはあなたが唯一のJavaの設定を使用している場合は、あなたのWebInitializerで設定):たとえば

<filter> 
    <filter-name>CORSFilter</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    <init-param> 
     <param-name>contextAttribute</param-name> 
     <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.[YOUR_SERVLET_NAME]</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>CORSFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
+0

私はSpring Boot設定を拡張できるソリューションを探していました。あなたは代わりにSpring Bootが既に私のために世話しているフィルタマッピングを設定することを提案しています。 – lanoxx

0

あなたがそれを行うことはできません理由は、あなたがあります注釈@EnableWebSecurityが欠落してjavadocを見て:

* Add this annotation to an {@code @Configuration} class to have the Spring Security 
* configuration defined in any {@link WebSecurityConfigurer} or more likely by extending 
* the {@link WebSecurityConfigurerAdapter} base class and overriding individual methods: 
+0

これは理由ではありません。これにより、Springブートは独自の設定を使用しなくなります。私は構成を拡張し、完全に置き換えることはしません。 – lanoxx

関連する問題