├── .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 futureMap = oauthConnectionService.getAsynchronousResults(userResourceUrl, Map.class, restTemplate); 50 | while (!futureMap.isDone()) { 51 | Thread.sleep(10); 52 | } 53 | 54 | return futureMap.get(); 55 | } 56 | 57 | /** 58 | * InsufficientAuthenticationException is not thrown since we have supplied 59 | * instance of{@link AccessTokenProvider} to the {@link OAuth2RestTemplate} 60 | * This uri does not require authentication on client side 61 | * 62 | * @see OauthClientConfig#userAccessTokenProvider() 63 | */ 64 | @RequestMapping(value = "/results-nonauthorized") 65 | @ResponseBody 66 | public Map nonAuthorizedResultsLoginNotRequired(HttpServletResponse response) throws Exception { 67 | return oauthConnectionService.getResults(userResourceUrl, Map.class, restTemplate); 68 | } 69 | 70 | /** 71 | * Template used has clientOnly() method returning true so user 72 | * authorization is not necessary. Uses 73 | * {@link ClientOnlyResourceOwnerPasswordResourceDetails} This uri does not 74 | * require authentication on client side 75 | */ 76 | @RequestMapping(value = "/results") 77 | @ResponseBody 78 | public Map results() throws Exception { 79 | return oauthConnectionService.getClientOnlyResults(clientResourceUrl, Map.class, clientOnlyrestTemplate); 80 | } 81 | 82 | /** 83 | * User is being redirected to login page for required authorization This 84 | * uri REQUIRES authentication on client side 85 | * 86 | * 87 | * if you want to set username and password for your template you can still do it here 88 | * but you have to obtain username and password from the user 89 | * 90 | * if (restTemplate.getResource() instanceof ResourceOwnerPasswordResourceDetails) { 91 | * ((ResourceOwnerPasswordResourceDetails) restTemplate.getResource()).setUsername(username); 92 | * ((ResourceOwnerPasswordResourceDetails) restTemplate.getResource()).setPassword(password); 93 | * 94 | * } 95 | * 96 | * in the rest template that is used in this example it is hard coded in the resource 97 | * details here @see {@link OauthClientConfig#fullAccessresourceDetails(String)} 98 | * 99 | */ 100 | @RequestMapping(value = "/authorized-results") 101 | @ResponseBody 102 | public Map authorized() throws Exception { 103 | 104 | return oauthConnectionService.getResults(userResourceUrl, Map.class, restTemplate); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/oauth/client/demo/config/AcceptAllHttpsConfig.java: -------------------------------------------------------------------------------- 1 | package oauth.client.demo.config; 2 | 3 | import java.io.IOException; 4 | import java.net.HttpURLConnection; 5 | import java.security.cert.X509Certificate; 6 | 7 | import javax.net.ssl.HostnameVerifier; 8 | import javax.net.ssl.HttpsURLConnection; 9 | import javax.net.ssl.SSLContext; 10 | import javax.net.ssl.TrustManager; 11 | import javax.net.ssl.X509TrustManager; 12 | 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.context.annotation.Bean; 15 | import org.springframework.context.annotation.Configuration; 16 | import org.springframework.context.annotation.Profile; 17 | import org.springframework.http.client.ClientHttpRequestFactory; 18 | import org.springframework.http.client.SimpleClientHttpRequestFactory; 19 | 20 | /* This is only for testing purposes. DO NOT USE FOR PRODUCTION !!! 21 | * It guarantees ssl handshake with unverified server with self created certificate 22 | * If you are trying to connect to Oauth server 23 | * that you started on your localhost operating on https 24 | * and using self-signed certificate you have to uncomment below annotations 25 | */ 26 | @Configuration 27 | @Profile("https_connection") 28 | public class AcceptAllHttpsConfig { 29 | 30 | @Bean 31 | public HostnameVerifier hostnameVerifier() { 32 | return new javax.net.ssl.HostnameVerifier() { 33 | 34 | public boolean verify(String hostname, 35 | javax.net.ssl.SSLSession sslSession) { 36 | if (hostname.equals("localhost")) { 37 | return true; 38 | } 39 | return false; 40 | } 41 | }; 42 | } 43 | 44 | @Bean 45 | public ClientHttpRequestFactory clientHttpRequestFactory( 46 | HostnameVerifier hostNameVerifier) { 47 | ClientHttpRequestFactory clientHttpRequestFactory = new ClientHttpsAcceptLoaclahostRequestFactory( 48 | hostNameVerifier); 49 | return clientHttpRequestFactory; 50 | } 51 | 52 | class ClientHttpsAcceptLoaclahostRequestFactory extends 53 | SimpleClientHttpRequestFactory { 54 | 55 | private final HostnameVerifier hostNameVerifier; 56 | 57 | @Autowired 58 | public ClientHttpsAcceptLoaclahostRequestFactory( 59 | HostnameVerifier hostNameVerifier) { 60 | this.hostNameVerifier = hostNameVerifier; 61 | } 62 | 63 | @Override 64 | protected void prepareConnection(final HttpURLConnection connection, 65 | final String httpMethod) throws IOException { 66 | if (connection instanceof HttpsURLConnection) { 67 | 68 | ((HttpsURLConnection) connection) 69 | .setHostnameVerifier(hostNameVerifier); 70 | ((HttpsURLConnection) connection) 71 | .setSSLSocketFactory(initSSLContext() 72 | .getSocketFactory()); 73 | } 74 | super.prepareConnection(connection, httpMethod); 75 | } 76 | 77 | private SSLContext initSSLContext() { 78 | try { 79 | System.setProperty("https.protocols", "TLSv1"); 80 | SSLContext sc = SSLContext.getInstance("TLSv1"); 81 | sc.init(null, getTrustManager(), null); 82 | 83 | return sc; 84 | } catch (final Exception ex) { 85 | return null; 86 | } 87 | } 88 | 89 | private TrustManager[] getTrustManager() { 90 | return new TrustManager[] { new X509TrustManager() { 91 | 92 | public java.security.cert.X509Certificate[] getAcceptedIssuers() { 93 | return new X509Certificate[0]; 94 | } 95 | 96 | public void checkClientTrusted( 97 | java.security.cert.X509Certificate[] certs, 98 | String authType) { 99 | } 100 | 101 | public void checkServerTrusted( 102 | java.security.cert.X509Certificate[] certs, 103 | String authType) { 104 | 105 | } 106 | } }; 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/oauth/client/demo/config/OauthClientConfig.java: -------------------------------------------------------------------------------- 1 | package oauth.client.demo.config; 2 | 3 | import oauth.client.demo.DemoApplicationUtils; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Qualifier; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.http.client.ClientHttpRequestFactory; 11 | import org.springframework.http.client.SimpleClientHttpRequestFactory; 12 | import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext; 13 | import org.springframework.security.oauth2.client.OAuth2RestOperations; 14 | import org.springframework.security.oauth2.client.OAuth2RestTemplate; 15 | import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; 16 | import org.springframework.security.oauth2.client.token.AccessTokenProvider; 17 | import org.springframework.security.oauth2.client.token.AccessTokenProviderChain; 18 | import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest; 19 | import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider; 20 | import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; 21 | import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider; 22 | import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails; 23 | 24 | @Configuration 25 | public class OauthClientConfig { 26 | 27 | @Autowired(required = false) 28 | ClientHttpRequestFactory clientHttpRequestFactory; 29 | 30 | /* 31 | * ClientHttpRequestFactory is autowired and checked in case somewhere in 32 | * your configuration you provided {@link ClientHttpRequestFactory} 33 | * implementation Bean where you defined specifics of your connection, if 34 | * not it is instantiated here with {@link SimpleClientHttpRequestFactory} 35 | */ 36 | private ClientHttpRequestFactory getClientHttpRequestFactory() { 37 | if (clientHttpRequestFactory == null) { 38 | clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); 39 | } 40 | return clientHttpRequestFactory; 41 | } 42 | 43 | @Bean 44 | @Qualifier("myRestTemplate") 45 | public OAuth2RestOperations restTemplate(@Value("${oauth.token}") String tokenUrl) { 46 | 47 | OAuth2RestTemplate template = new OAuth2RestTemplate(fullAccessresourceDetails(tokenUrl), new DefaultOAuth2ClientContext( 48 | new DefaultAccessTokenRequest())); 49 | return prepareTemplate(template, false); 50 | } 51 | 52 | @Bean 53 | @Qualifier("myClientOnlyRestTemplate") 54 | public OAuth2RestOperations restClientOnlyTemplate(@Value("${oauth.token}") String tokenUrl) { 55 | 56 | OAuth2RestTemplate template = new OAuth2RestTemplate(fullAccessresourceDetailsClientOnly(tokenUrl), new DefaultOAuth2ClientContext( 57 | new DefaultAccessTokenRequest())); 58 | return prepareTemplate(template, true); 59 | } 60 | 61 | public OAuth2RestTemplate prepareTemplate(OAuth2RestTemplate template, boolean isClient) { 62 | template.setRequestFactory(getClientHttpRequestFactory()); 63 | if (isClient) { 64 | template.setAccessTokenProvider(clientAccessTokenProvider()); 65 | } else { 66 | template.setAccessTokenProvider(userAccessTokenProvider()); 67 | } 68 | return template; 69 | } 70 | 71 | /** 72 | * {@link AccessTokenProviderChain} throws 73 | * InsufficientAuthenticationException in 74 | * obtainAccessToken(OAuth2ProtectedResourceDetails resource, 75 | * AccessTokenRequest request) if user is not authorized, but since we are 76 | * setting our own accessTokenProvider() on OAuth2RestTemplate this 77 | * condition is not being checked, thus exception is not being thrown and 78 | * requirement for user to be logged in is skipped 79 | */ 80 | @Bean 81 | public AccessTokenProvider userAccessTokenProvider() { 82 | ResourceOwnerPasswordAccessTokenProvider accessTokenProvider = new ResourceOwnerPasswordAccessTokenProvider(); 83 | accessTokenProvider.setRequestFactory(getClientHttpRequestFactory()); 84 | return accessTokenProvider; 85 | } 86 | 87 | @Bean 88 | public AccessTokenProvider clientAccessTokenProvider() { 89 | ClientCredentialsAccessTokenProvider accessTokenProvider = new ClientCredentialsAccessTokenProvider(); 90 | accessTokenProvider.setRequestFactory(getClientHttpRequestFactory()); 91 | return accessTokenProvider; 92 | } 93 | 94 | @Bean 95 | @Qualifier("myFullAcessDetails") 96 | public OAuth2ProtectedResourceDetails fullAccessresourceDetails(String tokenUrl) { 97 | ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails(); 98 | resource.setAccessTokenUri(tokenUrl); 99 | resource.setClientId("user_member"); 100 | resource.setGrantType("password"); 101 | resource.setScope(DemoApplicationUtils.getScopesList("read", "write")); 102 | resource.setUsername("roy"); 103 | resource.setPassword("spring"); 104 | return resource; 105 | } 106 | 107 | @Bean 108 | @Qualifier("myClientOnlyFullAcessDetails") 109 | public OAuth2ProtectedResourceDetails fullAccessresourceDetailsClientOnly(String tokenUrl) { 110 | ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails(); 111 | resource.setAccessTokenUri(tokenUrl); 112 | resource.setClientId("clientapp"); 113 | resource.setClientSecret("123456"); 114 | resource.setGrantType("client_credentials"); 115 | resource.setScope(DemoApplicationUtils.getScopesList("read", "write")); 116 | return resource; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/main/java/oauth/client/demo/config/WebSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package oauth.client.demo.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8 | import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; 9 | 10 | @Configuration 11 | @EnableWebMvcSecurity 12 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 13 | @Override 14 | protected void configure(HttpSecurity http) throws Exception { 15 | http.authorizeRequests().antMatchers("/", "/results*").permitAll() 16 | .anyRequest().authenticated().and().formLogin() 17 | .permitAll().and().logout().permitAll(); 18 | } 19 | 20 | @Autowired 21 | public void configureGlobal(AuthenticationManagerBuilder auth) 22 | throws Exception { 23 | auth.inMemoryAuthentication().withUser("roy").password("spring") 24 | .roles("USER"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/oauth/client/demo/service/OauthConnectionService.java: -------------------------------------------------------------------------------- 1 | package oauth.client.demo.service; 2 | 3 | import java.util.concurrent.Future; 4 | 5 | import org.springframework.scheduling.annotation.Async; 6 | import org.springframework.web.client.RestOperations; 7 | 8 | public interface OauthConnectionService { 9 | 10 | // implementation of this method should be annotated with @Async 11 | @Async 12 | public Future getAsynchronousResults(String resourceUrl, 13 | Class resultType, RestOperations restTemplate); 14 | 15 | public T getResults(String resourceUrl, Class resultType, 16 | RestOperations restTemplate); 17 | 18 | public T getClientOnlyResults(String resourceUrl, Class resultType, 19 | RestOperations restTemplate); 20 | 21 | default T getForObject(String resourceUrl, Class responseType, 22 | RestOperations restTemplate) { 23 | return restTemplate.getForObject(resourceUrl, responseType); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/oauth/client/demo/service/OauthConnectionServiceImpl.java: -------------------------------------------------------------------------------- 1 | package oauth.client.demo.service; 2 | 3 | import java.util.concurrent.Future; 4 | 5 | import org.springframework.scheduling.annotation.Async; 6 | import org.springframework.scheduling.annotation.AsyncResult; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.web.client.RestOperations; 9 | 10 | @Service 11 | public class OauthConnectionServiceImpl implements OauthConnectionService { 12 | 13 | @Async 14 | public Future getAsynchronousResults(String resourceUrl, 15 | Class resultType, RestOperations restTemplate) { 16 | return new AsyncResult(getForObject(resourceUrl, resultType, 17 | restTemplate)); 18 | } 19 | 20 | @Override 21 | public T getClientOnlyResults(String resourceUrl, Class resultType, 22 | RestOperations clientOnlyrestTemplate) { 23 | return getForObject(resourceUrl, resultType, clientOnlyrestTemplate); 24 | } 25 | 26 | @Override 27 | public T getResults(String resourceUrl, Class resultType, 28 | RestOperations restTemplate) { 29 | return getForObject(resourceUrl, resultType, restTemplate); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/main/resources/application-http_connection.properties: -------------------------------------------------------------------------------- 1 | auth.resource:http://localhost:8080 2 | oauth.authorize:http://localhost:8080/oauth/authorize 3 | oauth.token:http://localhost:8080/oauth/token 4 | oauth.resource.greeting:http://localhost:8080/greeting 5 | oauth.resource.client_greeting:http://localhost:8080/client_greeting 6 | -------------------------------------------------------------------------------- /src/main/resources/application-https_connection.properties: -------------------------------------------------------------------------------- 1 | oauth.resource:https://localhost:8443 2 | oauth.authorize:https://localhost:8443/oauth/authorize 3 | oauth.token:https://localhost:8443/oauth/token 4 | oauth.resource.greeting:https://localhost:8443/greeting 5 | oauth.resource.client_greeting:https://localhost:8080/client_greeting -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 8005 2 | spring.profiles.active=http_connection 3 | #comment above profile and uncoment line below to operate using https 4 | #spring.profiles.active=https_connection 5 | -------------------------------------------------------------------------------- /src/test/java/oauth/client/demo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package oauth.client.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.context.web.WebAppConfiguration; 6 | import org.springframework.boot.test.SpringApplicationConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | 9 | @RunWith(SpringJUnit4ClassRunner.class) 10 | @SpringApplicationConfiguration(classes = DemoApplication.class) 11 | @WebAppConfiguration 12 | public class DemoApplicationTests { 13 | 14 | @Test 15 | public void contextLoads() { 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/oauth/client/demo/MyRestControllerTest.java: -------------------------------------------------------------------------------- 1 | package oauth.client.demo; 2 | 3 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 4 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 5 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 6 | import oauth.client.demo.config.OauthClientConfig; 7 | import oauth.client.demo.config.WebSecurityConfig; 8 | 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.mockito.InjectMocks; 13 | import org.mockito.MockitoAnnotations; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.boot.test.SpringApplicationConfiguration; 16 | import org.springframework.http.HttpStatus; 17 | import org.springframework.http.MediaType; 18 | import org.springframework.security.web.FilterChainProxy; 19 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 20 | import org.springframework.test.context.web.WebAppConfiguration; 21 | import org.springframework.test.web.servlet.MockMvc; 22 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 23 | import org.springframework.web.context.WebApplicationContext; 24 | 25 | @RunWith(SpringJUnit4ClassRunner.class) 26 | @WebAppConfiguration 27 | @SpringApplicationConfiguration(classes = { DemoApplication.class, 28 | WebSecurityConfig.class, OauthClientConfig.class }) 29 | public class MyRestControllerTest { 30 | @Autowired 31 | WebApplicationContext context; 32 | @Autowired 33 | private FilterChainProxy springSecurityFilterChain; 34 | @InjectMocks 35 | MyRestController controller; 36 | 37 | private MockMvc mvc; 38 | 39 | @Before 40 | public void setUp() { 41 | MockitoAnnotations.initMocks(this); 42 | mvc = MockMvcBuilders.webAppContextSetup(context) 43 | .addFilter(springSecurityFilterChain).build(); 44 | } 45 | 46 | @Test 47 | public void resultsAsynch() throws Exception { 48 | 49 | mvc.perform(get("/results-asynch").accept(MediaType.APPLICATION_JSON)) 50 | .andExpect(status().isOk()) 51 | .andExpect( 52 | content() 53 | .contentTypeCompatibleWith( 54 | MediaType 55 | .parseMediaType(MediaType.APPLICATION_JSON_VALUE))); 56 | } 57 | 58 | @Test 59 | public void resultsClientOnly() throws Exception { 60 | 61 | mvc.perform(get("/results").accept(MediaType.APPLICATION_JSON)) 62 | .andExpect(status().isOk()) 63 | .andExpect( 64 | content() 65 | .contentTypeCompatibleWith( 66 | MediaType 67 | .parseMediaType(MediaType.APPLICATION_JSON_VALUE))); 68 | } 69 | 70 | @Test 71 | public void resultsRedirectToLogin() throws Exception { 72 | mvc.perform(get("/authorized-results")).andExpect( 73 | status().is(HttpStatus.FOUND.value())); 74 | 75 | } 76 | 77 | @Test 78 | public void resultsNonauthorizedExceptionForNoLoginRequiredLocaly() throws Exception { 79 | mvc.perform(get("/results-nonauthorized").accept(MediaType.APPLICATION_JSON)) 80 | .andExpect(status().isOk()) 81 | .andExpect( 82 | content() 83 | .contentTypeCompatibleWith( 84 | MediaType 85 | .parseMediaType(MediaType.APPLICATION_JSON_VALUE))); 86 | 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /target/classes/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Implementation-Title: demo 3 | Implementation-Version: 0.0.1-SNAPSHOT 4 | Built-By: marbro 5 | Implementation-Vendor-Id: org.test 6 | Build-Jdk: 1.8.0_25 7 | Created-By: Maven Integration for Eclipse 8 | Implementation-Vendor: Pivotal Software, Inc. 9 | Main-Class: oauth.client.demo.DemoApplication 10 | 11 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/org.test/oath2-client-demo/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Mon Aug 24 15:19:25 EDT 2015 3 | version=0.0.1-SNAPSHOT 4 | groupId=org.test 5 | m2e.projectName=oath2-client-demo 6 | m2e.projectLocation=/home/marbro/new_git/oauth-client-sample 7 | artifactId=oath2-client-demo 8 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/org.test/oath2-client-demo/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.4.RELEASE 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /target/classes/application-http_connection.properties: -------------------------------------------------------------------------------- 1 | auth.resource:http://localhost:8080 2 | oauth.authorize:http://localhost:8080/oauth/authorize 3 | oauth.token:http://localhost:8080/oauth/token 4 | oauth.resource.greeting:http://localhost:8080/greeting 5 | oauth.resource.client_greeting:http://localhost:8080/client_greeting 6 | -------------------------------------------------------------------------------- /target/classes/application-https_connection.properties: -------------------------------------------------------------------------------- 1 | oauth.resource:https://localhost:8443 2 | oauth.authorize:https://localhost:8443/oauth/authorize 3 | oauth.token:https://localhost:8443/oauth/token 4 | oauth.resource.greeting:https://localhost:8443/greeting 5 | oauth.resource.client_greeting:https://localhost:8080/client_greeting -------------------------------------------------------------------------------- /target/classes/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 8005 2 | spring.profiles.active=http_connection 3 | #comment above profile and uncoment line below to operate using https 4 | #spring.profiles.active=https_connection 5 | -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/DemoApplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/DemoApplication.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/DemoApplicationUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/DemoApplicationUtils.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/MyRestController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/MyRestController.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/config/AcceptAllHttpsConfig$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/config/AcceptAllHttpsConfig$1.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/config/AcceptAllHttpsConfig$ClientHttpsAcceptLoaclahostRequestFactory$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/config/AcceptAllHttpsConfig$ClientHttpsAcceptLoaclahostRequestFactory$1.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/config/AcceptAllHttpsConfig$ClientHttpsAcceptLoaclahostRequestFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/config/AcceptAllHttpsConfig$ClientHttpsAcceptLoaclahostRequestFactory.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/config/AcceptAllHttpsConfig.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/config/AcceptAllHttpsConfig.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/config/OauthClientConfig.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/config/OauthClientConfig.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/config/WebSecurityConfig.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/config/WebSecurityConfig.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/service/OauthConnectionService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/service/OauthConnectionService.class -------------------------------------------------------------------------------- /target/classes/oauth/client/demo/service/OauthConnectionServiceImpl.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/classes/oauth/client/demo/service/OauthConnectionServiceImpl.class -------------------------------------------------------------------------------- /target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Wed Jan 14 21:49:03 EST 2015 3 | version=0.0.1-SNAPSHOT 4 | groupId=org.test 5 | artifactId=oath2-client-demo 6 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | oauth/client/demo/MyRestController.class 2 | oauth/client/demo/config/WebSecurityConfig.class 3 | oauth/client/demo/DemoApplication.class 4 | oauth/client/demo/service/OauthConnectionServiceImpl.class 5 | oauth/client/demo/service/OauthConnectionService.class 6 | oauth/client/demo/DemoApplicationUtils.class 7 | oauth/client/demo/config/ClientOnlyResourceOwnerPasswordResourceDetails.class 8 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/config/ClientOnlyResourceOwnerPasswordResourceDetails.java 2 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/MyRestController.java 3 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/service/OauthConnectionServiceImpl.java 4 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/config/WebSecurityConfig.java 5 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/config/OauthClientConfig.java 6 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/config/AcceptAllHttpsConfig.java 7 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/DemoApplication.java 8 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/DemoApplicationUtils.java 9 | /home/marbro/git_rep/oauth-client-sample-master/src/main/java/oauth/client/demo/service/OauthConnectionService.java 10 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | oauth/client/demo/DemoApplicationTests.class 2 | oauth/client/demo/MyRestControllerTest.class 3 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /home/marbro/git_rep/oauth-client-sample-master/src/test/java/oauth/client/demo/DemoApplicationTests.java 2 | /home/marbro/git_rep/oauth-client-sample-master/src/test/java/oauth/client/demo/MyRestControllerTest.java 3 | -------------------------------------------------------------------------------- /target/oath2-client-demo-0.0.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/oath2-client-demo-0.0.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /target/oath2-client-demo-0.0.1-SNAPSHOT.jar.original: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/oath2-client-demo-0.0.1-SNAPSHOT.jar.original -------------------------------------------------------------------------------- /target/surefire-reports/TEST-oauth.client.demo.DemoApplicationTests.xml: -------------------------------------------------------------------------------- 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 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /target/surefire-reports/TEST-oauth.client.demo.MyRestControllerTest.xml: -------------------------------------------------------------------------------- 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 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /target/surefire-reports/oauth.client.demo.DemoApplicationTests.txt: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | Test set: oauth.client.demo.DemoApplicationTests 3 | ------------------------------------------------------------------------------- 4 | Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.616 sec - in oauth.client.demo.DemoApplicationTests 5 | -------------------------------------------------------------------------------- /target/surefire-reports/oauth.client.demo.MyRestControllerTest.txt: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | Test set: oauth.client.demo.MyRestControllerTest 3 | ------------------------------------------------------------------------------- 4 | Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.178 sec - in oauth.client.demo.MyRestControllerTest 5 | -------------------------------------------------------------------------------- /target/test-classes/oauth/client/demo/DemoApplicationTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/test-classes/oauth/client/demo/DemoApplicationTests.class -------------------------------------------------------------------------------- /target/test-classes/oauth/client/demo/MyRestControllerTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mariubog/oauth-client-sample/679ba8232d3dfffe8e849c732250a5ee8c68ae53/target/test-classes/oauth/client/demo/MyRestControllerTest.class --------------------------------------------------------------------------------