You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<a href="https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection">Spring Security Official Documentation: When to use CSRF protection</a><br/>
<description><p>Methods annotated with <code>RequestMapping</code> are by default mapped to all the HTTP request methods.
1124
+
However, Spring Security's CSRF protection is not enabled by default
1125
+
for the HTTP request methods <code>GET</code>, <code>HEAD</code>, <code>TRACE</code>, and <code>OPTIONS</code>
1126
+
(as this could cause the tokens to be leaked).
1127
+
Therefore, state-changing methods annotated with <code>RequestMapping</code> and not narrowing the mapping
1128
+
to the HTTP request methods <code>POST</code>, <code>PUT</code>, <code>DELETE</code>, or <code>PATCH</code>
1129
+
are vulnerable to CSRF attacks.</p>
1130
+
<p>
1131
+
<b>Vulnerable Code:</b><br/>
1132
+
<pre>@Controller
1133
+
public class UnsafeController {
1134
+
1135
+
@RequestMapping("/path")
1136
+
public void writeData() {
1137
+
// State-changing operations performed within this method.
1138
+
}
1139
+
}</pre>
1140
+
</p>
1141
+
<p>
1142
+
<b>Solution (Spring Framework 4.3 and later):</b><br/>
1143
+
<pre>@Controller
1144
+
public class SafeController {
1145
+
1146
+
/**
1147
+
* For methods without side-effects use @GetMapping.
1148
+
*/
1149
+
@GetMapping("/path")
1150
+
public String readData() {
1151
+
// No state-changing operations performed within this method.
1152
+
return "";
1153
+
}
1154
+
1155
+
/**
1156
+
* For state-changing methods use either @PostMapping, @PutMapping, @DeleteMapping, or @PatchMapping.
1157
+
*/
1158
+
@PostMapping("/path")
1159
+
public void writeData() {
1160
+
// State-changing operations performed within this method.
1161
+
}
1162
+
}</pre>
1163
+
</p>
1164
+
<p>
1165
+
<b>Solution (Before Spring Framework 4.3):</b><br/>
1166
+
<pre>@Controller
1167
+
public class SafeController {
1168
+
1169
+
/**
1170
+
* For methods without side-effects use either
1171
+
* RequestMethod.GET, RequestMethod.HEAD, RequestMethod.TRACE, or RequestMethod.OPTIONS.
0 commit comments