<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CSRF &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/csrf/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:45:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>CSRF &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Spring Security: Invalid CSRF Token &#8216;null&#8217; was found on the request parameter &#8216;_csrf&#8217; or header &#8216;X-CSRF-TOKEN&#8217;</title>
		<link>https://myshittycode.com/2015/03/30/spring-security-invalid-csrf-token-null-was-found-on-the-request-parameter-_csrf-or-header-x-csrf-token/</link>
					<comments>https://myshittycode.com/2015/03/30/spring-security-invalid-csrf-token-null-was-found-on-the-request-parameter-_csrf-or-header-x-csrf-token/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 30 Mar 2015 20:37:25 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[CSRF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Security]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=689</guid>

					<description><![CDATA[<p>PROBLEM With Spring Security 4.x, the CSRF protection is enabled by default. You may disable it, but to be more aligned with OWASP and the industry security standard, it&#8217;s best to leave this setting the way it is. Learn more about CSRF attack&#8230; To prevent this attack, Spring Security 4.x requires you to attach a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/30/spring-security-invalid-csrf-token-null-was-found-on-the-request-parameter-_csrf-or-header-x-csrf-token/">Spring Security: Invalid CSRF Token &#8216;null&#8217; was found on the request parameter &#8216;_csrf&#8217; or header &#8216;X-CSRF-TOKEN&#8217;</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>With Spring Security 4.x, the CSRF protection is enabled by default. You may disable it, but to be more aligned with OWASP and the industry security standard, it&#8217;s best to leave this setting the way it is. <a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29" target="_blank" rel="noopener">Learn more about CSRF attack&#8230;</a></p>



<p>To prevent this attack, Spring Security 4.x requires you to attach a server-side generated CSRF token on any POST, PUT or DELETE calls&#8230; basically, actions that may modify the request state. Their argument for not attaching this token on GET is to prevent this token value from leaking out.</p>



<p>Further, you will require to call <b>POST /login</b> and <b>POST /logout</b> now. In the past, you can call <b>GET /j_spring_security_logout</b> without problem.</p>



<p>If you invoke POST, PUT or DELETE without this CSRF token, you will get a 403 error with this message: <b>&#8220;Invalid CSRF Token &#8216;null&#8217; was found on the request parameter &#8216;_csrf&#8217; or header &#8216;X-CSRF-TOKEN&#8217;.&#8221;</b>.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To obtain this CSRF token, add this Spring Security custom tag to the JSP file:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [4]; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
	&lt;head&gt;
	    &lt;sec:csrfMetaTags/&gt;
	&lt;/head&gt;
	&lt;body&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre></div>


<p>The rendered HTML looks like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [4,5,6]; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html class=&quot;no-js&quot;&gt;
	&lt;head&gt;
	    &lt;meta name=&quot;_csrf_parameter&quot; content=&quot;_csrf&quot; /&gt;
	    &lt;meta name=&quot;_csrf_header&quot; content=&quot;X-CSRF-TOKEN&quot; /&gt;
	    &lt;meta name=&quot;_csrf&quot; content=&quot;e62835df-f1a0-49ea-bce7-bf96f998119c&quot; /&gt;
	&lt;/head&gt;
	&lt;body&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre></div>


<p>Finally, set the request header before making the AJAX call:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
var header = $(&quot;meta&#x5B;name=&#039;_csrf_header&#039;]&quot;).attr(&quot;content&quot;);
var token = $(&quot;meta&#x5B;name=&#039;_csrf&#039;]&quot;).attr(&quot;content&quot;);

$.ajax({
    url: &#039;/test&#039;,
    type: &#039;POST&#039;,
    beforeSend: function(xhr){
        xhr.setRequestHeader(header, token);
    },
    success: function(data) {
        console.log(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status + &quot;: &quot; + thrownError);
    }
});
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/30/spring-security-invalid-csrf-token-null-was-found-on-the-request-parameter-_csrf-or-header-x-csrf-token/">Spring Security: Invalid CSRF Token &#8216;null&#8217; was found on the request parameter &#8216;_csrf&#8217; or header &#8216;X-CSRF-TOKEN&#8217;</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/03/30/spring-security-invalid-csrf-token-null-was-found-on-the-request-parameter-_csrf-or-header-x-csrf-token/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">689</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 52/67 queries in 0.038 seconds using Disk

Served from: myshittycode.com @ 2026-02-19 19:10:28 by W3 Total Cache
-->