Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 无效的CSRF令牌';空';在请求参数'_csrf&x27;或标题';X-CSRF-TOKEN';_Spring_Spring Security_Csrf_Csrf Protection - Fatal编程技术网

Spring 无效的CSRF令牌';空';在请求参数'_csrf&x27;或标题';X-CSRF-TOKEN';

Spring 无效的CSRF令牌';空';在请求参数'_csrf&x27;或标题';X-CSRF-TOKEN';,spring,spring-security,csrf,csrf-protection,Spring,Spring Security,Csrf,Csrf Protection,配置Spring Security 3.2后,\u csrf.token未绑定到请求或会话对象 这是spring安全配置: <http pattern="/login.jsp" security="none"/> <http> <intercept-url pattern="/**" access="ROLE_USER"/> <form-login login-page="/login.jsp" auth

配置Spring Security 3.2后,
\u csrf.token
未绑定到请求或会话对象

这是spring安全配置:

<http pattern="/login.jsp" security="none"/>

<http>
    <intercept-url pattern="/**" access="ROLE_USER"/>
    <form-login login-page="/login.jsp"
                authentication-failure-url="/login.jsp?error=1"
                default-target-url="/index.jsp"/>
    <logout/>
    <csrf />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="test" password="test" authorities="ROLE_USER/>
        </user-service>
    </authentication-provider>
</authentication-manager>
更新 在一些调试之后,请求对象从DelegatingFilterProxy中获得良好的形式,但在CoyoteAdapter的第469行中,它执行request.recycle();这会删除所有属性

我使用JDK1.7在Tomcat6.0.36、7.0.50中进行测试


我不理解这种行为,相反,如果有人向我指出使用CSRF的Spring Security 3.2的一些应用程序示例war的方向,这是可能的。

请查看我的工作示例应用程序,并与您的设置进行比较。

您是否应该添加到登录表单中

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 


正如Spring安全文档中所述,我认为csrf只适用于Spring表单

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

更改为
form:form
标记并查看它是否有效。

您的Spring应用程序中的CSRF(跨站点请求伪造)保护似乎已启用。实际上它是默认启用的

根据:

什么时候应该使用CSRF保护?我们建议使用CSRF 保护浏览器可以通过以下方式处理的任何请求: 普通用户。如果您仅创建由使用的服务 非浏览器客户端,您可能希望禁用CSRF保护

因此,要禁用它:

@Configuration
public class RestSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
  }
}
如果您希望保持CSRF保护处于启用状态,则必须在表单中包含
csrftoken
。您可以这样做:

<form .... >
  ....other fields here....
  <input type="hidden"  name="${_csrf.parameterName}"   value="${_csrf.token}"/>
</form>

……这里的其他字段。。。。
您甚至可以在表单的操作中包含CSRF令牌:

<form action="./upload?${_csrf.parameterName}=${_csrf.token}" method="post" enctype="multipart/form-data">


尝试将此:
更改为此:
。它应该禁用csfr。

如果您将应用
security=“none”
则不会生成csrf令牌。页面将不会通过安全筛选器。使用角色匿名

我没有详细说明,但它对我有用

 <http auto-config="true" use-expressions="true">
   <intercept-url pattern="/login.jsp" access="hasRole('ANONYMOUS')" />
   <!-- you configuration -->
   </http>

我以前也有同样的问题

您的配置使用security=“none”,因此无法生成\u csrf:

<http pattern="/login.jsp" security="none"/>

您可以为页面/login.jsp设置access=“IS\u AUTHENTICATED\u匿名”,替换上述配置:

<http>
    <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    <intercept-url pattern="/**" access="ROLE_USER"/>
    <form-login login-page="/login.jsp"
            authentication-failure-url="/login.jsp?error=1"
            default-target-url="/index.jsp"/>
    <logout/>
    <csrf />
</http>

使用thymeleaf,您可以添加:

<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>

禁用csrf的Spring文档:


在控制器中添加以下内容:

@RequestParam(value = "_csrf", required = false) String csrf
并在jsp页面上添加

<form:form modelAttribute="someName" action="someURI?${_csrf.parameterName}=${_csrf.token}

这两种解决方案对我都不起作用。在Spring形态中,唯一对我有效的是:

action=“./upload?${u csrf.parameterName}=${{u csrf.token}”

替换为:

action=“./upload?\u csrf=${\u csrf.token}”


(Spring5在java配置中启用了csrf)

您使用哪种Spring版本?这同样适用于我(但是在
spring security.xml
)与spring 4.0.0版(GA)、spring security 3.2.0版(GA)(尽管它与Struts 2.3.16集成。我没有单独尝试spring MVC)。但是,当请求是用于上载状态为403的文件的多部分时,它将失败。我正在努力寻找解决方案。Spring 3.2.6,Spring Security 3.2.0,CSRF,令牌添加到http请求对象,会话对象与请求线程相同,但是当它呈现出来时,jsp会删除所有属性,只留下一个属性…过滤器_applied@Tiny:您是否找到了多部分问题的解决方案?我遇到了完全相同的问题。@AlienBishop:是的,请查看答案(它使用弹簧和支柱的组合)。如果您只有SpringMVC,请查看答案。应该注意的是,
web.xml
中过滤器的顺序至关重要<必须在
springSecurityFilterChain
之前声明code>MultipartFilter
。希望有帮助。谢谢。我将降级到spring 3.2.6,我希望它在没有spring mvc的情况下工作。是的,它应该可以正常工作,因为我用spring 3.1.4上的现有应用程序创建了示例应用程序。很好,为了让它工作,降级不是解决方案bhaiya ji@manish您不需要使用spring表单:我使用的是security=none,转到您的答案解决了这个问题。很明显,ELEAF会自动添加csrf令牌。谢谢请不要复制现有的答案。这应该被接受作为答案,因为它不仅解释了该做什么,而且还应该做些什么来阻止这些错误。你也可以做<代码>。CSRF()。在上面的答案中,忽略ANDMatter(“/H2控制台/**”)<代码>避免通过查询参数样式。如果您这样做,您就是在公开公开令牌。
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

   @Override
   protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
   }
}
@RequestParam(value = "_csrf", required = false) String csrf
<form:form modelAttribute="someName" action="someURI?${_csrf.parameterName}=${_csrf.token}