Skip to content

Commit ecee190

Browse files
author
Dave Syer
committed
Add some precautionary tests for documenting AuthenticationManager config
1 parent 7cb3ae4 commit ecee190

File tree

2 files changed

+131
-11
lines changed

2 files changed

+131
-11
lines changed

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfigurationTests.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616

1717
package org.springframework.boot.autoconfigure.security;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
23+
1924
import java.util.List;
2025
import java.util.concurrent.atomic.AtomicReference;
2126

27+
import org.junit.After;
2228
import org.junit.Test;
2329
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
2430
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
@@ -45,11 +51,6 @@
4551
import org.springframework.security.web.SecurityFilterChain;
4652
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
4753

48-
import static org.junit.Assert.assertEquals;
49-
import static org.junit.Assert.assertNotNull;
50-
import static org.junit.Assert.assertTrue;
51-
import static org.junit.Assert.fail;
52-
5354
/**
5455
* Tests for {@link SecurityAutoConfiguration}.
5556
*
@@ -59,6 +60,13 @@ public class SecurityAutoConfigurationTests {
5960

6061
private AnnotationConfigWebApplicationContext context;
6162

63+
@After
64+
public void close() {
65+
if (context != null) {
66+
context.close();
67+
}
68+
}
69+
6270
@Test
6371
public void testWebConfiguration() throws Exception {
6472
this.context = new AnnotationConfigWebApplicationContext();
@@ -137,11 +145,12 @@ public void onApplicationEvent(ApplicationEvent event) {
137145
public void testOverrideAuthenticationManager() throws Exception {
138146
this.context = new AnnotationConfigWebApplicationContext();
139147
this.context.setServletContext(new MockServletContext());
140-
this.context.register(TestConfiguration.class, SecurityAutoConfiguration.class,
141-
ServerPropertiesAutoConfiguration.class,
148+
this.context.register(TestAuthenticationConfiguration.class,
149+
SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class,
142150
PropertyPlaceholderAutoConfiguration.class);
143151
this.context.refresh();
144-
assertEquals(this.context.getBean(TestConfiguration.class).authenticationManager,
152+
assertEquals(
153+
this.context.getBean(TestAuthenticationConfiguration.class).authenticationManager,
145154
this.context.getBean(AuthenticationManager.class));
146155
}
147156

@@ -168,7 +177,7 @@ protected static class EntityConfiguration {
168177
}
169178

170179
@Configuration
171-
protected static class TestConfiguration {
180+
protected static class TestAuthenticationConfiguration {
172181

173182
private AuthenticationManager authenticationManager;
174183

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/SpringBootWebSecurityConfigurationTests.java

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,132 @@
1616

1717
package org.springframework.boot.autoconfigure.security;
1818

19-
import org.junit.Test;
20-
19+
import static org.junit.Assert.assertNotNull;
2120
import static org.junit.Assert.assertTrue;
2221

22+
import java.lang.annotation.Documented;
23+
import java.lang.annotation.ElementType;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
28+
import org.junit.After;
29+
import org.junit.Test;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.boot.SpringApplication;
32+
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
33+
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
34+
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
35+
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
36+
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
37+
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
38+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
39+
import org.springframework.context.ConfigurableApplicationContext;
40+
import org.springframework.context.annotation.Configuration;
41+
import org.springframework.context.annotation.Import;
42+
import org.springframework.core.Ordered;
43+
import org.springframework.core.annotation.Order;
44+
import org.springframework.security.authentication.AuthenticationManager;
45+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
46+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
47+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
48+
import org.springframework.security.config.annotation.web.builders.WebSecurity;
49+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
50+
2351
/**
2452
* Tests for {@link SpringBootWebSecurityConfiguration}.
2553
*
2654
* @author Dave Syer
2755
*/
2856
public class SpringBootWebSecurityConfigurationTests {
2957

58+
private ConfigurableApplicationContext context;
59+
60+
@After
61+
public void close() {
62+
if (context != null) {
63+
context.close();
64+
}
65+
}
66+
3067
@Test
3168
public void testDefaultIgnores() {
3269
assertTrue(SpringBootWebSecurityConfiguration
3370
.getIgnored(new SecurityProperties()).contains("/css/**"));
3471
}
3572

73+
@Test
74+
public void testWebConfigurationOverrideGlobalAuthentication() throws Exception {
75+
this.context = SpringApplication.run(TestWebConfiguration.class,
76+
"--server.port=0", "--debug");
77+
assertNotNull(this.context.getBean(AuthenticationManagerBuilder.class));
78+
assertNotNull(this.context.getBean(AuthenticationManager.class).authenticate(
79+
new UsernamePasswordAuthenticationToken("dave", "secret")));
80+
}
81+
82+
@Test
83+
public void testWebConfigurationInjectGlobalAuthentication() throws Exception {
84+
this.context = SpringApplication.run(TestInjectWebConfiguration.class,
85+
"--server.port=0", "--debug");
86+
assertNotNull(this.context.getBean(AuthenticationManagerBuilder.class));
87+
assertNotNull(this.context.getBean(AuthenticationManager.class).authenticate(
88+
new UsernamePasswordAuthenticationToken("dave", "secret")));
89+
}
90+
91+
@Configuration
92+
@Import(TestWebConfiguration.class)
93+
@Order(Ordered.LOWEST_PRECEDENCE)
94+
protected static class TestInjectWebConfiguration extends
95+
WebSecurityConfigurerAdapter {
96+
97+
// It's a bad idea to inject an AuthenticationManager into a
98+
// WebSecurityConfigurerAdapter because it can cascade early instantiation,
99+
// unless you explicitly want the Boot default AuthenticationManager. It's
100+
// better to inject the builder, if you want the global AuthenticationManager. It
101+
// might even be necessary to wrap the builder in a lazy AuthenticationManager
102+
// (that calls getOrBuild() only when the AuthenticationManager is actually
103+
// called).
104+
@Autowired
105+
private AuthenticationManagerBuilder auth;
106+
107+
@Override
108+
public void init(WebSecurity web) throws Exception {
109+
auth.getOrBuild();
110+
}
111+
}
112+
113+
@MinimalWebConfiguration
114+
@Import(SecurityAutoConfiguration.class)
115+
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
116+
protected static class TestWebConfiguration extends WebSecurityConfigurerAdapter {
117+
118+
@Autowired
119+
public void init(AuthenticationManagerBuilder auth) throws Exception {
120+
// @formatter:off
121+
auth.inMemoryAuthentication()
122+
.withUser("dave")
123+
.password("secret")
124+
.roles("USER");
125+
// @formatter:on
126+
}
127+
128+
@Override
129+
protected void configure(HttpSecurity http) throws Exception {
130+
http.authorizeRequests().anyRequest().denyAll();
131+
}
132+
133+
}
134+
135+
@Configuration
136+
@Target(ElementType.TYPE)
137+
@Retention(RetentionPolicy.RUNTIME)
138+
@Documented
139+
@Import({ EmbeddedServletContainerAutoConfiguration.class,
140+
ServerPropertiesAutoConfiguration.class,
141+
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
142+
HttpMessageConvertersAutoConfiguration.class,
143+
ErrorMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
144+
protected static @interface MinimalWebConfiguration {
145+
}
146+
36147
}

0 commit comments

Comments
 (0)