29
29
/**
30
30
* Configurer that adds {@link SocialAuthenticationFilter} to Spring Security's filter chain.
31
31
* Used with Spring Security 3.2's Java-based configuration support, when overriding WebSecurityConfigurerAdapter#configure(HttpSecurity):
32
- *
32
+ *
33
33
* <pre>
34
34
* protected void configure(HttpSecurity http) throws Exception {
35
35
* http.
38
38
* .apply(new SpringSocialConfigurer());
39
39
* }
40
40
* </pre>
41
- *
41
+ *
42
42
* @author Craig Walls
43
43
*/
44
44
public class SpringSocialConfigurer extends SecurityConfigurerAdapter <DefaultSecurityFilterChain , HttpSecurity > {
45
45
46
46
private UserIdSource userIdSource ;
47
-
47
+
48
48
private String postLoginUrl ;
49
-
49
+
50
50
private String postFailureUrl ;
51
51
52
52
private String signupUrl ;
53
53
54
54
private String connectionAddedRedirectUrl ;
55
55
56
+ private String filterProcessesUrl ;
57
+
56
58
private boolean alwaysUsePostLoginUrl = false ;
57
59
58
60
/**
@@ -62,30 +64,30 @@ public class SpringSocialConfigurer extends SecurityConfigurerAdapter<DefaultSec
62
64
*/
63
65
public SpringSocialConfigurer () {
64
66
}
65
-
67
+
66
68
@ Override
67
- public void configure (HttpSecurity http ) throws Exception {
69
+ public void configure (HttpSecurity http ) throws Exception {
68
70
ApplicationContext applicationContext = http .getSharedObject (ApplicationContext .class );
69
71
UsersConnectionRepository usersConnectionRepository = getDependency (applicationContext , UsersConnectionRepository .class );
70
72
SocialAuthenticationServiceLocator authServiceLocator = getDependency (applicationContext , SocialAuthenticationServiceLocator .class );
71
73
SocialUserDetailsService socialUsersDetailsService = getDependency (applicationContext , SocialUserDetailsService .class );
72
-
74
+
73
75
SocialAuthenticationFilter filter = new SocialAuthenticationFilter (
74
- http .getSharedObject (AuthenticationManager .class ),
75
- userIdSource != null ? userIdSource : new AuthenticationNameUserIdSource (),
76
- usersConnectionRepository ,
76
+ http .getSharedObject (AuthenticationManager .class ),
77
+ userIdSource != null ? userIdSource : new AuthenticationNameUserIdSource (),
78
+ usersConnectionRepository ,
77
79
authServiceLocator );
78
-
80
+
79
81
RememberMeServices rememberMe = http .getSharedObject (RememberMeServices .class );
80
82
if (rememberMe != null ) {
81
83
filter .setRememberMeServices (rememberMe );
82
84
}
83
-
85
+
84
86
if (postLoginUrl != null ) {
85
87
filter .setPostLoginUrl (postLoginUrl );
86
88
filter .setAlwaysUsePostLoginUrl (alwaysUsePostLoginUrl );
87
89
}
88
-
90
+
89
91
if (postFailureUrl != null ) {
90
92
filter .setPostFailureUrl (postFailureUrl );
91
93
}
@@ -97,7 +99,11 @@ public void configure(HttpSecurity http) throws Exception {
97
99
if (connectionAddedRedirectUrl != null ) {
98
100
filter .setConnectionAddedRedirectUrl (connectionAddedRedirectUrl );
99
101
}
100
-
102
+
103
+ if (filterProcessesUrl != null ) {
104
+ filter .setFilterProcessesUrl (filterProcessesUrl );
105
+ }
106
+
101
107
http .authenticationProvider (
102
108
new SocialAuthenticationProvider (usersConnectionRepository , socialUsersDetailsService ))
103
109
.addFilterBefore (postProcess (filter ), AbstractPreAuthenticatedProcessingFilter .class );
@@ -111,7 +117,7 @@ private <T> T getDependency(ApplicationContext applicationContext, Class<T> depe
111
117
throw new IllegalStateException ("SpringSocialConfigurer depends on " + dependencyType .getName () +". No single bean of that type found in application context." , e );
112
118
}
113
119
}
114
-
120
+
115
121
/**
116
122
* Sets the {@link UserIdSource} to use for authentication. Defaults to {@link AuthenticationNameUserIdSource}.
117
123
* @param userIdSource the UserIdSource to use when authenticating
@@ -121,27 +127,27 @@ public SpringSocialConfigurer userIdSource(UserIdSource userIdSource) {
121
127
this .userIdSource = userIdSource ;
122
128
return this ;
123
129
}
124
-
130
+
125
131
/**
126
132
* Sets the URL to land on after a successful login.
127
133
* @param postLoginUrl the URL to redirect to after a successful login
128
- * @return this SpringSocialConfigurer for chained configuration
134
+ * @return this SpringSocialConfigurer for chained configuration
129
135
*/
130
136
public SpringSocialConfigurer postLoginUrl (String postLoginUrl ) {
131
137
this .postLoginUrl = postLoginUrl ;
132
138
return this ;
133
139
}
134
-
140
+
135
141
/**
136
142
* If true, always redirect to postLoginUrl, even if a pre-signin target is in the request cache.
137
143
* @param alwaysUsePostLoginUrl if true, always redirect to the postLoginUrl
138
- * @return this SpringSocialConfigurer for chained configuration
144
+ * @return this SpringSocialConfigurer for chained configuration
139
145
*/
140
146
public SpringSocialConfigurer alwaysUsePostLoginUrl (boolean alwaysUsePostLoginUrl ) {
141
147
this .alwaysUsePostLoginUrl = alwaysUsePostLoginUrl ;
142
148
return this ;
143
149
}
144
-
150
+
145
151
/**
146
152
* Sets the URL to redirect to if authentication fails or if authorization is denied by the user.
147
153
* @param postFailureUrl the URL to redirect to after an authentication fail or authorization deny
@@ -172,4 +178,13 @@ public SpringSocialConfigurer connectionAddedRedirectUrl(String connectionAddedR
172
178
return this ;
173
179
}
174
180
181
+ /**
182
+ * Sets the URL that determines if social authentication is required.
183
+ * @param filterProcessesUrl the URL that will initiate the social authentication process
184
+ * @return this SpringSocialConfigurer for chained configuration
185
+ */
186
+ public SpringSocialConfigurer filterProcessesUrl (String filterProcessesUrl ) {
187
+ this .filterProcessesUrl = filterProcessesUrl ;
188
+ return this ;
189
+ }
175
190
}
0 commit comments