在 Java WebSecurityConfigurerAdapter 中禁用 CSRF。 SPRING 休息

Disable CSRF in Java WebSecurityConfigurerAdapter. SPRING REST

我正在使用 Spring 和 Tiles 制作一个新的网络应用程序。 到目前为止,我已经登录,create/edit 用户正在工作。 现在我必须从 json 的第三个应用程序的 Rest Controller 开始。 我只需要在其余 URL 上禁用 csrf。

我尝试使用 spring <csrf disabled="true"/> 中的 XML 并且它有效但是对于整个应用程序有没有办法通过路径进行此配置或者唯一的方法是编写它下降 Java?:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


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

这对我不起作用,我发现的每个简单示例都是相同的,而且似乎对除我以外的所有人都有效,我做错了什么?

spring-安全配置:

<?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>

还有我的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>

我也在考虑使用 2 个 xml servlet 来实现它,但是 spring-security 是如何工作的,我可以让它也使用另一个吗?

将此添加到我的 xml 配置中并且工作正常:

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

然后是标准应用的另一个 <http... </http> 配置