Skip to content

Commit c6ae11f

Browse files
authored
Merge pull request #1232 from esfomeado/master
Get CSRF token from local storage
2 parents 969b717 + e22f445 commit c6ae11f

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,17 @@ public final class Constants {
298298
/**
299299
* The constant CSRF_DEFAULT_COOKIE_NAME.
300300
*/
301-
public static final String CSRF_DEFAULT_COOKIE_NAME= "XSRF-TOKEN";
301+
public static final String CSRF_DEFAULT_COOKIE_NAME = "XSRF-TOKEN";
302+
303+
/**
304+
* The constant CSRF_DEFAULT_LOCAL_STORAGE_KEY
305+
*/
306+
public static final String CSRF_DEFAULT_LOCAL_STORAGE_KEY = "XSRF-TOKEN";
302307

303308
/**
304309
* The constant CSRF_DEFAULT_HEADER_NAME.
305310
*/
306-
public static final String CSRF_DEFAULT_HEADER_NAME= "X-XSRF-TOKEN";
311+
public static final String CSRF_DEFAULT_HEADER_NAME = "X-XSRF-TOKEN";
307312

308313
/**
309314
* The constant OPERATION_ATTRIBUTE.

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

+46
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,21 @@ public static class Csrf {
117117
*/
118118
private boolean enabled;
119119

120+
/**
121+
* Use Local storage.
122+
*/
123+
private boolean useLocalStorage;
124+
120125
/**
121126
* The Cookie name.
122127
*/
123128
private String cookieName = Constants.CSRF_DEFAULT_COOKIE_NAME;
124129

130+
/**
131+
* The Local storage key.
132+
*/
133+
private String localStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;
134+
125135
/**
126136
* The Header name.
127137
*/
@@ -145,6 +155,24 @@ public void setEnabled(boolean enabled) {
145155
this.enabled = enabled;
146156
}
147157

158+
/**
159+
* Use Local storage boolean.
160+
*
161+
* @return the boolean
162+
*/
163+
public boolean isUseLocalStorage() {
164+
return useLocalStorage;
165+
}
166+
167+
/**
168+
* Sets useLocalStorage.
169+
*
170+
* @param useLocalStorage the use local storage
171+
*/
172+
public void setUseLocalStorage(boolean useLocalStorage) {
173+
this.useLocalStorage = useLocalStorage;
174+
}
175+
148176
/**
149177
* Gets cookie name.
150178
*
@@ -163,6 +191,24 @@ public void setCookieName(String cookieName) {
163191
this.cookieName = cookieName;
164192
}
165193

194+
/**
195+
* Gets local storage key.
196+
*
197+
* @return the cookie name
198+
*/
199+
public String getLocalStorageKey() {
200+
return localStorageKey;
201+
}
202+
203+
/**
204+
* Sets local storage key.
205+
*
206+
* @param localStorageKey the local storage key
207+
*/
208+
public void setLocalStorageKey(String localStorageKey) {
209+
this.localStorageKey = localStorageKey;
210+
}
211+
166212
/**
167213
* Gets header name.
168214
*

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept
143143
html = overwriteSwaggerDefaultUrl(html);
144144
}
145145
if (swaggerUiConfig.isCsrfEnabled()) {
146-
html = addCSRF(html);
146+
if (swaggerUiConfig.getCsrf().isUseLocalStorage()) {
147+
html = addCSRFLocalStorage(html);
148+
} else {
149+
html = addCSRF(html);
150+
}
147151
}
148152
if (swaggerUiConfig.getSyntaxHighlight() != null) {
149153
html = addSyntaxHighlight(html);
@@ -174,6 +178,26 @@ protected String addCSRF(String html) {
174178
return html.replace(PRESETS, stringBuilder.toString());
175179
}
176180

181+
/**
182+
* Add csrf string.
183+
*
184+
* @param html the html
185+
* @return the string
186+
*/
187+
protected String addCSRFLocalStorage(String html) {
188+
StringBuilder stringBuilder = new StringBuilder();
189+
stringBuilder.append("requestInterceptor: (request) => {\n");
190+
stringBuilder.append("const value = window.localStorage.getItem('");
191+
stringBuilder.append(swaggerUiConfig.getCsrf().getLocalStorageKey() + "');\n");
192+
stringBuilder.append("request.headers['");
193+
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
194+
stringBuilder.append("'] = value;\n");
195+
stringBuilder.append("return request;\n");
196+
stringBuilder.append("},\n");
197+
stringBuilder.append(PRESETS);
198+
return html.replace(PRESETS, stringBuilder.toString());
199+
}
200+
177201
/**
178202
* Add syntax highlight string.
179203
*

0 commit comments

Comments
 (0)