22
22
23
23
import org .apache .commons .logging .Log ;
24
24
import org .apache .commons .logging .LogFactory ;
25
+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
25
26
import org .springframework .beans .factory .annotation .Autowired ;
26
27
import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
27
28
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
28
29
import org .springframework .boot .autoconfigure .security .SecurityProperties .User ;
29
- import org .springframework .context .ApplicationContext ;
30
30
import org .springframework .context .ApplicationListener ;
31
31
import org .springframework .context .annotation .Bean ;
32
32
import org .springframework .context .annotation .Configuration ;
@@ -71,53 +71,33 @@ public class AuthenticationManagerConfiguration {
71
71
72
72
@ Bean
73
73
@ Primary
74
- public AuthenticationManager authenticationManager (AuthenticationConfiguration auth )
75
- throws Exception {
76
- return auth .getAuthenticationManager ();
74
+ public AuthenticationManager authenticationManager (
75
+ AuthenticationConfiguration configuration ) throws Exception {
76
+ return configuration .getAuthenticationManager ();
77
77
}
78
78
79
79
@ Bean
80
- public static BootDefaultingAuthenticationConfigurerAdapter bootDefaultingAuthenticationConfigurerAdapter (
81
- SecurityProperties security , List <SecurityPrequisite > dependencies ) {
82
- return new BootDefaultingAuthenticationConfigurerAdapter (security );
83
- }
84
-
85
- @ Component
86
- protected static class AuthenticationManagerConfigurationListener implements
87
- ApplicationListener <ContextRefreshedEvent > {
88
-
89
- @ Autowired
90
- private AuthenticationEventPublisher authenticationEventPublisher ;
91
-
92
- @ Override
93
- public void onApplicationEvent (ContextRefreshedEvent event ) {
94
- ApplicationContext context = event .getApplicationContext ();
95
- if (context .getBeanNamesForType (AuthenticationManager .class ).length == 0 ) {
96
- return ;
97
- }
98
- AuthenticationManager manager = context .getBean (AuthenticationManager .class );
99
- if (manager instanceof ProviderManager ) {
100
- ((ProviderManager ) manager )
101
- .setAuthenticationEventPublisher (this .authenticationEventPublisher );
102
- }
103
- }
104
-
80
+ public static SpringBootAuthenticationConfigurerAdapter springBootAuthenticationConfigurerAdapter (
81
+ SecurityProperties securityProperties , List <SecurityPrequisite > dependencies ) {
82
+ return new SpringBootAuthenticationConfigurerAdapter (securityProperties );
105
83
}
106
84
107
85
/**
108
- * We must add {@link BootDefaultingAuthenticationConfigurerAdapter} in the init phase
109
- * of the last {@link GlobalAuthenticationConfigurerAdapter}. The reason is that the
110
- * typical flow is something like:
86
+ * {@link GlobalAuthenticationConfigurerAdapter} to apply
87
+ * {@link DefaultInMemoryUserDetailsManagerConfigurer}. We must apply
88
+ * {@link DefaultInMemoryUserDetailsManagerConfigurer} in the init phase of the last
89
+ * {@link GlobalAuthenticationConfigurerAdapter}. The reason is that the typical flow
90
+ * is something like:
111
91
*
112
92
* <ul>
113
93
* <li>A
114
94
* {@link GlobalAuthenticationConfigurerAdapter#init(AuthenticationManagerBuilder)}
115
95
* exists that adds a {@link SecurityConfigurer} to the
116
- * {@link AuthenticationManagerBuilder}</li>
96
+ * {@link AuthenticationManagerBuilder}. </li>
117
97
* <li>
118
98
* {@link AuthenticationManagerConfiguration#init(AuthenticationManagerBuilder)} adds
119
- * BootDefaultingAuthenticationConfigurerAdapter so it is after the
120
- * {@link SecurityConfigurer} in the first step</li>
99
+ * {@link SpringBootAuthenticationConfigurerAdapter} so it is after the
100
+ * {@link SecurityConfigurer} in the first step. </li>
121
101
* <li>We then can default an {@link AuthenticationProvider} if necessary. Note we can
122
102
* only invoke the
123
103
* {@link AuthenticationManagerBuilder#authenticationProvider(AuthenticationProvider)}
@@ -127,69 +107,103 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
127
107
* </ul>
128
108
*/
129
109
@ Order (Ordered .LOWEST_PRECEDENCE - 100 )
130
- private static class BootDefaultingAuthenticationConfigurerAdapter extends
110
+ private static class SpringBootAuthenticationConfigurerAdapter extends
131
111
GlobalAuthenticationConfigurerAdapter {
132
- private final SecurityProperties security ;
112
+
113
+ private final SecurityProperties securityProperties ;
133
114
134
115
@ Autowired
135
- public BootDefaultingAuthenticationConfigurerAdapter (SecurityProperties security ) {
136
- this .security = security ;
116
+ public SpringBootAuthenticationConfigurerAdapter (
117
+ SecurityProperties securityProperties ) {
118
+ this .securityProperties = securityProperties ;
137
119
}
138
120
139
121
@ Override
140
122
public void init (AuthenticationManagerBuilder auth ) throws Exception {
141
- auth .apply (new DefaultingInMemoryUserDetailsManagerConfigurer (this .security ));
123
+ auth .apply (new DefaultInMemoryUserDetailsManagerConfigurer (
124
+ this .securityProperties ));
142
125
}
143
126
144
- /**
145
- * This is necessary to delay adding the default user.
146
- *
147
- * <ul>
148
- * <li>A GlobalAuthenticationConfigurerAdapter will initialize the
149
- * AuthenticationManagerBuilder with a Configurer which will be after any
150
- * GlobalAuthenticationConfigurerAdapter</li>
151
- * <li>BootDefaultingAuthenticationConfigurerAdapter will be invoked after all
152
- * GlobalAuthenticationConfigurerAdapter, but before the Configurers that were
153
- * added by other GlobalAuthenticationConfigurerAdapter instances</li>
154
- * <li>BootDefaultingAuthenticationConfigurerAdapter will add
155
- * DefaultingInMemoryUserDetailsManagerConfigurer after all Configurer instances</li>
156
- * <li>All init methods will be invoked</li>
157
- * <li>All configure methods will be invoked which is where the
158
- * AuthenticationProvider instances are setup</li>
159
- * <li>If no AuthenticationProviders were provided,
160
- * DefaultingInMemoryUserDetailsManagerConfigurer will default the value</li>
161
- * </ul>
162
- *
163
- * @author Rob Winch
164
- */
165
- private static class DefaultingInMemoryUserDetailsManagerConfigurer extends
166
- InMemoryUserDetailsManagerConfigurer <AuthenticationManagerBuilder > {
167
- private final SecurityProperties security ;
168
-
169
- public DefaultingInMemoryUserDetailsManagerConfigurer (
170
- SecurityProperties security ) {
171
- this .security = security ;
127
+ }
128
+
129
+ /**
130
+ * {@link InMemoryUserDetailsManagerConfigurer} to add user details from
131
+ * {@link SecurityProperties}. This is necessary to delay adding the default user.
132
+ *
133
+ * <ul>
134
+ * <li>A {@link GlobalAuthenticationConfigurerAdapter} will initialize the
135
+ * {@link AuthenticationManagerBuilder} with a Configurer which will be after any
136
+ * {@link GlobalAuthenticationConfigurerAdapter}.</li>
137
+ * <li>{@link SpringBootAuthenticationConfigurerAdapter} will be invoked after all
138
+ * {@link GlobalAuthenticationConfigurerAdapter}, but before the Configurers that were
139
+ * added by other {@link GlobalAuthenticationConfigurerAdapter} instances.</li>
140
+ * <li>A {@link SpringBootAuthenticationConfigurerAdapter} will add
141
+ * {@link DefaultInMemoryUserDetailsManagerConfigurer} after all Configurer instances.
142
+ * </li>
143
+ * <li>All init methods will be invoked.</li>
144
+ * <li>All configure methods will be invoked which is where the
145
+ * {@link AuthenticationProvider} instances are setup.</li>
146
+ * <li>If no AuthenticationProviders were provided,
147
+ * {@link DefaultInMemoryUserDetailsManagerConfigurer} will default the value.</li>
148
+ * </ul>
149
+ */
150
+ private static class DefaultInMemoryUserDetailsManagerConfigurer extends
151
+ InMemoryUserDetailsManagerConfigurer <AuthenticationManagerBuilder > {
152
+
153
+ private final SecurityProperties securityProperties ;
154
+
155
+ public DefaultInMemoryUserDetailsManagerConfigurer (
156
+ SecurityProperties securityProperties ) {
157
+ this .securityProperties = securityProperties ;
158
+ }
159
+
160
+ @ Override
161
+ public void configure (AuthenticationManagerBuilder auth ) throws Exception {
162
+ if (auth .isConfigured ()) {
163
+ return ;
172
164
}
165
+ User user = this .securityProperties .getUser ();
166
+ if (user .isDefaultPassword ()) {
167
+ logger .info ("\n \n Using default security password: " + user .getPassword ()
168
+ + "\n " );
169
+ }
170
+ Set <String > roles = new LinkedHashSet <String >(user .getRole ());
171
+ withUser (user .getName ()).password (user .getPassword ()).roles (
172
+ roles .toArray (new String [roles .size ()]));
173
+ super .configure (auth );
174
+ }
173
175
174
- @ Override
175
- public void configure (AuthenticationManagerBuilder auth ) throws Exception {
176
- if (auth .isConfigured ()) {
177
- return ;
178
- }
176
+ }
179
177
180
- User user = this .security .getUser ();
181
- if (user .isDefaultPassword ()) {
182
- logger .info ("\n \n Using default security password: "
183
- + user .getPassword () + "\n " );
184
- }
178
+ /**
179
+ * {@link ApplicationListener} to autowire the {@link AuthenticationEventPublisher}
180
+ * into the {@link AuthenticationManager}.
181
+ */
182
+ @ Component
183
+ protected static class AuthenticationManagerConfigurationListener implements
184
+ ApplicationListener <ContextRefreshedEvent > {
185
185
186
- Set <String > roles = new LinkedHashSet <String >(user .getRole ());
187
- withUser (user .getName ()).password (user .getPassword ()).roles (
188
- roles .toArray (new String [roles .size ()]));
186
+ @ Autowired
187
+ private AuthenticationEventPublisher eventPublisher ;
189
188
190
- super .configure (auth );
189
+ @ Override
190
+ public void onApplicationEvent (ContextRefreshedEvent event ) {
191
+ try {
192
+ configureAuthenticationManager (event .getApplicationContext ().getBean (
193
+ AuthenticationManager .class ));
194
+ }
195
+ catch (NoSuchBeanDefinitionException ex ) {
196
+ // Ignore
191
197
}
198
+ }
192
199
200
+ private void configureAuthenticationManager (AuthenticationManager manager ) {
201
+ if (manager instanceof ProviderManager ) {
202
+ ((ProviderManager ) manager )
203
+ .setAuthenticationEventPublisher (this .eventPublisher );
204
+ }
193
205
}
206
+
194
207
}
195
- }
208
+
209
+ }
0 commit comments