Skip to content

Commit 2015aaf

Browse files
authored
Merge pull request #1501 from GandalfTheBlack16/master
Add option to get the CSRF token from the Session Storage
2 parents c56df04 + e9bab25 commit 2015aaf

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public static class Csrf {
145145
*/
146146
private boolean useLocalStorage;
147147

148+
/**
149+
* Use Session storage.
150+
*/
151+
private boolean useSessionStorage;
152+
148153
/**
149154
* The Cookie name.
150155
*/
@@ -155,6 +160,11 @@ public static class Csrf {
155160
*/
156161
private String localStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;
157162

163+
/**
164+
* The Session storage key.
165+
*/
166+
private String sessionStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;
167+
158168
/**
159169
* The Header name.
160170
*/
@@ -187,6 +197,15 @@ public boolean isUseLocalStorage() {
187197
return useLocalStorage;
188198
}
189199

200+
/**
201+
* Use Session storage boolean.
202+
*
203+
* @return the boolean
204+
*/
205+
public boolean isUseSessionStorage() {
206+
return useSessionStorage;
207+
}
208+
190209
/**
191210
* Sets useLocalStorage.
192211
*
@@ -196,6 +215,15 @@ public void setUseLocalStorage(boolean useLocalStorage) {
196215
this.useLocalStorage = useLocalStorage;
197216
}
198217

218+
/**
219+
* Sets useSessionStorage.
220+
*
221+
* @param useSessionStorage the use local storage
222+
*/
223+
public void setUseSessionStorage(boolean useSessionStorage) {
224+
this.useSessionStorage = useSessionStorage;
225+
}
226+
199227
/**
200228
* Gets cookie name.
201229
*
@@ -223,6 +251,15 @@ public String getLocalStorageKey() {
223251
return localStorageKey;
224252
}
225253

254+
/**
255+
* Gets session storage key.
256+
*
257+
* @return the cookie name
258+
*/
259+
public String getSessionStorageKey() {
260+
return sessionStorageKey;
261+
}
262+
226263
/**
227264
* Sets local storage key.
228265
*
@@ -232,6 +269,15 @@ public void setLocalStorageKey(String localStorageKey) {
232269
this.localStorageKey = localStorageKey;
233270
}
234271

272+
/**
273+
* Sets local storage key.
274+
*
275+
* @param sessionStorageKey the local storage key
276+
*/
277+
public void setSessionStorageKey(String sessionStorageKey) {
278+
this.sessionStorageKey = sessionStorageKey;
279+
}
280+
235281
/**
236282
* Gets header name.
237283
*

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept
145145
if (swaggerUiConfig.isCsrfEnabled()) {
146146
if (swaggerUiConfig.getCsrf().isUseLocalStorage())
147147
html = addCSRFLocalStorage(html);
148+
else if (swaggerUiConfig.getCsrf().isUseSessionStorage())
149+
html = addCSRFSessionStorage(html);
148150
else
149151
html = addCSRF(html);
150152
}
@@ -226,21 +228,45 @@ protected String addCSRF(String html) {
226228
protected String addCSRFLocalStorage(String html) {
227229
StringBuilder stringBuilder = new StringBuilder();
228230
stringBuilder.append("requestInterceptor: (request) => {\n");
229-
stringBuilder.append("t\t\tconst value = window.localStorage.getItem('");
231+
stringBuilder.append("\t\t\tconst value = window.localStorage.getItem('");
230232
stringBuilder.append(swaggerUiConfig.getCsrf().getLocalStorageKey() + "');\n");
231-
stringBuilder.append("t\t\tconst currentURL = new URL(document.URL);\n");
232-
stringBuilder.append("t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
233-
stringBuilder.append("t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
234-
stringBuilder.append("t\t\tif (isSameOrigin) ");
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) ");
235237
stringBuilder.append("request.headers['");
236238
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
237239
stringBuilder.append("'] = value;\n");
238-
stringBuilder.append("t\t\treturn request;\n");
240+
stringBuilder.append("\t\t\treturn request;\n");
239241
stringBuilder.append("\t\t},\n");
240242
stringBuilder.append("\t\t" + PRESETS);
241243
return html.replace(PRESETS, stringBuilder.toString());
242244
}
243245

246+
/**
247+
* Add csrf string from Session storage.
248+
*
249+
* @param html the html
250+
* @return the string
251+
*/
252+
protected String addCSRFSessionStorage(String html) {
253+
StringBuilder stringBuilder = new StringBuilder();
254+
stringBuilder.append("requestInterceptor: (request) => {\n");
255+
stringBuilder.append("\t\t\tconst value = window.sessionStorage.getItem('");
256+
stringBuilder.append(swaggerUiConfig.getCsrf().getSessionStorageKey() + "');\n");
257+
stringBuilder.append("\t\t\tconst currentURL = new URL(document.URL);\n");
258+
stringBuilder.append("\t\t\tconst requestURL = new URL(request.url, document.location.origin);\n");
259+
stringBuilder.append("\t\t\tconst isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n");
260+
stringBuilder.append("\t\t\tif (isSameOrigin) ");
261+
stringBuilder.append("request.headers['");
262+
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
263+
stringBuilder.append("'] = value.replace(/['\"]+/g,'');\n");
264+
stringBuilder.append("\t\t\treturn request;\n");
265+
stringBuilder.append("\t\t},\n");
266+
stringBuilder.append("\t\t" + PRESETS);
267+
return html.replace(PRESETS, stringBuilder.toString());
268+
}
269+
244270
/**
245271
* Add syntax highlight string.
246272
*

0 commit comments

Comments
 (0)