Skip to content

Commit 1231da1

Browse files
committed
Add security.basic.authorize-mode property
Add a `security.basic.authorize-mode` property that can be used to affect how basic security authorization is applied. Fixes gh-2462
1 parent f7221be commit 1231da1

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 org.springframework.boot.autoconfigure.security;
18+
19+
/**
20+
* Security authorization modes as specified in {@link SecurityProperties}.
21+
*
22+
* @author Phillip Webb
23+
* @since 1.2.2
24+
*/
25+
public enum SecurityAuthorizeMode {
26+
27+
/**
28+
* Must be a member of one of the security roles.
29+
*/
30+
ROLE,
31+
32+
/**
33+
* Must be an authenticated user.
34+
*/
35+
AUTHENTICATED,
36+
37+
/**
38+
* No security authorization is setup.
39+
*/
40+
NONE
41+
42+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ public static class Basic {
238238
*/
239239
private String[] path = new String[] { "/**" };
240240

241+
/**
242+
* The security authorize mode to apply.
243+
*/
244+
private SecurityAuthorizeMode authorizeMode = SecurityAuthorizeMode.ROLE;
245+
241246
public boolean isEnabled() {
242247
return this.enabled;
243248
}
@@ -262,6 +267,14 @@ public void setPath(String... paths) {
262267
this.path = paths;
263268
}
264269

270+
public SecurityAuthorizeMode getAuthorizeMode() {
271+
return this.authorizeMode;
272+
}
273+
274+
public void setAuthorizeMode(SecurityAuthorizeMode authorizeMode) {
275+
this.authorizeMode = authorizeMode;
276+
}
277+
265278
}
266279

267280
public static class User {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,14 @@ protected void configure(HttpSecurity http) throws Exception {
252252
http.exceptionHandling().authenticationEntryPoint(entryPoint);
253253
http.httpBasic().authenticationEntryPoint(entryPoint);
254254
http.requestMatchers().antMatchers(paths);
255-
String[] role = this.security.getUser().getRole().toArray(new String[0]);
256-
http.authorizeRequests().anyRequest().hasAnyRole(role);
255+
String[] roles = this.security.getUser().getRole().toArray(new String[0]);
256+
SecurityAuthorizeMode mode = this.security.getBasic().getAuthorizeMode();
257+
if (mode == null || mode == SecurityAuthorizeMode.ROLE) {
258+
http.authorizeRequests().anyRequest().hasAnyRole(roles);
259+
}
260+
else if (mode == SecurityAuthorizeMode.AUTHENTICATED) {
261+
http.authorizeRequests().anyRequest().authenticated();
262+
}
257263
}
258264
}
259265

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,37 @@ public void testWebConfigurationFilterChainUnauthenticated() throws Exception {
103103
Matchers.containsString("realm=\"Spring\"")));
104104
}
105105

106+
@Test
107+
public void testWebConfigurationFilterChainUnauthenticatedWithAuthorizeModeNone()
108+
throws Exception {
109+
this.context = SpringApplication.run(VanillaWebConfiguration.class,
110+
"--server.port=0", "--security.basic.authorize-mode=none");
111+
MockMvc mockMvc = MockMvcBuilders
112+
.webAppContextSetup((WebApplicationContext) this.context)
113+
.addFilters(
114+
this.context.getBean("springSecurityFilterChain", Filter.class))
115+
.build();
116+
mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(
117+
MockMvcResultMatchers.status().isNotFound());
118+
}
119+
120+
@Test
121+
public void testWebConfigurationFilterChainUnauthenticatedWithAuthorizeModeAuthenticated()
122+
throws Exception {
123+
this.context = SpringApplication.run(VanillaWebConfiguration.class,
124+
"--server.port=0", "--security.basic.authorize-mode=authenticated");
125+
MockMvc mockMvc = MockMvcBuilders
126+
.webAppContextSetup((WebApplicationContext) this.context)
127+
.addFilters(
128+
this.context.getBean("springSecurityFilterChain", Filter.class))
129+
.build();
130+
mockMvc.perform(MockMvcRequestBuilders.get("/"))
131+
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
132+
.andExpect(
133+
MockMvcResultMatchers.header().string("www-authenticate",
134+
Matchers.containsString("realm=\"Spring\"")));
135+
}
136+
106137
@Test
107138
public void testWebConfigurationFilterChainBadCredentials() throws Exception {
108139
this.context = SpringApplication.run(VanillaWebConfiguration.class,
@@ -164,10 +195,8 @@ protected static class TestWebConfiguration extends WebSecurityConfigurerAdapter
164195

165196
@Autowired
166197
public void init(AuthenticationManagerBuilder auth) throws Exception {
167-
// @formatter:off
168198
auth.inMemoryAuthentication().withUser("dave").password("secret")
169199
.roles("USER");
170-
// @formatter:on
171200
}
172201

173202
@Override

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ content into your application; rather pick only the properties that you need.
211211
security.basic.enabled=true
212212
security.basic.realm=Spring
213213
security.basic.path= # /**
214+
security.basic.authorize-mode= # ROLE, AUTHENTICATED, NONE
214215
security.filter-order=0
215216
security.headers.xss=false
216217
security.headers.cache=false

0 commit comments

Comments
 (0)