2016-04-01 62 views
1

我想更好地瞭解Spring引導的自動配置。特別是我需要添加一些自定義彈簧安全配置來禁用HTTP OPTIONS動詞的身份驗證,以便使我的CORS請求正常工作。使用自定義HttpSecurity配置擴展SpringBootWebSecurityConfiguration

默認情況下,如果沒有任何自定義配置,則Spring Boot的自動配置會加載SpringBootWebSecurityConfiguration

我想要做的是繼續使用此自動配置,但添加一些額外的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自動配置都已執行,但看起來好像我的http-配置優先。

那麼這是否意味着自動配置只能在有或無種方式?或者我可以使用自動配置,但仍然使用一些自定義antMatcher擴展它?

這種情況下的最佳做法是什麼?

+0

應該不是你的SecurityConfiguration類與註釋'@ EnableWebSecurity'呢? –

回答

0

您可以創建一個新的servlet篩選器並將其放置在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文件(或在您的WebInitializer配置,如果你只使用Java的配置):

<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 Boot不使用自己的配置。我想擴展配置,而不是完全替換它。 – lanoxx

相關問題