|
1 | 1 | /* |
2 | 2 | * |
3 | 3 | * * |
| 4 | + * * * Copyright 2019-2020 the original author or authors. |
4 | 5 | * * * |
5 | 6 | * * * * Copyright 2019-2022 the original author or authors. |
6 | 7 | * * * * |
@@ -147,6 +148,8 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept |
147 | 148 | if (swaggerUiConfig.isCsrfEnabled()) { |
148 | 149 | if (swaggerUiConfig.getCsrf().isUseLocalStorage()) |
149 | 150 | html = addCSRFLocalStorage(html); |
| 151 | + else if (swaggerUiConfig.getCsrf().isUseSessionStorage()) |
| 152 | + html = addCSRFSessionStorage(html); |
150 | 153 | else |
151 | 154 | html = addCSRF(html); |
152 | 155 | } |
@@ -228,16 +231,40 @@ protected String addCSRF(String html) { |
228 | 231 | protected String addCSRFLocalStorage(String html) { |
229 | 232 | StringBuilder stringBuilder = new StringBuilder(); |
230 | 233 | 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('"); |
232 | 235 | 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) "); |
237 | 240 | stringBuilder.append("request.headers['"); |
238 | 241 | stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName()); |
239 | 242 | 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"); |
241 | 268 | stringBuilder.append("\t\t},\n"); |
242 | 269 | stringBuilder.append("\t\t" + PRESETS); |
243 | 270 | return html.replace(PRESETS, stringBuilder.toString()); |
|
0 commit comments