Add new sample for oauth2 security
This commit is contained in:
@@ -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:
|
||||
|
||||
BIN
configserver-bootstrap/src/main/resources/keystore.jks
Normal file
BIN
configserver-bootstrap/src/main/resources/keystore.jks
Normal file
Binary file not shown.
1
oauth2-zuul/README.md
Normal file
1
oauth2-zuul/README.md
Normal file
@@ -0,0 +1 @@
|
||||
This project is a sample of a client application with OAuth2 and Zuul
|
||||
109
oauth2-zuul/pom.xml
Normal file
109
oauth2-zuul/pom.xml
Normal file
@@ -0,0 +1,109 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework.cloud.sample</groupId>
|
||||
<artifactId>spring-cloud-sample-oauth2-zuul</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-cloud-sample-oauth2-zuul</name>
|
||||
<description>Demo project for Spring Cloud</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.5.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.7</java.version>
|
||||
<spring-cloud.version>Brixton.BUILD-SNAPSHOT</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zuul</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<!--skip deploy (this is just a test module) -->
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
|
||||
</project>
|
||||
14
oauth2-zuul/src/main/java/demo/ZuulApplication.java
Normal file
14
oauth2-zuul/src/main/java/demo/ZuulApplication.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
6
oauth2-zuul/src/main/resources/application.properties
Normal file
6
oauth2-zuul/src/main/resources/application.properties
Normal file
@@ -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:
|
||||
35
oauth2-zuul/src/test/java/demo/ZuulApplicationTests.java
Normal file
35
oauth2-zuul/src/test/java/demo/ZuulApplicationTests.java
Normal file
@@ -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<String> 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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user