@@ -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 \t const value = window.localStorage.getItem('" );
231+ stringBuilder .append ("\ t\t \t const value = window.localStorage.getItem('" );
230232 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) " );
235237 stringBuilder .append ("request.headers['" );
236238 stringBuilder .append (swaggerUiConfig .getCsrf ().getHeaderName ());
237239 stringBuilder .append ("'] = value;\n " );
238- stringBuilder .append ("t\t \t return request;\n " );
240+ stringBuilder .append ("\ t\t \t return 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 \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+
244270 /**
245271 * Add syntax highlight string.
246272 *
0 commit comments