Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit f103b4e

Browse files
feat(#212): CSRF headers are now automatically added (cf. #253)
1 parent da45823 commit f103b4e

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

playground-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/playground/boot/PlaygroundController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.springframework.ui.Model;
77
import org.springframework.web.bind.annotation.GetMapping;
88

9+
import javax.servlet.http.HttpServletRequest;
10+
911
@Controller
1012
@RequiredArgsConstructor
1113
public class PlaygroundController {
@@ -31,14 +33,15 @@ public class PlaygroundController {
3133
private final ObjectMapper objectMapper;
3234

3335
@GetMapping("${graphql.playground.mapping:/playground}")
34-
public String playground(final Model model) {
36+
public String playground(final Model model, final HttpServletRequest request) {
3537
if (propertiesConfiguration.getPlayground().getCdn().isEnabled()) {
3638
setCdnUrls(model);
3739
} else {
3840
setLocalAssetUrls(model);
3941
}
4042
model.addAttribute("pageTitle", propertiesConfiguration.getPlayground().getPageTitle());
4143
model.addAttribute("properties", objectMapper.valueToTree(propertiesConfiguration.getPlayground()));
44+
model.addAttribute("_csrf", request.getAttribute("_csrf"));
4245
return "playground";
4346
}
4447

playground-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/playground/boot/properties/PlaygroundProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010

1111
@Data
12-
@JsonInclude(JsonInclude.Include.NON_NULL)
12+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
1313
public class PlaygroundProperties {
1414

1515
@NotEmpty

playground-spring-boot-autoconfigure/src/main/resources/templates/playground.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@
1919
</div>
2020

2121
<script th:inline="javascript">
22+
function addCsrfHeaderTo(properties, csrf) {
23+
if (!properties.headers) {
24+
properties.headers = {};
25+
}
26+
properties.headers[csrf.headerName] = csrf.token;
27+
}
28+
29+
let csrf = /*[[${_csrf}]]*/ null;
2230
let properties = /*[[${properties}]]*/ {};
31+
if (csrf) {
32+
addCsrfHeaderTo(properties, csrf);
33+
if (properties.tabs) {
34+
properties.tabs.forEach(tab => addCsrfHeaderTo(tab, csrf));
35+
}
36+
}
2337
properties.setTitle = false;
2438
window.addEventListener('load', function (event) {
2539
GraphQLPlayground.init(document.getElementById('root'), properties)

0 commit comments

Comments
 (0)