Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat CORS filter and Spring Security

I am using Tomcat 7. I've set up native CORS filtering as suggested in the official Tomcat documentation.

I've tested everything I could, but it isn't working. Given that I am using a vendor application that I know is using Spring Security filters, I'm wondering if there is a known bug here.

Here is what I'm talking about:

<!-- Spring Security START -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>
<!-- Spring Security END -->

I have added the following configurations to Web.xml:

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I know it is minimal, but it should work.

I've also previously tried a longer version:

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param>
    <init-param>
        <param-name>cors.support.credentials</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.preflight.maxage</param-name>
        <param-value>10</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

It didn't work either.

Does anyone have experience with setting up CORS filters with Spring Security?

like image 817
MaatDeamon Avatar asked Jul 23 '15 11:07

MaatDeamon


People also ask

What is CORS filter in spring?

Cross-Origin Resource Sharing (CORS) is a security concept that allows restricting the resources implemented in web browsers. It prevents the JavaScript code producing or consuming the requests against different origin.

How do you put a CORS filter in a spring boot?

In case you want to add it under a common security configuration: @EnableWebSecurity public class AppSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http. cors().

Is CORS enabled by default in spring boot?

No. You need to add @CrossOrigin annotation by yourself to get CORS Support in Spring. Enabling CORS (Cross-origin resource sharing) by default will be a serious security issue.


1 Answers

I had a similar configuration and the key problem was that the Authorization header is not allowed in Tomcat's defaults for the cors.allowed.headers initialisation parameters. So I had just to add it like this:

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Authorization,Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
        <!--         ^^^^^^^^^^^^^ -->
    </init-param>
</filter>
like image 147
splash Avatar answered Nov 09 '22 11:11

splash