|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.security;
|
18 | 18 |
|
19 |
| -import org.junit.Test; |
20 |
| - |
| 19 | +import static org.junit.Assert.assertNotNull; |
21 | 20 | import static org.junit.Assert.assertTrue;
|
22 | 21 |
|
| 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 | + |
23 | 51 | /**
|
24 | 52 | * Tests for {@link SpringBootWebSecurityConfiguration}.
|
25 | 53 | *
|
26 | 54 | * @author Dave Syer
|
27 | 55 | */
|
28 | 56 | public class SpringBootWebSecurityConfigurationTests {
|
29 | 57 |
|
| 58 | + private ConfigurableApplicationContext context; |
| 59 | + |
| 60 | + @After |
| 61 | + public void close() { |
| 62 | + if (context != null) { |
| 63 | + context.close(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
30 | 67 | @Test
|
31 | 68 | public void testDefaultIgnores() {
|
32 | 69 | assertTrue(SpringBootWebSecurityConfiguration
|
33 | 70 | .getIgnored(new SecurityProperties()).contains("/css/**"));
|
34 | 71 | }
|
35 | 72 |
|
| 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 | + |
36 | 147 | }
|
0 commit comments