From 25cf1f698245ff91ed58bdd5e6e2649eaffe1d97 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 5 Jul 2016 10:56:02 +0100 Subject: [PATCH] Add new sample for oauth2 security --- .../src/main/resources/bootstrap.yml | 8 +- .../src/main/resources/keystore.jks | Bin 0 -> 2243 bytes oauth2-zuul/README.md | 1 + oauth2-zuul/pom.xml | 109 ++++++++++++++++++ .../src/main/java/demo/ZuulApplication.java | 14 +++ .../src/main/resources/application.properties | 6 + .../test/java/demo/ZuulApplicationTests.java | 35 ++++++ pom.xml | 1 + 8 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 configserver-bootstrap/src/main/resources/keystore.jks create mode 100644 oauth2-zuul/README.md create mode 100644 oauth2-zuul/pom.xml create mode 100644 oauth2-zuul/src/main/java/demo/ZuulApplication.java create mode 100644 oauth2-zuul/src/main/resources/application.properties create mode 100644 oauth2-zuul/src/test/java/demo/ZuulApplicationTests.java diff --git a/configserver-bootstrap/src/main/resources/bootstrap.yml b/configserver-bootstrap/src/main/resources/bootstrap.yml index 258dbb5..6d0b26e 100644 --- a/configserver-bootstrap/src/main/resources/bootstrap.yml +++ b/configserver-bootstrap/src/main/resources/bootstrap.yml @@ -6,7 +6,13 @@ spring: server: bootstrap: true git: - uri: https://github.com/spring-cloud-samples/config-repo + uri: file://${HOME}/dev/demo/config-repo +encrypt: + failOnError: false + keyStore: + location: classpath:keystore.jks + password: ${KEYSTORE_PASSWORD:foobar} # don't use a default in production + alias: test --- spring: diff --git a/configserver-bootstrap/src/main/resources/keystore.jks b/configserver-bootstrap/src/main/resources/keystore.jks new file mode 100644 index 0000000000000000000000000000000000000000..0ad98eade454db2617fd1b1dc54052c264710f81 GIT binary patch literal 2243 zcmchYX*3iHAI4|THjG^%nuLhPjIwWKh7lrUtkq37Tf6p@|APS=`!9oKu$`@ZL%)A#qo^Wph@`k&`K=lOFNxC;OP0CXte-+~0)3Jf|l zW;9SZ0sw%5QB=qQN)X1U3IjrbDj;DX5D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D!iA!ZkEeS{M`>rHMvkod0b#P-yM{*#Gl2R3P%7aU8x2Fcl~SKv98w zASw_Dc;!vL_~J(Afs%d=E2B}h+{5Rok+IA3(VHL+9^c~7f%|hZgcM{ep!_lamX1># zvZf(;)mWF;n;cu7AZBBg%_B4QFG|Few>pKHZHhDw_e84a2DVj=$skA47K++Z zq&+grK0qtJQp5(N<}3Nrm&TfyZwE@u&6^|f?_{O#Q03cV!bE!fc_RtfN0TOUS*4@f zATSUB%szpVL&+XCL{<30(Z6+(+;lYKrIEJW8%+$ z{cTTLv(&o&niDE{tl#W+=h8HV_d(>}U@KtDXvzy!mX5R2M7_#ECjO3b?Lg%5y;X4s z7dzc#ATy}WDK5UNXioMdOqsnw@|fE!$+pWRT5Ni`x7 + + 4.0.0 + + org.springframework.cloud.sample + spring-cloud-sample-oauth2-zuul + ${spring-cloud.version} + jar + + spring-cloud-sample-oauth2-zuul + Demo project for Spring Cloud + + + org.springframework.boot + spring-boot-starter-parent + 1.3.5.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + UTF-8 + 1.7 + Brixton.BUILD-SNAPSHOT + + + + + org.springframework.cloud + spring-cloud-starter-oauth2 + + + org.springframework.cloud + spring-cloud-starter-zuul + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + maven-deploy-plugin + + true + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/oauth2-zuul/src/main/java/demo/ZuulApplication.java b/oauth2-zuul/src/main/java/demo/ZuulApplication.java new file mode 100644 index 0000000..1e2ad46 --- /dev/null +++ b/oauth2-zuul/src/main/java/demo/ZuulApplication.java @@ -0,0 +1,14 @@ +package demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; + +@SpringBootApplication +@EnableOAuth2Sso +public class ZuulApplication { + + public static void main(String[] args) { + SpringApplication.run(ZuulApplication.class, args); + } +} diff --git a/oauth2-zuul/src/main/resources/application.properties b/oauth2-zuul/src/main/resources/application.properties new file mode 100644 index 0000000..7999064 --- /dev/null +++ b/oauth2-zuul/src/main/resources/application.properties @@ -0,0 +1,6 @@ +security.oauth2.client.clientId: acme +security.oauth2.client.clientSecret: acmesecret +security.oauth2.client.accessTokenUri: https://example.com +security.oauth2.client.userAuthorizationUri: https://example.com +security.oauth2.resource.user-info-uri: https://example.com +# debug: \ No newline at end of file diff --git a/oauth2-zuul/src/test/java/demo/ZuulApplicationTests.java b/oauth2-zuul/src/test/java/demo/ZuulApplicationTests.java new file mode 100644 index 0000000..79ac75d --- /dev/null +++ b/oauth2-zuul/src/test/java/demo/ZuulApplicationTests.java @@ -0,0 +1,35 @@ +package demo; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.client.RestTemplate; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = ZuulApplication.class) +@IntegrationTest({ "debug=true", "server.port=0" }) +@WebAppConfiguration +public class ZuulApplicationTests { + + private RestTemplate restTemplate = new TestRestTemplate(); + + @Value("${local.server.port}") + int port; + + @Test + public void useRestTemplate() throws Exception { + ResponseEntity entity = this.restTemplate.getForEntity("http://localhost:" + this.port, String.class); + assertEquals(HttpStatus.FOUND, entity.getStatusCode()); + assertEquals("http://localhost:" + this.port + "/login", entity.getHeaders().getLocation().toString()); + } + +} diff --git a/pom.xml b/pom.xml index 29bcd29..2d120e8 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ netflix-sidecar noweb oauth2-ribbon + oauth2-zuul ribbon-default-config ribbon-eureka sleuth