Skip to content

Commit ec77949

Browse files
author
Dave Syer
committed
Switch off CSRF filter
Might need to revisit later.
1 parent d852f29 commit ec77949

File tree

10 files changed

+195
-132
lines changed

10 files changed

+195
-132
lines changed

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

Lines changed: 108 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
package org.springframework.boot.actuate.autoconfigure;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.LinkedHashSet;
2022
import java.util.List;
23+
import java.util.Set;
2124

2225
import org.apache.commons.logging.Log;
2326
import org.apache.commons.logging.LogFactory;
2427
import org.springframework.beans.factory.annotation.Autowired;
2528
import org.springframework.boot.actuate.endpoint.Endpoint;
2629
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
2730
import org.springframework.boot.actuate.properties.ManagementServerProperties;
28-
import org.springframework.boot.actuate.properties.ManagementServerProperties.User;
2931
import org.springframework.boot.actuate.properties.SecurityProperties;
32+
import org.springframework.boot.actuate.properties.SecurityProperties.User;
3033
import org.springframework.boot.actuate.web.ErrorController;
3134
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3235
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -40,13 +43,14 @@
4043
import org.springframework.security.authentication.AuthenticationManager;
4144
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
4245
import org.springframework.security.authentication.ProviderManager;
46+
import org.springframework.security.config.annotation.ObjectPostProcessor;
4347
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
48+
import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer;
4449
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4550
import org.springframework.security.config.annotation.web.builders.WebSecurity;
4651
import org.springframework.security.config.annotation.web.builders.WebSecurity.IgnoredRequestConfigurer;
4752
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
4853
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
49-
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
5054
import org.springframework.security.web.AuthenticationEntryPoint;
5155
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
5256

