[bs-129] Split up SecurityConfiguration and makes bits of it optional

* Added security.basic.* and security.sessions
* Also security.enabled (so you can switch it off on command line)

[Fixes #50181269]
This commit is contained in:
Dave Syer
2013-05-24 06:01:40 +01:00
parent e649ef44cc
commit 514cf3dcc1
10 changed files with 193 additions and 23 deletions

View File

@@ -4,3 +4,4 @@ handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINE
sun.net.www.protocol.http.HttpURLConnection.level = ALL
org.springframework.bootstrap.level = ALL
org.springframework.security.level = ALL

View File

@@ -1,4 +1,4 @@
package org.springframework.bootstrap.sample.service;
package org.springframework.bootstrap.sample.test;
import java.io.IOException;
import java.util.ArrayList;
@@ -13,6 +13,7 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.actuate.properties.EndpointsProperties;
import org.springframework.bootstrap.sample.service.ServiceBootstrapApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

View File

@@ -1,4 +1,4 @@
package org.springframework.bootstrap.sample.service;
package org.springframework.bootstrap.sample.test;
import java.io.IOException;
import java.util.ArrayList;
@@ -13,6 +13,7 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.sample.service.ServiceBootstrapApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;

View File

@@ -1,4 +1,4 @@
package org.springframework.bootstrap.sample.service;
package org.springframework.bootstrap.sample.test;
import java.io.IOException;
import java.util.Map;
@@ -11,6 +11,7 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.sample.service.ServiceBootstrapApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -61,7 +62,7 @@ public class ManagementServiceBootstrapApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = getRestTemplate().getForEntity(
"http://localhost:" + port, Map.class);
assertEquals(HttpStatus.FORBIDDEN, entity.getStatusCode());
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
}
@Test

View File

@@ -1,4 +1,4 @@
package org.springframework.bootstrap.sample.service;
package org.springframework.bootstrap.sample.test;
import java.io.IOException;
import java.util.ArrayList;
@@ -13,6 +13,7 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.sample.service.ServiceBootstrapApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;

View File

@@ -1,4 +1,4 @@
package org.springframework.bootstrap.sample.service;
package org.springframework.bootstrap.sample.test;
import java.io.IOException;
import java.util.ArrayList;
@@ -13,6 +13,7 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.sample.service.ServiceBootstrapApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
@@ -27,6 +28,7 @@ import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
@@ -65,11 +67,11 @@ public class ServiceBootstrapApplicationTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = getRestTemplate().getForEntity(
"http://localhost:8080", Map.class);
// FIXME: should be UNAUTHORIZED?
assertEquals(HttpStatus.FORBIDDEN, entity.getStatusCode());
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Wrong body: " + body, "Forbidden", body.get("error"));
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
assertFalse(entity.getHeaders().containsKey("Set-Cookie"));
}
@Test
@@ -117,7 +119,7 @@ public class ServiceBootstrapApplicationTests {
@Test
public void testErrorPageDirectAccess() throws Exception {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = getRestTemplate("user", "password").getForEntity(
ResponseEntity<Map> entity = getRestTemplate().getForEntity(
"http://localhost:8080/error", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked")

View File

@@ -1,4 +1,4 @@
package org.springframework.bootstrap.sample.service;
package org.springframework.bootstrap.sample.test;
import java.io.IOException;
import java.util.ArrayList;
@@ -13,6 +13,7 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.sample.service.ServiceBootstrapApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;

View File

@@ -0,0 +1,81 @@
package org.springframework.bootstrap.sample.test;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.sample.service.ServiceBootstrapApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* Integration tests for unsecured service endpoints (even with Spring Security on
* classpath).
*
* @author Dave Syer
*
*/
public class UnsecureServiceBootstrapApplicationTests {
private static ConfigurableApplicationContext context;
@BeforeClass
public static void start() throws Exception {
Future<ConfigurableApplicationContext> future = Executors
.newSingleThreadExecutor().submit(
new Callable<ConfigurableApplicationContext>() {
@Override
public ConfigurableApplicationContext call() throws Exception {
return (ConfigurableApplicationContext) SpringApplication
.run(ServiceBootstrapApplication.class,
"--security.basic.enabled=false");
}
});
context = future.get(10, TimeUnit.SECONDS);
}
@AfterClass
public static void stop() {
if (context != null) {
context.close();
}
}
@Test
public void testHome() throws Exception {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = getRestTemplate().getForEntity(
"http://localhost:8080", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("Hello Phil", body.get("message"));
assertFalse(entity.getHeaders().containsKey("Set-Cookie"));
}
private RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
}
});
return restTemplate;
}
}