Add auto-config for spring-security-oauth2-client

Closes gh-10497
This commit is contained in:
Madhura Bhave
2017-09-21 15:47:53 -07:00
parent 494b79c439
commit dbe1d9608d
24 changed files with 1706 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
package sample.oauth2.client;
import java.security.Principal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Madhura Bhave
*/
@RestController
public class ExampleController {
@RequestMapping("/")
public String email(Principal principal) {
return "Hello " + principal.getName();
}
}

View File

@@ -0,0 +1,16 @@
package sample.oauth2.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Madhura Bhave
*/
@SpringBootApplication
public class SampleOAuth2ClientApplication {
public static void main(String[] args) {
SpringApplication.run(SampleOAuth2ClientApplication.class);
}
}

View File

@@ -0,0 +1,19 @@
spring:
security:
oauth2:
client:
registration:
github-client-1:
client-id: 1b4c6c2d23a2dbec8959
client-secret: 8bdb4047d98f37e1c6aeba9ffd2017bc31682e1b
client-name: Github user
provider: github
scope: user
redirect_uri: http://localhost:8080/oauth2/authorize/code/github
github-client-2:
client-id: 1b4c6c2d23a2dbec8959
client-secret: 8bdb4047d98f37e1c6aeba9ffd2017bc31682e1b
client-name: Github email
provider: github
scope: user:email
redirect_uri: http://localhost:8080/oauth2/authorize/code/github

View File

@@ -0,0 +1,51 @@
package sample.oauth2.client;
import java.net.URI;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for an OAuth2 client application.
*
* @author Madhura Bhave
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class SampleOAuth2ClientApplicationTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void everythingShouldRedirectToLogin() throws Exception {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
assertThat(entity.getHeaders().getLocation())
.isEqualTo(URI.create("http://localhost:" + this.port + "/login"));
}
@Test
public void loginShouldHaveBothOAuthClientsToChooseFrom() throws Exception {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/login", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("/oauth2/authorization/code/github-client-1");
assertThat(entity.getBody()).contains("/oauth2/authorization/code/github-client-2");
}
}