├── .classpath
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
├── org.eclipse.jdt.core.prefs
├── org.eclipse.m2e.core.prefs
└── org.hibernate.eclipse.console.prefs
├── README.md
├── pom.xml
├── src
├── main
│ ├── java
│ │ └── oauth
│ │ │ └── client
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ ├── DemoApplicationUtils.java
│ │ │ ├── MyRestController.java
│ │ │ ├── config
│ │ │ ├── AcceptAllHttpsConfig.java
│ │ │ ├── OauthClientConfig.java
│ │ │ └── WebSecurityConfig.java
│ │ │ └── service
│ │ │ ├── OauthConnectionService.java
│ │ │ └── OauthConnectionServiceImpl.java
│ └── resources
│ │ ├── application-http_connection.properties
│ │ ├── application-https_connection.properties
│ │ └── application.properties
└── test
│ └── java
│ └── oauth
│ └── client
│ └── demo
│ ├── DemoApplicationTests.java
│ └── MyRestControllerTest.java
└── target
├── classes
├── META-INF
│ ├── MANIFEST.MF
│ └── maven
│ │ └── org.test
│ │ └── oath2-client-demo
│ │ ├── pom.properties
│ │ └── pom.xml
├── application-http_connection.properties
├── application-https_connection.properties
├── application.properties
└── oauth
│ └── client
│ └── demo
│ ├── DemoApplication.class
│ ├── DemoApplicationUtils.class
│ ├── MyRestController.class
│ ├── config
│ ├── AcceptAllHttpsConfig$1.class
│ ├── AcceptAllHttpsConfig$ClientHttpsAcceptLoaclahostRequestFactory$1.class
│ ├── AcceptAllHttpsConfig$ClientHttpsAcceptLoaclahostRequestFactory.class
│ ├── AcceptAllHttpsConfig.class
│ ├── OauthClientConfig.class
│ └── WebSecurityConfig.class
│ └── service
│ ├── OauthConnectionService.class
│ └── OauthConnectionServiceImpl.class
├── maven-archiver
└── pom.properties
├── maven-status
└── maven-compiler-plugin
│ ├── compile
│ └── default-compile
│ │ ├── createdFiles.lst
│ │ └── inputFiles.lst
│ └── testCompile
│ └── default-testCompile
│ ├── createdFiles.lst
│ └── inputFiles.lst
├── oath2-client-demo-0.0.1-SNAPSHOT.jar
├── oath2-client-demo-0.0.1-SNAPSHOT.jar.original
├── surefire-reports
├── TEST-oauth.client.demo.DemoApplicationTests.xml
├── TEST-oauth.client.demo.MyRestControllerTest.xml
├── oauth.client.demo.DemoApplicationTests.txt
└── oauth.client.demo.MyRestControllerTest.txt
└── test-classes
└── oauth
└── client
└── demo
├── DemoApplicationTests.class
└── MyRestControllerTest.class
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | oath2-client-demo
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.hibernate.eclipse.console.hibernateBuilder
15 |
16 |
17 |
18 |
19 | org.springframework.ide.eclipse.core.springbuilder
20 |
21 |
22 |
23 |
24 | org.eclipse.m2e.core.maven2Builder
25 |
26 |
27 |
28 |
29 |
30 | org.springframework.ide.eclipse.core.springnature
31 | org.eclipse.jdt.core.javanature
32 | org.eclipse.m2e.core.maven2Nature
33 | org.hibernate.eclipse.console.hibernateNature
34 |
35 |
36 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/main/java=UTF-8
3 | encoding//src/main/resources=UTF-8
4 | encoding//src/test/java=UTF-8
5 | encoding/=UTF-8
6 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3 | org.eclipse.jdt.core.compiler.compliance=1.8
4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5 | org.eclipse.jdt.core.compiler.source=1.8
6 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/.settings/org.hibernate.eclipse.console.prefs:
--------------------------------------------------------------------------------
1 | default.configuration=oath2-client-demo
2 | eclipse.preferences.version=1
3 | hibernate3.enabled=true
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # oauth-client-sample
2 | #####Sample Ouath2 client app connecting to REST service secured with Oauth2.
3 |
4 |
5 | It connects to the sample service created by royclarkson. You can find it here https://github.com/royclarkson/spring-rest-service-oauth.
6 | Royclarkson service is runing on port 8080 and this app is runing on port 8005 which you can change according to your needs so it does not colide with other services.
7 |
8 | !!! IMPORTANT !!!
9 |
10 | After I made changes in order to add clientOnly template authentication it does not work properly with above Roys' example, I made proper changes in my fork for his project and this example suppoesed to be used in order to get desirable results
11 | https://github.com/mariubog/spring-rest-service-oauth
12 | If I have a chance and Roy agrees I ll try to pull request for these changes in his example, but for now please just use my fork. Otherwise it will not work unless you make changes to comply with his authentication settings.
13 |
14 | It is basic spring boot application that can be started with
15 | `mvn clean package spring-boot:run`
16 |
17 | Instructions how to run RESTful service protected by OAuth 2 required for this client to interact with are posted here.
18 |
19 | https://github.com/royclarkson/spring-rest-service-oauth.
20 |
21 |
22 |
23 | Both applications obviously have to be run simultanously.
24 |
25 | After starting application you can use following links either with curl or simple web browser.
26 |
27 | Link to access resource protected by Oauth with not synchronazed method, uri does NOT require login on client side.
28 | Resource is acquired.
29 |
30 | `http://localhost:8005/results-asynch`
31 |
32 |
33 |
34 |
35 |
36 | Link to access resource protected by Oauth , uri does NOT require login on client side.
37 | Resource is not acquired.Authorization is required.(If acces token had been acquired earlier this method completes and returns desired results)
38 |
39 | `http://localhost:8005/results-nonauthorized`
40 |
41 |
42 |
43 |
44 |
45 |
46 | Link to access resource protected by Oauth using ResourceDetails clientOnly() == true, uri does NOT require login on client side.
47 | Resource is acquired.
48 |
49 | `http://localhost:8005/results`
50 |
51 |
52 |
53 |
54 |
55 |
56 | Link to access resource protected by Oauth with synchronazed method, uri REQUIRES login on client side.
57 | Resource is acquired after authentication.
58 |
59 | `http://localhost:8005/authorized-results`
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.test
7 | oath2-client-demo
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | demo
12 | Demo project for Oauth2 client and Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 1.2.1.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | oauth.client.demo.DemoApplication
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-web
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-test
35 | test
36 |
37 |
38 | org.springframework.security.oauth
39 | spring-security-oauth2
40 | 2.0.17.RELEASE
41 |
42 |
43 |
44 |
45 |
46 |
47 | org.springframework.boot
48 | spring-boot-maven-plugin
49 |
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/src/main/java/oauth/client/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package oauth.client.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5 | import org.springframework.context.annotation.ComponentScan;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.scheduling.annotation.EnableAsync;
8 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
9 |
10 | @Configuration
11 | @ComponentScan
12 | @EnableAutoConfiguration
13 | @EnableOAuth2Client
14 | @EnableAsync
15 | public class DemoApplication {
16 | public static void main(String[] args) {
17 | SpringApplication.run(DemoApplication.class, args);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/oauth/client/demo/DemoApplicationUtils.java:
--------------------------------------------------------------------------------
1 | package oauth.client.demo;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.List;
6 |
7 | public class DemoApplicationUtils {
8 | public static List getScopesList(String... scopes) {
9 | List scopesList = new ArrayList(Arrays.asList(scopes));
10 | return scopesList;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/oauth/client/demo/MyRestController.java:
--------------------------------------------------------------------------------
1 | package oauth.client.demo;
2 |
3 | import java.util.Map;
4 | import java.util.concurrent.Future;
5 |
6 | import javax.servlet.http.HttpServletResponse;
7 |
8 | import oauth.client.demo.config.OauthClientConfig;
9 | import oauth.client.demo.service.OauthConnectionService;
10 |
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.beans.factory.annotation.Qualifier;
13 | import org.springframework.beans.factory.annotation.Value;
14 | import org.springframework.security.oauth2.client.OAuth2RestOperations;
15 | import org.springframework.security.oauth2.client.OAuth2RestTemplate;
16 | import org.springframework.security.oauth2.client.token.AccessTokenProvider;
17 | import org.springframework.web.bind.annotation.RequestMapping;
18 | import org.springframework.web.bind.annotation.ResponseBody;
19 | import org.springframework.web.bind.annotation.RestController;
20 |
21 | @SuppressWarnings("rawtypes")
22 | @RestController
23 | public class MyRestController {
24 | @Autowired
25 | @Qualifier("myRestTemplate")
26 | private OAuth2RestOperations restTemplate;
27 | @Autowired
28 | @Qualifier("myClientOnlyRestTemplate")
29 | private OAuth2RestOperations clientOnlyrestTemplate;
30 | @Autowired
31 | OauthConnectionService oauthConnectionService;
32 |
33 | @Value("${oauth.resource.greeting}")
34 | private String userResourceUrl;
35 |
36 | @Value("${oauth.resource.client_greeting}")
37 | private String clientResourceUrl;
38 |
39 | /**
40 | * In this method if token is not obtained exception is ***NOT*** thrown and
41 | * access token is obtained by template. It bypasses requirement for
42 | * associating request with authenticated user ? This uri does not require
43 | * authentication on client side
44 | */
45 | @RequestMapping(value = "/results-asynch")
46 | @ResponseBody
47 | public Map resultsAsynch(HttpServletResponse response) throws Exception {
48 |
49 | Future