Search code examples
javaspringrestcsrf

Disable CSRF in Java WebSecurityConfigurerAdapter. SPRING REST


Im doing a new webapp with Spring and Tiles. So far I have the login, create/edit users working. Now I have to start with Rest Controller for third apps with json. And I need to disable the csrf only on the rest URL's.

I tried using the XML from spring <csrf disabled="true"/> and it works but for the enthire app is there a way to do this config by path or the only way is to write it down by Java?:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
}

This is not working for me, every simple example I find is the same and seems to work to everyone except me, what am i doing wrong?

spring-security config:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

    <http auto-config="true">
<!--        <csrf disabled="true"/>  -->
        <intercept-url pattern="/" access="permitAll" />
        <intercept-url pattern="/welcome" access="hasRole('ROLE_ADMIN')"/>
<!--        <intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" access="ROLE_USER,ROLE_ADMIN"/> -->
        <intercept-url pattern="/administration/**" access="hasRole('ROLE_ADMIN')"/>
        <form-login login-page="/login" default-target-url="/" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password" />
        <logout logout-success-url="/login?logout" />
    </http>


    <authentication-manager>
        <authentication-provider ref="CustomAuthenticationProvider" />
    </authentication-manager>
    <beans:bean id="CustomAuthenticationProvider"
        class="net.eqtconsulting.webapp.service.CustomAuthenticationProvider">
    </beans:bean>
    <beans:bean id="encoder"
        class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <beans:constructor-arg name="strength" value="11" />
    </beans:bean>
</beans:beans>

Also my spring-mvc

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd 
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <mvc:annotation-driven/>

    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>

    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="cookieName" value="locale" />
    </bean>
    <!-- Application Message Bundle -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="WEB-INF/i18n/messages" />
        <property name="fallbackToSystemLocale" value="false" />
        <property name="useCodeAsDefaultMessage" value="true" />
        <property name="cacheSeconds" value="3000" />
    </bean>

    <mvc:annotation-driven >
        <mvc:argument-resolvers>
            <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
                <property name="maxPageSize" value="10"></property>
            </bean>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <context:component-scan base-package="com.javatpoint.controller" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
</beans>

Im also thinking about doing it by using 2 xml servlets, but how would the spring-security work, can I make it use an other one aswell?


Solution

  • Added this to my xml configuration and works just fine:

    <http pattern="/rest/**" security="none" />

    and then the other <http... </http> config for the standard app