Skip to content

Commit df81b31

Browse files
committed
Backport to 1.1.x the fix for gh-2474 (originally made in e42fa79)
1 parent 8622e5d commit df81b31

File tree

1 file changed

+24
-82
lines changed

1 file changed

+24
-82
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.annotation.Primary;
3434
import org.springframework.context.event.ContextRefreshedEvent;
35+
import org.springframework.core.Ordered;
3536
import org.springframework.core.annotation.Order;
3637
import org.springframework.security.authentication.AuthenticationEventPublisher;
3738
import org.springframework.security.authentication.AuthenticationManager;
@@ -40,9 +41,8 @@
4041
import org.springframework.security.config.annotation.ObjectPostProcessor;
4142
import org.springframework.security.config.annotation.SecurityConfigurer;
4243
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
44+
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
4345
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
44-
import org.springframework.security.core.Authentication;
45-
import org.springframework.security.core.AuthenticationException;
4646
import org.springframework.stereotype.Component;
4747

4848
/**
@@ -60,52 +60,25 @@
6060
@ConditionalOnBean(ObjectPostProcessor.class)
6161
@ConditionalOnMissingBean({ AuthenticationManager.class })
6262
@Order(0)
63-
public class AuthenticationManagerConfiguration extends
64-
GlobalAuthenticationConfigurerAdapter {
65-
66-
/*
67-
* Yes, this class is a GlobalAuthenticationConfigurerAdapter, even though none of
68-
* those methods are overridden: we want Spring Security to instantiate us early, so
69-
* we can in turn force the SecurityPrequisites to be instantiated. This will prevent
70-
* ordering issues between Spring Boot modules when they need to influence the default
71-
* security configuration.
72-
*/
63+
public class AuthenticationManagerConfiguration {
7364

7465
private static Log logger = LogFactory
7566
.getLog(AuthenticationManagerConfiguration.class);
7667

7768
@Autowired
7869
private List<SecurityPrequisite> dependencies;
7970

80-
@Autowired
81-
private SecurityProperties security;
82-
83-
@Autowired
84-
private ObjectPostProcessor<Object> objectPostProcessor;
85-
8671
@Bean
8772
@Primary
88-
public AuthenticationManager authenticationManager(AuthenticationManagerBuilder auth,
89-
ApplicationContext context) throws Exception {
90-
91-
if (isAuthenticationManagerAlreadyConfigured(context)) {
92-
return new LazyAuthenticationManager(auth);
93-
}
94-
95-
/*
96-
* This AuthenticationManagerBuilder is for the global AuthenticationManager
97-
*/
98-
BootDefaultingAuthenticationConfigurerAdapter configurer = new BootDefaultingAuthenticationConfigurerAdapter();
99-
configurer.configure(auth);
100-
AuthenticationManager manager = configurer.getAuthenticationManagerBuilder()
101-
.getOrBuild();
102-
configurer.configureParent(auth);
103-
return manager;
104-
73+
public AuthenticationManager authenticationManager(AuthenticationConfiguration auth)
74+
throws Exception {
75+
return auth.getAuthenticationManager();
10576
}
10677

107-
private boolean isAuthenticationManagerAlreadyConfigured(ApplicationContext context) {
108-
return context.getBeanNamesForType(GlobalAuthenticationConfigurerAdapter.class).length > 2;
78+
@Bean
79+
public static BootDefaultingAuthenticationConfigurerAdapter bootDefaultingAuthenticationConfigurerAdapter(
80+
SecurityProperties security, List<SecurityPrequisite> dependencies) {
81+
return new BootDefaultingAuthenticationConfigurerAdapter(security);
10982
}
11083

11184
@Component
@@ -152,64 +125,33 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
152125
* methods are invoked before configure, which cannot be guaranteed at this point.</li>
153126
* </ul>
154127
*/
155-
private class BootDefaultingAuthenticationConfigurerAdapter {
156-
157-
private AuthenticationManagerBuilder defaultAuth;
158-
159-
private AuthenticationManager parent;
160-
161-
public void configureParent(AuthenticationManagerBuilder auth) {
162-
if (!auth.isConfigured() && this.parent != null) {
163-
auth.parentAuthenticationManager(this.parent);
164-
}
165-
}
128+
@Order(Ordered.LOWEST_PRECEDENCE - 100)
129+
private static class BootDefaultingAuthenticationConfigurerAdapter extends
130+
GlobalAuthenticationConfigurerAdapter {
131+
private final SecurityProperties security;
166132

167-
public AuthenticationManagerBuilder getAuthenticationManagerBuilder() {
168-
return this.defaultAuth;
133+
@Autowired
134+
public BootDefaultingAuthenticationConfigurerAdapter(SecurityProperties security) {
135+
this.security = security;
169136
}
170137

171-
public void configure(AuthenticationManagerBuilder auth) throws Exception {
138+
@Override
139+
public void init(AuthenticationManagerBuilder auth) throws Exception {
172140
if (auth.isConfigured()) {
173-
this.defaultAuth = auth;
174141
return;
175142
}
176143

177-
User user = AuthenticationManagerConfiguration.this.security.getUser();
144+
User user = this.security.getUser();
178145
if (user.isDefaultPassword()) {
179146
logger.info("\n\nUsing default security password: " + user.getPassword()
180147
+ "\n\n");
181148
}
182149

183-
this.defaultAuth = new AuthenticationManagerBuilder(
184-
AuthenticationManagerConfiguration.this.objectPostProcessor);
185-
186150
Set<String> roles = new LinkedHashSet<String>(user.getRole());
187-
188-
this.parent = this.defaultAuth.inMemoryAuthentication()
189-
.withUser(user.getName()).password(user.getPassword())
190-
.roles(roles.toArray(new String[roles.size()])).and().and().build();
191-
192-
// Defer actually setting the parent on the AuthenticationManagerBuilder
193-
// because it makes it "configured" and we are only in the init() phase
194-
// here.
195-
151+
auth.inMemoryAuthentication().withUser(user.getName())
152+
.password(user.getPassword())
153+
.roles(roles.toArray(new String[roles.size()]));
196154
}
197155
}
198156

199-
private static class LazyAuthenticationManager implements AuthenticationManager {
200-
201-
private AuthenticationManagerBuilder builder;
202-
203-
public LazyAuthenticationManager(AuthenticationManagerBuilder builder) {
204-
this.builder = builder;
205-
}
206-
207-
@Override
208-
public Authentication authenticate(Authentication authentication)
209-
throws AuthenticationException {
210-
return this.builder.getOrBuild().authenticate(authentication);
211-
}
212-
213-
}
214-
215157
}

0 commit comments

Comments
 (0)