Skip to content

Commit e057063

Browse files
committed
Changes report: Merge pull request #1501 from GandalfTheBlack16/master
Add option to get the CSRF token from the Session Storage
1 parent 8251769 commit e057063

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/SwaggerUiConfigProperties.java

+46
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ public static class Csrf {
149149
*/
150150
private boolean useLocalStorage;
151151

152+
/**
153+
* Use Session storage.
154+
*/
155+
private boolean useSessionStorage;
156+
152157
/**
153158
* The Cookie name.
154159
*/
@@ -159,6 +164,11 @@ public static class Csrf {
159164
*/
160165
private String localStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;
161166

167+
/**
168+
* The Session storage key.
169+
*/
170+
private String sessionStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;
171+
162172
/**
163173
* The Header name.
164174
*/
@@ -191,6 +201,15 @@ public boolean isUseLocalStorage() {
191201
return useLocalStorage;
192202
}
193203

204+
/**
205+
* Use Session storage boolean.
206+
*
207+
* @return the boolean
208+
*/
209+
public boolean isUseSessionStorage() {
210+
return useSessionStorage;
211+
}
212+
194213
/**
195214
* Sets useLocalStorage.
196215
*
@@ -200,6 +219,15 @@ public void setUseLocalStorage(boolean useLocalStorage) {
200219
this.useLocalStorage = useLocalStorage;
201220
}
202221

222+
/**
223+
* Sets useSessionStorage.
224+
*
225+
* @param useSessionStorage the use local storage
226+
*/
227+
public void setUseSessionStorage(boolean useSessionStorage) {
228+
this.useSessionStorage = useSessionStorage;
229+
}
230+
203231
/**
204232
* Gets cookie name.
205233
*
@@ -227,6 +255,15 @@ public String getLocalStorageKey() {
227255
return localStorageKey;
228256
}
229257

258+
/**
259+
* Gets session storage key.
260+
*
261+
* @return the cookie name
262+
*/
263+
public String getSessionStorageKey() {
264+
return sessionStorageKey;
265+
}
266+
230267
/**
231268
* Sets local storage key.
232269
*
@@ -236,6 +273,15 @@ public void setLocalStorageKey(String localStorageKey) {
236273
this.localStorageKey = localStorageKey;
237274
}
238275

276+
/**
277+
* Sets local storage key.
278+
*
279+
* @param sessionStorageKey the local storage key
280+
*/
281+
public void setSessionStorageKey(String sessionStorageKey) {
282+
this.sessionStorageKey = sessionStorageKey;
283+
}
284+
239285
/**
240286
* Gets header name.
241287
*

springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerIndexTransformer.java

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
*
33
* *
4+
* * * Copyright 2019-2020 the original author or authors.
45
* * *
56
* * * * Copyright 2019-2022 the original author or authors.
67
* * * *
@@ -147,6 +148,8 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept
147148
if (swaggerUiConfig.isCsrfEnabled()) {
148149
if (swaggerUiConfig.getCsrf().isUseLocalStorage())
149150
html = addCSRFLocalStorage(html);
151+
else if (swaggerUiConfig.getCsrf().isUseSessionStorage())
152+
html = addCSRFSessionStorage(html);
150153
else
151154
html = addCSRF(html);
152155
}
@@ -228,16 +231,40 @@ protected String addCSRF(String html) {
228231
protected String addCSRFLocalStorage(String html) {
229232
StringBuilder stringBuilder = new StringBuilder();
230233
stringBuilder.append("requestInterceptor: (request) => {\n");
231-
stringBuilder.append("t\t\tconst value = window.localStorage.getItem('");
234+
stringBuilder.append("\t\t\tconst value = window.localStorage.getItem('");
232235
stringBuilder.append(swaggerUiConfig.getCsrf().getLocalStorageKey() + "');\n");
233-
stringBuilder.append("t\t\tconst currentURL = new URL(document.URL);\n");
234-
stringBuilder.append("t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
235-
stringBuilder.append("t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
236-
stringBuilder.append("t\t\tif (isSameOrigin) ");
236+
stringBuilder.append("\t\t\tconst currentURL = new URL(document.URL);\n");
237+
stringBuilder.append("\t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
238+
stringBuilder.append("\t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
239+
stringBuilder.append("\t\t\tif (isSameOrigin) ");
237240
stringBuilder.append("request.headers['");
238241
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
239242
stringBuilder.append("'] = value;\n");
240-
stringBuilder.append("t\t\treturn request;\n");
243+
stringBuilder.append("\t\t\treturn request;\n");
244+
stringBuilder.append("\t\t},\n");
245+
stringBuilder.append("\t\t" + PRESETS);
246+
return html.replace(PRESETS, stringBuilder.toString());
247+
}
248+
249+
/**
250+
* Add csrf string from Session storage.
251+
*
252+
* @param html the html
253+
* @return the string
254+
*/
255+
protected String addCSRFSessionStorage(String html) {
256+
StringBuilder stringBuilder = new StringBuilder();
257+
stringBuilder.append("requestInterceptor: (request) => {\n");
258+
stringBuilder.append("\t\t\tconst value = window.sessionStorage.getItem('");
259+
stringBuilder.append(swaggerUiConfig.getCsrf().getSessionStorageKey() + "');\n");
260+
stringBuilder.append("\t\t\tconst currentURL = new URL(document.URL);\n");
261+
stringBuilder.append("\t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
262+
stringBuilder.append("\t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
263+
stringBuilder.append("\t\t\tif (isSameOrigin) ");
264+
stringBuilder.append("request.headers['");
265+
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
266+
stringBuilder.append("'] = value.replace(/['\"]+/g,'');\n");
267+
stringBuilder.append("\t\t\treturn request;\n");
241268
stringBuilder.append("\t\t},\n");
242269
stringBuilder.append("\t\t" + PRESETS);
243270
return html.replace(PRESETS, stringBuilder.toString());

0 commit comments

Comments
 (0)