@@ -145,6 +145,8 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept
145
145
if (swaggerUiConfig .isCsrfEnabled ()) {
146
146
if (swaggerUiConfig .getCsrf ().isUseLocalStorage ())
147
147
html = addCSRFLocalStorage (html );
148
+ else if (swaggerUiConfig .getCsrf ().isUseSessionStorage ())
149
+ html = addCSRFSessionStorage (html );
148
150
else
149
151
html = addCSRF (html );
150
152
}
@@ -226,21 +228,45 @@ protected String addCSRF(String html) {
226
228
protected String addCSRFLocalStorage (String html ) {
227
229
StringBuilder stringBuilder = new StringBuilder ();
228
230
stringBuilder .append ("requestInterceptor: (request) => {\n " );
229
- stringBuilder .append ("t\t \t const value = window.localStorage.getItem('" );
231
+ stringBuilder .append ("\ t\t \t const value = window.localStorage.getItem('" );
230
232
stringBuilder .append (swaggerUiConfig .getCsrf ().getLocalStorageKey () + "');\n " );
231
- stringBuilder .append ("t\t \t const currentURL = new URL(document.URL);\n " );
232
- stringBuilder .append ("t\t \t const requestURL = new URL(request.url, document.location.origin);\n " );
233
- stringBuilder .append ("t\t \t const isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n " );
234
- stringBuilder .append ("t\t \t if (isSameOrigin) " );
233
+ stringBuilder .append ("\ t\t \t const currentURL = new URL(document.URL);\n " );
234
+ stringBuilder .append ("\ t\t \t const requestURL = new URL(request.url, document.location.origin);\n " );
235
+ stringBuilder .append ("\ t\t \t const isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n " );
236
+ stringBuilder .append ("\ t\t \t if (isSameOrigin) " );
235
237
stringBuilder .append ("request.headers['" );
236
238
stringBuilder .append (swaggerUiConfig .getCsrf ().getHeaderName ());
237
239
stringBuilder .append ("'] = value;\n " );
238
- stringBuilder .append ("t\t \t return request;\n " );
240
+ stringBuilder .append ("\ t\t \t return request;\n " );
239
241
stringBuilder .append ("\t \t },\n " );
240
242
stringBuilder .append ("\t \t " + PRESETS );
241
243
return html .replace (PRESETS , stringBuilder .toString ());
242
244
}
243
245
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 \t const value = window.sessionStorage.getItem('" );
256
+ stringBuilder .append (swaggerUiConfig .getCsrf ().getSessionStorageKey () + "');\n " );
257
+ stringBuilder .append ("\t \t \t const currentURL = new URL(document.URL);\n " );
258
+ stringBuilder .append ("\t \t \t const requestURL = new URL(request.url, document.location.origin);\n " );
259
+ stringBuilder .append ("\t \t \t const isSameOrigin = (currentURL.protocol === requestURL.protocol && currentURL.host === requestURL.host);\n " );
260
+ stringBuilder .append ("\t \t \t if (isSameOrigin) " );
261
+ stringBuilder .append ("request.headers['" );
262
+ stringBuilder .append (swaggerUiConfig .getCsrf ().getHeaderName ());
263
+ stringBuilder .append ("'] = value.replace(/['\" ]+/g,'');\n " );
264
+ stringBuilder .append ("\t \t \t return request;\n " );
265
+ stringBuilder .append ("\t \t },\n " );
266
+ stringBuilder .append ("\t \t " + PRESETS );
267
+ return html .replace (PRESETS , stringBuilder .toString ());
268
+ }
269
+
244
270
/**
245
271
* Add syntax highlight string.
246
272
*
0 commit comments