2016-09-16 67 views
0

我很抱歉下面的HTML混乱的语法。当我启用http.authorizeRequest() csrf时,我不断收到403错误。当然,如果我有csrf.disable(),一切正常。为什么Spring csrf给我一个403网络错误?

我的理解是,<form:form>标签会自动使用CSRF标记,因为下面的URL停止工作,所以必须这样做。

有人能告诉我这是什么,我不明白?

NetworkError: 403 Forbidden - http://localhost:8080/AssetCore/createGuideline/

我的配置类:

@SuppressWarnings("deprecation") 
@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter{ 
    private final Logger log = Logger.getLogger (this.getClass()); 
    /** 
    * This class gets called during startup. 
    */ 

    /** 
    * Configure http security. 
    */ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     /** 
     * This method gets called when the app starts up. 
     * I believe that all the patterns for the MVC and rest calls will need to go here. 
     */ 
     log.info("configure(): called to set up authorizedRequest pattern matching."); 
     http 
      .logout().logoutSuccessUrl("/login?loggedout=true").invalidateHttpSession(true).deleteCookies("JSESSIONID") 
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout")); 
     http.authorizeRequests() 
      .antMatchers(HttpMethod.PUT, "/assessment/**").permitAll() 
      .antMatchers("/createGuideline/**",).permitAll()  
    } 

的JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> 
<%@ taglib prefix="ark" tagdir="/WEB-INF/tags" %> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta http-equiv="content-type" 
      content="text/html; charset=windows-1252"> 
     <meta charset="utf-8"> 
     <title>ASSET Core - PCC</title> 
     <jsp:include page="ourCSSandJS.jsp" /> 
     <link rel="stylesheet" href="/AssetCore/resources/css/pccStyle.css"> 
     <link rel="stylesheet" href="/AssetCore/resources/css/scrollbar.css"> 
     <link rel="stylesheet" href="/AssetCore/resources/css/mcp_style.css"> 
     <script>var storedFormatExtraArgs = [];</script> 
     <script src="/AssetCore/resources/js/valid/pcc.js"></script> 
     <script src="/AssetCore/resources/js/controlFormatting.js"></script> 
     <script src="/AssetCore/resources/js/valid/controlCard.js"></script> 
     <script src="/AssetCore/resources/js/thirdParty/jquery.scrollbar.min.js"></script> 
     <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>  
    </head> 
    <body>`      <form:form id="customGuide" method="POST" 
          action="/AssetCore/createGuideline"> 
          <fieldset> 
           <legend id="createGuideline">Create Guideline</legend> 
           <p> 
            <label for="gName">Name</label><br /> <input type="text" 
             id="gName" name="gName" /> 
           </p>` 
+0

乍一看,它应该工作。在Chrome中试用它,并在网络检查器中查看csrf标记是否在表单文章中。 – Taylor

+0

我不认为这是。这是我的了:请求URL:http://本地主机:8080/AssetCore/createGuideline/ 请求方法:POST 状态代码:403禁止 远程地址:[:: 1]:8080 响应头 查看源 Cache-Control:no-cache,no-store,max-age = 0,必须重新验证 Content-Language:en-US Content-Length:2106 Content-Type:text/html; charset = ISO-8859- 1个 日期:星期五,2016年9月16日十九时14分44秒GMT 过期:0 杂注:无缓存 服务器:Apache-狼/ 1.1 X-Content-Type的选项:nosniff X框选项: DENY X-XSS-Protection:1; mode = block 请求头文件 – DenisMP

+1

这就是响应头文件(我认为),除非我误解了它将在请求正文中的文档。 – Taylor

回答

0

好了,所以Spring文档是不正确的,至少不是我所看到的。我实际上在<form:form>标签中添加了以下内容

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

现在它起作用了。