Skip to content

Commit 0aa9303

Browse files
author
Dave Syer
committed
Fix secure method configuration global authentication
This fixes a bug in the sample, where the AuthenticationManager it builds is a local one for the filter chain containing "/login", whereas it was expecting to override the Boot default, which is "global". The fix is to extract the authentication configuration out into a GlobalAuthenticationConfigurerAdapter. Fixes gh-699
1 parent e4b8e17 commit 0aa9303

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

spring-boot-samples/spring-boot-sample-web-method-security/src/main/java/sample/ui/method/SampleMethodSecurityApplication.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import org.springframework.boot.builder.SpringApplicationBuilder;
2424
import org.springframework.context.annotation.Bean;
2525
import org.springframework.context.annotation.ComponentScan;
26+
import org.springframework.context.annotation.Configuration;
2627
import org.springframework.core.Ordered;
2728
import org.springframework.core.annotation.Order;
2829
import org.springframework.security.access.annotation.Secured;
2930
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
31+
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
3032
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
3133
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3234
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -70,17 +72,22 @@ public ApplicationSecurity applicationSecurity() {
7072
return new ApplicationSecurity();
7173
}
7274

73-
@Order(Ordered.LOWEST_PRECEDENCE - 8)
74-
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
75-
75+
@Order(Ordered.HIGHEST_PRECEDENCE)
76+
@Configuration
77+
protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
78+
7679
@Override
77-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
80+
public void init(AuthenticationManagerBuilder auth) throws Exception {
7881
// @formatter:off
7982
auth.inMemoryAuthentication().withUser("admin").password("admin")
8083
.roles("ADMIN", "USER").and().withUser("user").password("user")
8184
.roles("USER");
8285
// @formatter:on
8386
}
87+
}
88+
89+
@Order(Ordered.LOWEST_PRECEDENCE - 8)
90+
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
8491

8592
@Override
8693
protected void configure(HttpSecurity http) throws Exception {

spring-boot-samples/spring-boot-sample-web-method-security/src/test/java/sample/ui/method/SampleMethodSecurityApplicationTests.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616

1717
package sample.ui.method;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
21+
1922
import java.util.Arrays;
2023
import java.util.regex.Matcher;
2124
import java.util.regex.Pattern;
2225

23-
import org.junit.Ignore;
2426
import org.junit.Test;
2527
import org.junit.runner.RunWith;
2628
import org.springframework.beans.factory.annotation.Value;
2729
import org.springframework.boot.test.IntegrationTest;
28-
import org.springframework.boot.test.TestRestTemplate;
2930
import org.springframework.boot.test.SpringApplicationConfiguration;
31+
import org.springframework.boot.test.TestRestTemplate;
3032
import org.springframework.http.HttpEntity;
3133
import org.springframework.http.HttpHeaders;
3234
import org.springframework.http.HttpMethod;
@@ -39,9 +41,6 @@
3941
import org.springframework.util.LinkedMultiValueMap;
4042
import org.springframework.util.MultiValueMap;
4143

42-
import static org.junit.Assert.assertEquals;
43-
import static org.junit.Assert.assertTrue;
44-
4544
/**
4645
* Basic integration tests for demo application.
4746
*
@@ -117,13 +116,19 @@ public void testManagementProtected() throws Exception {
117116
}
118117

119118
@Test
120-
@Ignore("https://github.com/spring-projects/spring-boot/issues/699")
121119
public void testManagementAuthorizedAccess() throws Exception {
122-
ResponseEntity<String> entity = new TestRestTemplate("user", "user")
120+
ResponseEntity<String> entity = new TestRestTemplate("admin", "admin")
123121
.getForEntity("http://localhost:" + port + "/beans", String.class);
124122
assertEquals(HttpStatus.OK, entity.getStatusCode());
125123
}
126124

125+
@Test
126+
public void testManagementUnauthorizedAccess() throws Exception {
127+
ResponseEntity<String> entity = new TestRestTemplate("user", "user")
128+
.getForEntity("http://localhost:" + port + "/beans", String.class);
129+
assertEquals(HttpStatus.FORBIDDEN, entity.getStatusCode());
130+
}
131+
127132
private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) {
128133
ResponseEntity<String> page = new TestRestTemplate().getForEntity(
129134
"http://localhost:" + port + "/login", String.class);

0 commit comments

Comments
 (0)