├── OAuthFlows.graffle
├── settings.gradle
├── applications
├── client
│ ├── .DS_Store
│ ├── src
│ │ └── main
│ │ │ ├── resources
│ │ │ ├── static
│ │ │ │ └── css
│ │ │ │ │ └── application.css
│ │ │ ├── application.yml
│ │ │ └── templates
│ │ │ │ ├── showvalue.html
│ │ │ │ ├── exception_page.html
│ │ │ │ ├── index.html
│ │ │ │ ├── layout
│ │ │ │ └── sitelayout.html
│ │ │ │ └── show_token.html
│ │ │ └── java
│ │ │ └── samples
│ │ │ └── authcode
│ │ │ ├── web
│ │ │ ├── ExceptionHandlingAdvice.java
│ │ │ └── SampleController.java
│ │ │ ├── service
│ │ │ ├── TokenBeautifier.java
│ │ │ └── DownstreamServiceHandler.java
│ │ │ ├── config
│ │ │ └── OAuth2SecurityConfig.java
│ │ │ ├── Application.java
│ │ │ └── SSLValidationDisabler.java
│ ├── manifest.yml
│ ├── run_with_vcap.sh
│ └── build.gradle
└── resourceserver
│ ├── src
│ ├── main
│ │ ├── java
│ │ │ └── org
│ │ │ │ └── bk
│ │ │ │ ├── ResourceServerApplication.java
│ │ │ │ ├── web
│ │ │ │ └── GreetingsController.java
│ │ │ │ └── security
│ │ │ │ └── ResourceServerConfiguration.java
│ │ └── resources
│ │ │ └── application.yml
│ └── test
│ │ └── java
│ │ └── org
│ │ └── bk
│ │ └── AuthserverApplicationTests.java
│ └── build.gradle
├── flows
└── Authorization_Code_Flow.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── README.adoc
├── gradlew.bat
├── gradlew
└── LICENSE.md
/OAuthFlows.graffle:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bijukunjummen/oauth-uaa-sample/HEAD/OAuthFlows.graffle
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name="oauth-uaa-sample"
2 | include('applications:client', 'applications:resourceserver')
3 |
--------------------------------------------------------------------------------
/applications/client/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bijukunjummen/oauth-uaa-sample/HEAD/applications/client/.DS_Store
--------------------------------------------------------------------------------
/flows/Authorization_Code_Flow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bijukunjummen/oauth-uaa-sample/HEAD/flows/Authorization_Code_Flow.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bijukunjummen/oauth-uaa-sample/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/applications/client/src/main/resources/static/css/application.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: tahoma, verdana, arial, sans-serif;
3 | padding-top: 60px;
4 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .settings/
3 | .springBeans
4 | .classpath
5 | .project
6 | bin/
7 | .cache
8 | .gradle
9 | .idea
10 | build/
11 | out/
12 | *.ipr
13 | *.iws
14 | *.iml
15 | /classes/
16 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Feb 10 15:03:08 PST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-all.zip
7 |
--------------------------------------------------------------------------------
/applications/client/manifest.yml:
--------------------------------------------------------------------------------
1 | ---
2 | applications:
3 | - name: authcode-sample
4 | memory: 512M
5 | instances: 1
6 | path: build/libs/authcode.jar
7 | env:
8 | SKIP_SSL_VALIDATION: "true"
9 | GRANT_TYPE: authorization_code
10 | SSO_IDENTITY_PROVIDERS: uaa
11 |
--------------------------------------------------------------------------------
/applications/resourceserver/src/main/java/org/bk/ResourceServerApplication.java:
--------------------------------------------------------------------------------
1 | package org.bk;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ResourceServerApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(ResourceServerApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/applications/resourceserver/src/test/java/org/bk/AuthserverApplicationTests.java:
--------------------------------------------------------------------------------
1 | package org.bk;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class AuthserverApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/applications/resourceserver/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | ssoServiceUrl: http://localhost:8080/uaa
2 | server:
3 | port: 9999
4 | spring:
5 | thymeleaf:
6 | cache: false
7 | security:
8 | ignored: /favicon.ico, /
9 | basic:
10 | enabled: false
11 | oauth2:
12 | client:
13 | client-id: resource1
14 | client-secret: resource1
15 | resource:
16 | token-info-uri: ${ssoServiceUrl}/oauth/check_token
17 | jwt:
18 | key-uri: ${ssoServiceUrl}/token_key
19 | user-info-uri: ${ssoServiceUrl}/userinfo
20 | id: resource
21 |
22 | logging.level:
23 | org.springframework.security: DEBUG
--------------------------------------------------------------------------------
/applications/client/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | ssoServiceUrl: http://localhost:8080/uaa
2 | server:
3 | port: 8888
4 | spring:
5 | thymeleaf:
6 | cache: false
7 | security:
8 | ignored: /favicon.ico, /webjars/**, /css/**
9 | basic:
10 | enabled: false
11 | oauth2:
12 | client:
13 | client-id: client1
14 | client-secret: client1
15 | access-token-uri: ${ssoServiceUrl}/oauth/token
16 | user-authorization-uri: ${ssoServiceUrl}/oauth/authorize
17 | resource:
18 | token-info-uri: http://localhost:8080/check_token
19 | jwt:
20 | key-uri: ${ssoServiceUrl}/token_key
21 | user-info-uri: ${ssoServiceUrl}/userinfo
22 |
23 | logging.level:
24 | org.springframework.security: INFO
25 | resourceServerUrl: ${RESOURCE_URL:http://localhost:9999}
--------------------------------------------------------------------------------
/applications/client/run_with_vcap.sh:
--------------------------------------------------------------------------------
1 | read -r -d '' VCAP_APPLICATION <<'ENDOFVAR'
2 | {"application_version":"1","application_name":"sample-client","application_uris":[""],"version":"1.0","name":"sample-client","instance_id":"abcd","instance_index":0}
3 | ENDOFVAR
4 |
5 | export VCAP_APPLICATION=$VCAP_APPLICATION
6 |
7 | read -r -d '' VCAP_SERVICES <<'ENDOFVAR'
8 | {
9 | "p-identity": [
10 | {
11 | "credentials": {
12 | "client_id": "live-test-auth",
13 | "client_secret": "live-test-auth",
14 | "auth_domain": "http://localhost:8080"
15 | },
16 | "syslog_drain_url": null,
17 | "volume_mounts": [],
18 | "label": "p-identity",
19 | "provider": null,
20 | "plan": "uaa-only",
21 | "name": "bk-test-sso",
22 | "tags": []
23 | }
24 | ]
25 | }
26 | ENDOFVAR
27 |
28 | export VCAP_SERVICES=$VCAP_SERVICES
29 |
30 | ./gradlew clean bootRun
--------------------------------------------------------------------------------
/applications/client/src/main/java/samples/authcode/web/ExceptionHandlingAdvice.java:
--------------------------------------------------------------------------------
1 | package samples.authcode.web;
2 |
3 | import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException;
4 | import org.springframework.ui.Model;
5 | import org.springframework.web.bind.annotation.ControllerAdvice;
6 | import org.springframework.web.bind.annotation.ExceptionHandler;
7 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
8 |
9 | @ControllerAdvice
10 | public class ExceptionHandlingAdvice extends ResponseEntityExceptionHandler {
11 |
12 | @ExceptionHandler(OAuth2AccessDeniedException.class)
13 | public String handleCustomBindException(OAuth2AccessDeniedException ex, Model model) {
14 | model.addAttribute("exception_message",
15 | String.format("Http Error Code: %s, OAuth2Error Code: %s, Exception Message: %s",
16 | ex.getHttpErrorCode(), ex.getOAuth2ErrorCode(), ex.getMessage()));
17 | return "exception_page";
18 | }
19 |
20 | }
--------------------------------------------------------------------------------
/applications/client/src/main/resources/templates/showvalue.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | OAuth2 Sample
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
Received from Downstream:
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/applications/client/src/main/resources/templates/exception_page.html:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | Exception Details
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
Exception Message:
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/applications/resourceserver/src/main/java/org/bk/web/GreetingsController.java:
--------------------------------------------------------------------------------
1 | package org.bk.web;
2 |
3 | import org.springframework.security.access.prepost.PreAuthorize;
4 | import org.springframework.security.core.Authentication;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.RequestMethod;
7 | import org.springframework.web.bind.annotation.ResponseBody;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | @RestController
11 | public class GreetingsController {
12 | @PreAuthorize("#oauth2.hasScope('resource.read')")
13 | @RequestMapping(method = RequestMethod.GET, value = "/secured/read")
14 | @ResponseBody
15 | public String read(Authentication authentication) {
16 | return String.format("Read Called: Hello %s", authentication.getCredentials());
17 | }
18 |
19 | @PreAuthorize("#oauth2.hasScope('resource.write')")
20 | @RequestMapping(method = RequestMethod.GET, value = "/secured/write")
21 | @ResponseBody
22 | public String write(Authentication authentication) {
23 | return String.format("Write Called: Hello %s", authentication.getCredentials());
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/applications/client/src/main/java/samples/authcode/service/TokenBeautifier.java:
--------------------------------------------------------------------------------
1 | package samples.authcode.service;
2 |
3 | import com.fasterxml.jackson.core.type.TypeReference;
4 | import com.fasterxml.jackson.databind.ObjectMapper;
5 | import org.apache.commons.codec.binary.Base64;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | import java.io.IOException;
10 | import java.util.Map;
11 |
12 | @Service
13 | public class TokenBeautifier {
14 |
15 | @Autowired
16 | private ObjectMapper objectMapper;
17 |
18 | public String formatJwtToken(String token) {
19 | try {
20 | return toPrettyJsonString(parseToken(token));
21 | } catch (Exception e) {
22 | return "";
23 | }
24 | }
25 |
26 | private Map parseToken(String base64Token) throws IOException {
27 | String token = base64Token.split("\\.")[1];
28 | return this.objectMapper.readValue(Base64.decodeBase64(token),
29 | new TypeReference