@@ -85,6 +89,7 @@
8589
@Configuration
8690
@ConditionalOnClass({ EnableWebSecurity.class })
8791
@EnableWebSecurity
92+
// (debug = true)
8893
@EnableConfigurationProperties
8994
public class SecurityAutoConfiguration {
9095

@@ -101,27 +106,25 @@ public AuthenticationEventPublisher authenticationEventPublisher() {
101106
}
102107

103108
@Bean
104-
@ConditionalOnMissingBean({ BoostrapWebSecurityConfigurerAdapter.class })
105-
public WebSecurityConfigurerAdapter webSecurityConfigurerAdapter() {
106-
return new BoostrapWebSecurityConfigurerAdapter();
109+
@ConditionalOnMissingBean({ ApplicationWebSecurityConfigurerAdapter.class })
110+
public WebSecurityConfigurerAdapter applicationWebSecurityConfigurerAdapter() {
111+
return new ApplicationWebSecurityConfigurerAdapter();
112+
}
113+
114+
@Bean
115+
@ConditionalOnMissingBean({ ManagementWebSecurityConfigurerAdapter.class })
116+
public WebSecurityConfigurerAdapter managementWebSecurityConfigurerAdapter() {
117+
return new ManagementWebSecurityConfigurerAdapter();
107118
}
108119

109120
// Give user-supplied filters a chance to be last in line
110-
@Order(Ordered.LOWEST_PRECEDENCE - 10)
111-
private static class BoostrapWebSecurityConfigurerAdapter extends
121+
@Order(Ordered.LOWEST_PRECEDENCE - 5)
122+
private static class ApplicationWebSecurityConfigurerAdapter extends
112123
WebSecurityConfigurerAdapter {
113124

114-
private static final String[] NO_PATHS = new String[0];
115-
116125
@Autowired
117126
private SecurityProperties security;
118127

119-
@Autowired
120-
private ManagementServerProperties management;
121-
122-
@Autowired(required = false)
123-
private EndpointHandlerMapping endpointHandlerMapping;
124-
125128
@Autowired
126129
private AuthenticationEventPublisher authenticationEventPublisher;
127130

@@ -135,26 +138,20 @@ protected void configure(HttpSecurity http) throws Exception {
135138
http.requiresChannel().anyRequest().requiresSecure();
136139
}
137140

138-
if (this.security.getBasic().isEnabled()) {
141+
String[] paths = getSecureApplicationPaths();
142+
if (this.security.getBasic().isEnabled() && paths.length > 0) {
139143
http.exceptionHandling().authenticationEntryPoint(entryPoint());
140-
http.httpBasic().and().anonymous().disable();
141-
ExpressionUrlAuthorizationConfigurer<HttpSecurity> authorizeUrls = http
142-
.authorizeUrls();
143-
String[] paths = getEndpointPaths(true);
144-
if (paths.length > 0) {
145-
authorizeUrls.antMatchers(getEndpointPaths(true)).hasRole(
146-
this.management.getUser().getRole());
147-
}
148-
paths = getSecureApplicationPaths();
149-
if (paths.length > 0) {
150-
authorizeUrls.antMatchers(getSecureApplicationPaths()).hasRole(
151-
this.security.getBasic().getRole());
152-
}
153-
authorizeUrls.and().httpBasic();
144+
http.requestMatchers().antMatchers(paths);
145+
http.authorizeRequests().anyRequest()
146+
.hasRole(this.security.getUser().getRole()) //
147+
.and().httpBasic() //
148+
.and().anonymous().disable();
154149
}
155-
156-
// No cookies for service endpoints by default
150+
// Remove this when session creation is disabled by default
151+
http.csrf().disable();
152+
// No cookies for application endpoints by default
157153
http.sessionManagement().sessionCreationPolicy(this.security.getSessions());
154+
158155
}
159156

160157
private String[] getSecureApplicationPaths() {
@@ -181,12 +178,74 @@ private AuthenticationEntryPoint entryPoint() {
181178
public void configure(WebSecurity builder) throws Exception {
182179
IgnoredRequestConfigurer ignoring = builder.ignoring();
183180
ignoring.antMatchers(this.security.getIgnored());
184-
ignoring.antMatchers(getEndpointPaths(false));
185181
if (this.errorController != null) {
186182
ignoring.antMatchers(this.errorController.getErrorPath());
187183
}
188184
}
189185

186+
@Override
187+
protected AuthenticationManager authenticationManager() throws Exception {
188+
AuthenticationManager manager = super.authenticationManager();
189+
if (manager instanceof ProviderManager) {
190+
((ProviderManager) manager)
191+
.setAuthenticationEventPublisher(this.authenticationEventPublisher);
192+
}
193+
return manager;
194+
}
195+
196+
}
197+
198+
// Give user-supplied filters a chance to be last in line
199+
@Order(Ordered.LOWEST_PRECEDENCE - 10)
200+
private static class ManagementWebSecurityConfigurerAdapter extends
201+
WebSecurityConfigurerAdapter {
202+
203+
private static final String[] NO_PATHS = new String[0];
204+
205+
@Autowired
206+
private SecurityProperties security;
207+
208+
@Autowired
209+
private ManagementServerProperties management;
210+
211+
@Autowired(required = false)
212+
private EndpointHandlerMapping endpointHandlerMapping;
213+
214+
@Override
215+
protected void configure(HttpSecurity http) throws Exception {
216+
217+
if (this.security.isRequireSsl()) {
218+
http.requiresChannel().anyRequest().requiresSecure();
219+
}
220+
221+
String[] paths = getEndpointPaths(true);
222+
if (this.security.getBasic().isEnabled() && paths.length > 0) {
223+
http.exceptionHandling().authenticationEntryPoint(entryPoint());
224+
http.requestMatchers().antMatchers(paths);
225+
http.authorizeRequests().anyRequest()
226+
.hasRole(this.security.getManagement().getRole()) //
227+
.and().httpBasic() //
228+
.and().anonymous().disable();
229+
}
230+
// No cookies for management endpoints by default
231+
http.csrf().disable();
232+
http.sessionManagement().sessionCreationPolicy(
233+
this.security.getManagement().getSessions());
234+
235+
}
236+
237+
@Override
238+
public void configure(WebSecurity builder) throws Exception {
239+
IgnoredRequestConfigurer ignoring = builder.ignoring();
240+
ignoring.antMatchers(getEndpointPaths(false));
241+
}
242+
243+
private AuthenticationEntryPoint entryPoint() {
244+
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
245+
entryPoint.setRealmName(this.security.getBasic().getRealm());
246+
return entryPoint;
247+
}
248+
190249
private String[] getEndpointPaths(boolean secure) {
191250
if (this.endpointHandlerMapping == null) {
192251
return NO_PATHS;
@@ -202,16 +261,6 @@ private String[] getEndpointPaths(boolean secure) {
202261
return paths.toArray(new String[paths.size()]);
203262
}
204263

205-
@Override
206-
protected AuthenticationManager authenticationManager() throws Exception {
207-
AuthenticationManager manager = super.authenticationManager();
208-
if (manager instanceof ProviderManager) {
209-
((ProviderManager) manager)
210-
.setAuthenticationEventPublisher(this.authenticationEventPublisher);
211-
}
212-
return manager;
213-
}
214-
215264
}
216265

217266
@ConditionalOnMissingBean(AuthenticationManager.class)
@@ -222,23 +271,28 @@ public static class AuthenticationManagerConfiguration {
222271
.getLog(AuthenticationManagerConfiguration.class);
223272

224273
@Autowired
225-
private ManagementServerProperties management;
274+
private SecurityProperties security;
226275

227276
@Bean
228277
public AuthenticationManager authenticationManager() throws Exception {
229-
User user = this.management.getUser();
278+
279+
InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> builder = new AuthenticationManagerBuilder(
280+
ObjectPostProcessor.QUIESCENT_POSTPROCESSOR).inMemoryAuthentication();
281+
User user = this.security.getUser();
282+
230283
if (user.isDefaultPassword()) {
231-
logger.info("Using default password for management endpoints: "
284+
logger.info("Using default password for application endpoints: "
232285
+ user.getPassword());
233286
}
234-
List<String> roles = new ArrayList<String>();
235-
roles.add("USER");
236-
if (!"USER".equals(user.getRole())) {
237-
roles.add(user.getRole());
238-
}
239-
return new AuthenticationManagerBuilder().inMemoryAuthentication()
240-
.withUser(user.getName()).password(user.getPassword())
241-
.roles(roles.toArray(new String[roles.size()])).and().and().build();
287+
288+
Set<String> roles = new LinkedHashSet<String>(Arrays.asList(this.security
289+
.getManagement().getRole(), user.getRole()));
290+
291+
builder.withUser(user.getName()).password(user.getPassword())
292+
.roles(roles.toArray(new String[roles.size()]));
293+
294+
return builder.and().build();
295+
242296
}
243297

244298
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ManagementServerProperties.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.actuate.properties;
1818

1919
import java.net.InetAddress;
20-
import java.util.UUID;
2120

2221
import javax.validation.constraints.NotNull;
2322

@@ -40,14 +39,8 @@ public class ManagementServerProperties {
4039
@NotNull
4140
private String contextPath = "";
4241

43-
private User user = new User();
44-
4542
private boolean allowShutdown = false;
4643

47-
public User getUser() {
48-
return this.user;
49-
}
50-
5144
public boolean isAllowShutdown() {
5245
return this.allowShutdown;
5346
}
@@ -89,45 +82,4 @@ public void setContextPath(String contextPath) {
8982
this.contextPath = contextPath;
9083
}
9184

92-
public static class User {
93-
94-
private String name = "user";
95-
96-
private String password = UUID.randomUUID().toString();
97-
98-
private String role = "ADMIN";
99-
100-
private boolean defaultPassword;
101-
102-
public String getName() {
103-
return this.name;
104-
}
105-
106-
public void setName(String name) {
107-
this.name = name;
108-
}
109-
110-
public String getPassword() {
111-
return this.password;
112-
}
113-
114-
public void setPassword(String password) {
115-
this.defaultPassword = false;
116-
this.password = password;
117-
}
118-
119-
public String getRole() {
120-
return this.role;
121-
}
122-
123-
public void setRole(String role) {
124-
this.role = role;
125-
}
126-
127-
public boolean isDefaultPassword() {
128-
return this.defaultPassword;
129-
}
130-
131-
}
132-
13385
}

0 commit comments

Comments
 (0)