Skip to content

Commit 0fbb5ef

Browse files
committed
https://github.com/devondragon/SpringUserFramework/issues/2
1 parent 4f667d9 commit 0fbb5ef

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/main/java/com/digitalsanctuary/spring/user/util/WebSecurityConfig.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
4949
@Value("#{'${user.security.unprotectedURIs}'.split(',')}")
5050
private String[] unprotectedURIsArray;
5151

52+
@Value("#{'${user.security.disableCSRFdURIs}'.split(',')}")
53+
private String[] disableCSRFURIsArray;
54+
5255
@Value("${user.security.loginPageURI}")
5356
private String loginPageURI;
5457

@@ -122,22 +125,33 @@ protected void configure(HttpSecurity http) throws Exception {
122125
unprotectedURIs.add(forgotPasswordURI);
123126
unprotectedURIs.add(forgotPasswordPendingURI);
124127
unprotectedURIs.add(forgotPasswordChangeURI);
125-
unprotectedURIs.toArray(new String[0]);
128+
unprotectedURIs.removeAll(Arrays.asList("", null));
126129

127130
logger.debug("WebSecurityConfig.configure:" + "enhanced unprotectedURIs: {}", unprotectedURIs.toString());
128131

132+
ArrayList<String> disableCSRFURIs = new ArrayList<String>();
133+
disableCSRFURIs.addAll(Arrays.asList(disableCSRFURIsArray));
134+
disableCSRFURIs.removeAll(Arrays.asList("", null));
135+
129136
if (DEFAULT_ACTION_DENY.equals(getDefaultAction())) {
130137
http.authorizeRequests().antMatchers(unprotectedURIs.toArray(new String[0])).permitAll().anyRequest()
131138
.authenticated().and().formLogin().loginPage(loginPageURI).loginProcessingUrl(loginActionURI)
132139
.successHandler(loginSuccessService).permitAll().and().logout().logoutUrl(logoutActionURI)
133140
.invalidateHttpSession(true).logoutSuccessHandler(logoutSuccessService).deleteCookies("JSESSIONID")
134141
.permitAll();
142+
if (disableCSRFURIs != null && disableCSRFURIs.size() > 0) {
143+
http.csrf().ignoringAntMatchers(disableCSRFURIs.toArray(new String[0]));
144+
}
135145
} else if (DEFAULT_ACTION_ALLOW.equals(getDefaultAction())) {
136146
http.authorizeRequests().antMatchers(protectedURIsArray).authenticated().antMatchers("/**").permitAll()
137147
.and().formLogin().loginPage(loginPageURI).loginProcessingUrl(loginActionURI)
138148
.successHandler(loginSuccessService).successHandler(loginSuccessService).and().logout()
139149
.logoutUrl(logoutActionURI).invalidateHttpSession(true).logoutSuccessHandler(logoutSuccessService)
140150
.deleteCookies("JSESSIONID").permitAll();
151+
152+
if (disableCSRFURIs != null && disableCSRFURIs.size() > 0) {
153+
http.csrf().ignoringAntMatchers(disableCSRFURIs.toArray(new String[0]));
154+
}
141155
} else {
142156
logger.error("WebSecurityConfig.configure:"
143157
+ "user.security.defaultAction must be set to either {} or {}!!! Denying access to all resources to force intentional configuration.",

src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ user.security.protectedURIs=/protected.html
3535
// Used if default is deny
3636
user.security.unprotectedURIs=/,/index.html,/favicon.ico,/css/*,/js/*,/img/*,/user/registration,/user/resendRegistrationToken,/user/resetPassword,/user/registrationConfirm,/user/changePassword,/user/savePassword
3737

38+
// URIs to disable CSRF checks. This might include API endpoints used by external clients.
39+
user.security.disableCSRFdURIs=/no-csrf-test
40+
3841

3942
// Centralizing the URIs of common pages to make changing paths easier. You can leave this section alone if you use the default page locations from this project. These URLs do NOT have to be included in the unprotectedURIs list above as they will automatically be handled.
4043
user.security.loginPageURI=/user/login.html

0 commit comments

Comments
 (0)