【问题标题】:Disable CSRF in Java WebSecurityConfigurerAdapter. SPRING REST在 Java WebSecurityConfigurerAdapter 中禁用 CSRF。弹簧休息
【发布时间】:2019-07-09 13:22:54
【问题描述】:

我正在用 Spring 和 Tiles 做一个新的 web 应用程序。 到目前为止,我已经登录,创建/编辑用户工作。 现在我必须从带有 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();
    }
}

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

弹簧安全配置:

<?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 将如何工作,我可以让它使用另一个吗?

【问题讨论】:

    标签: java spring rest csrf


    【解决方案1】:

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

    &lt;http pattern="/rest/**" security="none" /&gt;

    然后是标准应用程序的另一个 &lt;http... &lt;/http&gt; 配置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      相关资源
      最近更新 更多