2016-09-16 66 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}" /> 

現在它起作用了。