Skip to content

Commit 8622e5d

Browse files
committed
Apply f9816ea and 64e94f3 to 1.1.x
Fixes gh-2473
1 parent b29c5a3 commit 8622e5d

File tree

7 files changed

+254
-2
lines changed

7 files changed

+254
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.autoconfigure.security;
17+
18+
import java.util.Map;
19+
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
28+
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
29+
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
30+
31+
/**
32+
* This works with the {@link AuthenticationConfiguration} to ensure that users are able
33+
* to use:
34+
*
35+
* <pre>
36+
* public void configureGlobal(AuthenticationManagerBuilder auth) {
37+
* ...
38+
* }
39+
* </pre>
40+
*
41+
* within their classes annotated with {@link EnableAutoConfiguration}.
42+
*
43+
* @author Rob Winch
44+
* @since 1.1.11
45+
*/
46+
@Configuration
47+
@ConditionalOnClass(GlobalAuthenticationConfigurerAdapter.class)
48+
public class BootGlobalAuthenticationConfiguration {
49+
50+
@Bean
51+
public static BootGlobalAuthenticationConfigurationAdapter bootGlobalAuthenticationConfigurationAdapter(
52+
ApplicationContext context) {
53+
return new BootGlobalAuthenticationConfigurationAdapter(context);
54+
}
55+
56+
private static class BootGlobalAuthenticationConfigurationAdapter extends
57+
GlobalAuthenticationConfigurerAdapter {
58+
59+
private static Log logger = LogFactory
60+
.getLog(BootGlobalAuthenticationConfiguration.class);
61+
62+
private final ApplicationContext context;
63+
64+
public BootGlobalAuthenticationConfigurationAdapter(ApplicationContext context) {
65+
this.context = context;
66+
}
67+
68+
@Override
69+
public void init(AuthenticationManagerBuilder auth) {
70+
Map<String, Object> beansWithAnnotation = this.context
71+
.getBeansWithAnnotation(EnableAutoConfiguration.class);
72+
if (logger.isDebugEnabled()) {
73+
logger.debug("Eagerly initializing " + beansWithAnnotation);
74+
}
75+
}
76+
}
77+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
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.
@@ -49,7 +49,8 @@
4949
GlobalAuthenticationConfigurerAdapter.class })
5050
@EnableConfigurationProperties
5151
@Import({ SpringBootWebSecurityConfiguration.class,
52-
AuthenticationManagerConfiguration.class })
52+
AuthenticationManagerConfiguration.class,
53+
BootGlobalAuthenticationConfiguration.class })
5354
public class SecurityAutoConfiguration {
5455

5556
@Bean

spring-boot-full-build/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<module>../spring-boot-cli</module>
5757
<module>../spring-boot-samples</module>
5858
<module>../spring-boot-integration-tests</module>
59+
<module>../spring-boot-security-tests</module>
5960
<module>../spring-boot-docs</module>
6061
</modules>
6162
<profiles>

spring-boot-security-tests/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-parent</artifactId>
7+
<version>1.1.11.BUILD-SNAPSHOT</version>
8+
<relativePath>../spring-boot-parent</relativePath>
9+
</parent>
10+
<artifactId>spring-boot-security-tests</artifactId>
11+
<packaging>pom</packaging>
12+
<name>Spring Boot Security Tests</name>
13+
<description>${project.name}</description>
14+
<url>http://projects.spring.io/spring-boot/</url>
15+
<organization>
16+
<name>Pivotal Software, Inc.</name>
17+
<url>http://www.spring.io</url>
18+
</organization>
19+
<modules>
20+
<module>spring-boot-security-tests-web-helloworld</module>
21+
</modules>
22+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-security-tests</artifactId>
7+
<version>1.1.11.BUILD-SNAPSHOT</version>
8+
<relativePath>../</relativePath>
9+
</parent>
10+
<artifactId>spring-boot-security-tests-web-helloworld</artifactId>
11+
<name>Spring Boot Security Tests - Web Hello World</name>
12+
<description>${project.name}</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/../..</main.basedir>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-security</artifactId>
33+
</dependency>
34+
</dependencies>
35+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
22+
import org.springframework.context.annotation.ComponentScan;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
25+
26+
@EnableAutoConfiguration
27+
@ComponentScan
28+
@Configuration
29+
public class HelloWebSecurityApplication {
30+
31+
@Autowired
32+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
33+
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
34+
}
35+
36+
public static void main(String[] args) {
37+
SpringApplication.run(HelloWebSecurityApplication.class, args);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import javax.servlet.http.HttpServletResponse;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.IntegrationTest;
26+
import org.springframework.boot.test.SpringApplicationConfiguration;
27+
import org.springframework.mock.web.MockFilterChain;
28+
import org.springframework.mock.web.MockHttpServletRequest;
29+
import org.springframework.mock.web.MockHttpServletResponse;
30+
import org.springframework.security.crypto.codec.Base64;
31+
import org.springframework.security.web.FilterChainProxy;
32+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33+
import org.springframework.test.context.web.WebAppConfiguration;
34+
35+
import static org.hamcrest.Matchers.equalTo;
36+
import static org.junit.Assert.assertThat;
37+
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@SpringApplicationConfiguration(classes = HelloWebSecurityApplication.class)
40+
@IntegrationTest("server.port:0")
41+
@WebAppConfiguration
42+
public class HelloWebSecurityApplicationTests {
43+
44+
@Autowired
45+
private FilterChainProxy springSecurityFilterChain;
46+
47+
private MockHttpServletRequest request;
48+
49+
private MockHttpServletResponse response;
50+
51+
private MockFilterChain chain;
52+
53+
@Before
54+
public void setup() {
55+
this.request = new MockHttpServletRequest();
56+
this.response = new MockHttpServletResponse();
57+
this.chain = new MockFilterChain();
58+
}
59+
60+
@Test
61+
public void requiresAuthentication() throws Exception {
62+
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
63+
64+
assertThat(this.response.getStatus(),
65+
equalTo(HttpServletResponse.SC_UNAUTHORIZED));
66+
}
67+
68+
@Test
69+
public void userAuthenticates() throws Exception {
70+
this.request.addHeader("Authorization",
71+
"Basic " + new String(Base64.encode("user:password".getBytes("UTF-8"))));
72+
73+
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
74+
75+
assertThat(this.response.getStatus(), equalTo(HttpServletResponse.SC_OK));
76+
}
77+
}

0 commit comments

Comments
 (0)