Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit b1cd99b

Browse files
author
Mario
committed
1123 - added support to 'stormpath.spring.security.enabled = false' introducing StormpathWebSecurityDisabledConfiguration
1 parent ba8320f commit b1cd99b

File tree

17 files changed

+756
-347
lines changed

17 files changed

+756
-347
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.stormpath.spring.boot.autoconfigure.StormpathSpringSecurityAutoConfiguration
2-
org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer = com.stormpath.spring.config.StormpathWebSecurityConfigurer
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.stormpath.spring.boot.autoconfigure.StormpathSpringSecurityAutoConfiguration

extensions/spring/boot/stormpath-spring-security-webmvc-spring-boot-starter/src/main/java/com/stormpath/spring/boot/autoconfigure/StormpathWebSecurityAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.stormpath.sdk.servlet.filter.account.AccountResolverFilter;
2222
import com.stormpath.sdk.servlet.mvc.ErrorModelFactory;
2323
import com.stormpath.spring.config.AbstractStormpathWebSecurityConfiguration;
24-
import com.stormpath.spring.config.StormpathWebSecurityConfigurer;
2524
import com.stormpath.spring.filter.ContentNegotiationSpringSecurityAuthenticationFilter;
2625
import com.stormpath.spring.filter.StormpathSecurityContextPersistenceFilter;
2726
import com.stormpath.spring.filter.StormpathWrapperFilter;
@@ -34,6 +33,7 @@
3433
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3534
import org.springframework.context.annotation.Bean;
3635
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
3737
import org.springframework.security.web.AuthenticationEntryPoint;
3838
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
3939
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
@@ -72,9 +72,10 @@ public AuthenticationFailureHandler stormpathAuthenticationFailureHandler() {
7272
}
7373

7474
@Bean
75-
@ConditionalOnMissingBean(name="stormpathWebSecurityConfigurer")
76-
public StormpathWebSecurityConfigurer stormpathWebSecurityConfigurer() {
77-
return super.stormpathWebSecurityConfigurer();
75+
@Override
76+
@ConditionalOnMissingBean(name="stormpathSecurityConfigurerAdapter")
77+
public SecurityConfigurerAdapter stormpathSecurityConfigurerAdapter() {
78+
return super.stormpathSecurityConfigurerAdapter();
7879
}
7980

8081
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.spring.boot.autoconfigure;
17+
18+
import com.stormpath.spring.config.AbstractStormpathWebSecurityDisabledConfiguration;
19+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
27+
import org.springframework.security.web.authentication.logout.LogoutHandler;
28+
import org.springframework.web.servlet.DispatcherServlet;
29+
30+
import javax.servlet.Filter;
31+
import javax.servlet.Servlet;
32+
33+
/**
34+
* @since 1.3.0
35+
*/
36+
@SuppressWarnings("SpringFacetCodeInspection")
37+
@Configuration
38+
@ConditionalOnProperty(name = {"stormpath.enabled", "stormpath.web.enabled"}, matchIfMissing = true)
39+
@ConditionalOnClass({Servlet.class, Filter.class, DispatcherServlet.class})
40+
@ConditionalOnWebApplication
41+
@AutoConfigureAfter(StormpathWebSecurityAutoConfiguration.class)
42+
public class StormpathWebSecurityDisabledAutoConfiguration extends AbstractStormpathWebSecurityDisabledConfiguration {
43+
44+
@Bean
45+
@ConditionalOnMissingBean(name="stormpathSecurityConfigurerAdapter")
46+
@ConditionalOnProperty(name = "stormpath.spring.security.enabled", havingValue = "false")
47+
public SecurityConfigurerAdapter stormpathSecurityConfigurerAdapter() {
48+
//This bean will only be created if `stormpath.spring.security.enabled` is false
49+
return super.stormpathSecurityConfigurerAdapter();
50+
}
51+
52+
@Bean
53+
@ConditionalOnMissingBean(name="stormpathLogoutHandler")
54+
@ConditionalOnProperty(name = "stormpath.spring.security.enabled", havingValue = "false")
55+
public LogoutHandler stormpathLogoutHandler() {
56+
return super.stormpathLogoutHandler();
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.stormpath.spring.boot.autoconfigure.StormpathWebSecurityAutoConfiguration,\
2-
com.stormpath.spring.boot.autoconfigure.StormpathMethodSecurityAutoConfiguration
2+
com.stormpath.spring.boot.autoconfigure.StormpathMethodSecurityAutoConfiguration,\
3+
com.stormpath.spring.boot.autoconfigure.StormpathWebSecurityDisabledAutoConfiguration
4+
org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer = com.stormpath.spring.config.StormpathWebSecurityConfigurer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.spring.config;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.beans.factory.annotation.Qualifier;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
24+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
25+
import org.springframework.security.web.DefaultSecurityFilterChain;
26+
import org.springframework.security.web.authentication.logout.LogoutHandler;
27+
28+
/**
29+
* @since 1.3.0
30+
*/
31+
public abstract class AbstractStormpathSecurityConfigurerAdapter extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
32+
33+
private static final Logger log = LoggerFactory.getLogger(AbstractStormpathSecurityConfigurerAdapter.class);
34+
35+
@Autowired
36+
@Qualifier("stormpathLogoutHandler")
37+
protected LogoutHandler logoutHandler;
38+
39+
@Value("#{ @environment['stormpath.web.enabled'] ?: true }")
40+
protected boolean stormpathWebEnabled;
41+
42+
@Value("#{ @environment['stormpath.web.login.enabled'] ?: true }")
43+
protected boolean loginEnabled;
44+
45+
@Value("#{ @environment['stormpath.web.login.uri'] ?: '/login' }")
46+
protected String loginUri;
47+
48+
@Value("#{ @environment['stormpath.web.logout.enabled'] ?: true }")
49+
protected boolean logoutEnabled;
50+
51+
@Value("#{ @environment['stormpath.web.logout.uri'] ?: '/logout' }")
52+
protected String logoutUri;
53+
54+
@Value("#{ @environment['stormpath.web.forgotPassword.enabled'] ?: true }")
55+
protected boolean forgotEnabled;
56+
57+
@Value("#{ @environment['stormpath.web.forgotPassword.uri'] ?: '/forgot' }")
58+
protected String forgotUri;
59+
60+
@Value("#{ @environment['stormpath.web.changePassword.enabled'] ?: true }")
61+
protected boolean changeEnabled;
62+
63+
@Value("#{ @environment['stormpath.web.changePassword.uri'] ?: '/change' }")
64+
protected String changeUri;
65+
66+
@Value("#{ @environment['stormpath.web.register.enabled'] ?: true }")
67+
protected boolean registerEnabled;
68+
69+
@Value("#{ @environment['stormpath.web.register.uri'] ?: '/register' }")
70+
protected String registerUri;
71+
72+
@Value("#{ @environment['stormpath.web.verifyEmail.enabled'] ?: true }")
73+
protected boolean verifyEnabled;
74+
75+
@Value("#{ @environment['stormpath.web.verifyEmail.uri'] ?: '/verify' }")
76+
protected String verifyUri;
77+
78+
@Value("#{ @environment['stormpath.web.csrf.token.enabled'] ?: true }")
79+
protected boolean csrfTokenEnabled;
80+
81+
@Value("#{ @environment['stormpath.web.resendVerification.uri'] ?: '/resendVerification' }")
82+
protected String resendVerificationUri;
83+
84+
}

extensions/spring/stormpath-spring-security-webmvc/src/main/java/com/stormpath/spring/config/AbstractStormpathWebSecurityConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.core.annotation.Order;
4848
import org.springframework.security.authentication.AuthenticationManager;
4949
import org.springframework.security.authentication.AuthenticationProvider;
50+
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
5051
import org.springframework.security.web.AuthenticationEntryPoint;
5152
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
5253
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
@@ -153,9 +154,11 @@ public abstract class AbstractStormpathWebSecurityConfiguration {
153154
@Qualifier("stormpathWrappedServletRequestFactory")
154155
private WrappedServletRequestFactory wrappedServletRequestFactory;
155156

157+
@Value("#{ @environment['stormpath.spring.security.enabled'] ?: true }")
158+
protected boolean stormpathSecurityEnabled;
156159

157-
public StormpathWebSecurityConfigurer stormpathWebSecurityConfigurer() {
158-
return new StormpathWebSecurityConfigurer();
160+
public SecurityConfigurerAdapter stormpathSecurityConfigurerAdapter() {
161+
return new StormpathSecurityConfigurerAdapter();
159162
}
160163

161164
public AuthenticationSuccessHandler stormpathAuthenticationSuccessHandler() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.spring.config;
17+
18+
import com.stormpath.sdk.authc.AuthenticationResult;
19+
import com.stormpath.sdk.servlet.http.Saver;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.beans.factory.annotation.Qualifier;
22+
import org.springframework.core.annotation.Order;
23+
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
24+
import org.springframework.security.web.authentication.logout.LogoutHandler;
25+
26+
/**
27+
* @since 1.3.0
28+
*/
29+
@Order(100)
30+
public abstract class AbstractStormpathWebSecurityDisabledConfiguration {
31+
32+
@Autowired
33+
@Qualifier("stormpathAuthenticationResultSaver")
34+
protected Saver<AuthenticationResult> authenticationResultSaver; //provided by stormpath-spring-webmvc
35+
36+
public SecurityConfigurerAdapter stormpathSecurityConfigurerAdapter() {
37+
//This bean will only be created if `stormpath.spring.security.enabled` is false
38+
return new DisabledStormpathSecurityConfigurerAdapter();
39+
}
40+
41+
public LogoutHandler stormpathLogoutHandler() {
42+
return new StormpathLogoutHandler(authenticationResultSaver);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.spring.config;
17+
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.beans.factory.annotation.Qualifier;
20+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
21+
import org.springframework.security.web.authentication.logout.LogoutHandler;
22+
23+
/**
24+
* @since 1.3.0
25+
*/
26+
public class DisabledStormpathSecurityConfigurerAdapter extends AbstractStormpathSecurityConfigurerAdapter {
27+
28+
@Autowired
29+
@Qualifier("stormpathLogoutHandler")
30+
protected LogoutHandler logoutHandler;
31+
32+
@Override
33+
public void init(HttpSecurity http) throws Exception {
34+
if (stormpathWebEnabled) {
35+
if (csrfTokenEnabled) {
36+
//Since our Spring Securoty integration is disabled and we are using our own CSRF tokens then we want
37+
//to avoid our own pages to be validated by Spring Security, otherwise they will fail
38+
disableCsrf(loginUri, loginEnabled, http);
39+
disableCsrf(logoutUri, logoutEnabled, http);
40+
disableCsrf(forgotUri, forgotEnabled, http);
41+
disableCsrf(changeUri, changeEnabled, http);
42+
disableCsrf(registerUri, registerEnabled, http);
43+
disableCsrf(verifyUri, verifyEnabled, http);
44+
45+
}
46+
http.logout().addLogoutHandler(logoutHandler);
47+
}
48+
}
49+
50+
private void disableCsrf(String endpoint, boolean doDisable, HttpSecurity http) throws Exception {
51+
if (doDisable) {
52+
http.csrf().ignoringAntMatchers(endpoint);
53+
}
54+
}
55+
}

extensions/spring/stormpath-spring-security-webmvc/src/main/java/com/stormpath/spring/config/EnableStormpathWebSecurity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
@EnableStormpathWebMvc
3232
@EnableStormpathSecurity
3333
@EnableWebSecurity
34-
@Import({StormpathWebSecurityConfiguration.class, StormpathMethodSecurityConfiguration.class})
34+
@Import({StormpathWebSecurityConfiguration.class, StormpathWebSecurityDisabledConfiguration.class, StormpathMethodSecurityConfiguration.class})
3535
public @interface EnableStormpathWebSecurity {}

0 commit comments

Comments
 (0)