Java Code Examples for org.springframework.security.oauth2.client.OAuth2RestTemplate#getForObject()

The following examples show how to use org.springframework.security.oauth2.client.OAuth2RestTemplate#getForObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApplicationTests.java    From Learning-Path-Spring-5-End-to-End-Programming with MIT License 6 votes vote down vote up
@Test
public void testOAuthService() {	
       ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
       resource.setUsername("guest");
       resource.setPassword("guest123");
       resource.setAccessTokenUri("http://localhost:8080/oauth/token");
       resource.setClientId("trustedclient");
       resource.setClientSecret("trustedclient123");
       resource.setGrantType("password");
       resource.setScope(Arrays.asList(new String[]{"read","write","trust"}));
 
       DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
       OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource, clientContext);

       Greet greet = restTemplate.getForObject("http://localhost:8080", Greet.class);

       Assert.assertEquals("Hello World!", greet.getMessage());
}
 
Example 2
Source File: ApplicationTests.java    From Spring-Microservices with MIT License 6 votes vote down vote up
@Test
public void testOAuthService() {	
       ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
       resource.setUsername("guest");
       resource.setPassword("guest123");
       resource.setAccessTokenUri("http://localhost:8080/oauth/token");
       resource.setClientId("trustedclient");
       resource.setClientSecret("trustedclient123");
       resource.setGrantType("password");
 
       DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
       OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource, clientContext);

       Greet greet = restTemplate.getForObject("http://localhost:8080", Greet.class);

       Assert.assertEquals("Hello World!", greet.getMessage());
}
 
Example 3
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
public void testConnectDirectlyToResourceServer() throws Exception {
        OAuth2RestTemplate template = template();

        logger.info(" CALLING: " + baseUrl+"/api");
        String result = template.getForObject(baseUrl+"/api", String.class);
        logger.info(" RESULT: " + result);

        assertEquals("{Hello API}", result);

        logger.info(" CALLING: " + baseUrl+"/events/101/");
        result = template.getForObject(baseUrl+"/events/101/", String.class);
        logger.info(" RESULT: " + result);
//        assertEquals("{[\"id\":101,\"summary\":\"Conference Call\",\"description\":\"Call with the client\",\"when\":1514059200000]}", result);
//
        logger.info(" CALLING: " + baseUrl+"/events/my/");
        result = template.getForObject(baseUrl+"/events/my/", String.class);
        logger.info(" RESULT: " + result);
        assertEquals("{Hello API}", result);
    }
 
Example 4
Source File: ApplicationTests.java    From Microservices-Building-Scalable-Software with MIT License 6 votes vote down vote up
@Test
public void testOAuthService() {	
       ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
       resource.setUsername("guest");
       resource.setPassword("guest123");
       resource.setAccessTokenUri("http://localhost:8080/oauth/token");
       resource.setClientId("trustedclient");
       resource.setClientSecret("trustedclient123");
       resource.setGrantType("password");
 
       DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
       OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource, clientContext);

       Greet greet = restTemplate.getForObject("http://localhost:8080", Greet.class);

       Assert.assertEquals("Hello World!", greet.getMessage());
}
 
Example 5
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
public void testConnectDirectlyToResourceServer() throws Exception {
    ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
    resource.setAccessTokenUri(tokenUrl);
    resource.setId("microservice-test");
    resource.setClientId("oauthClient1");
    resource.setClientSecret("oauthClient1Password");

    resource.setGrantType("password");

    resource.setScope(Arrays.asList("openid"));

    resource.setUsername("[email protected]");
    resource.setPassword("user1");
    OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
    logger.info(" CALLING: " + baseUrl+"/api");

    String result = template.getForObject(baseUrl+"/api", String.class);

    System.err.println(result);
    assertEquals("Hello, Trusted User marissa", result);
}
 
Example 6
Source File: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccessSecurityInfoUrlWithOAuth2AccessToken2TimesAndLogout() throws Exception {

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setGrantType("client_credentials");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localDataflowResource.getMockMvc()
			.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
			.andExpect(status().isOk())
			.andExpect(jsonPath("$.authenticated", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.authenticationEnabled", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.roles", hasSize(7)));

	boolean oAuthServerResponse = oAuth2RestTemplate.getForObject("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/revoke_token", Boolean.class);

	assertTrue(Boolean.valueOf(oAuthServerResponse));

	localDataflowResource.getMockMvc()
		.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
		.andExpect(status().isUnauthorized());

	localDataflowResource.getMockMvc()
		.perform(get("/logout").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
		.andExpect(status().isFound());

	localDataflowResource.getMockMvc()
		.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
		.andExpect(status().isUnauthorized());

}
 
Example 7
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 4 votes vote down vote up
public void test_post_signup() throws Exception {

        OAuth2RestTemplate template = template();

        String url = baseUrl+"/signup/new";
        String expected = "foobar";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);

//        String result = template.postForObject(url, Object request, String.class, Object... uriVariables)

//        String result = template.postForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        Assert.assertThat(result, is(expected));

    }
 
Example 8
Source File: SsoAuthClient.java    From cola with MIT License 4 votes vote down vote up
/**
 * 退出
 *
 * @return
 */
public Result<String> logout() {
	OAuth2RestTemplate auth2RestTemplate = new OAuth2RestTemplate(properties);
	auth2RestTemplate.getForObject(properties.getRevokeTokenUri(), String.class);
	return Result.success();
}
 
Example 9
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 4 votes vote down vote up
@Test
public void test_myEvents_admin1() throws Exception {

    OAuth2RestTemplate template = template("admin1");

    String url = baseUrl+"/events/my";
    String expected = "{\"currentUser\":[{\"id\":102,\"summary\":\"Vacation\",\"description\":\"Paragliding in Greece\",\"when\":";

    logger.info(" CALLING: {}", url);
    String result = template.getForObject(url, String.class);
    logger.info(" RESULT: {}", result);

    assertThat(result, startsWith(expected));


}
 
Example 10
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 4 votes vote down vote up
@Test
public void test_myEvents_user1() throws Exception {

    OAuth2RestTemplate template = template("user1");

    String url = baseUrl+"/events/my";
    String expected = "{\"currentUser\":[{\"id\":100,\"summary\":\"Birthday Party\",\"description\":\"This is going to be a great birthday\",\"when\":";

    logger.info(" CALLING: {}", url);
    String result = template.getForObject(url, String.class);
    logger.info(" RESULT: {}", result);

    assertThat(result, startsWith(expected));


}
 
Example 11
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 4 votes vote down vote up
public void test_post_signup_user1() throws Exception {

        OAuth2RestTemplate template = template("user1");

        String url = baseUrl+"/signup/new";
        String expected = "foobar";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);

//        String result = template.postForObject(url, Object request, String.class, Object... uriVariables)

//        String result = template.postForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        assertThat(result, is(expected));

    }
 
Example 12
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_default() throws Exception {

        OAuth2RestTemplate template = template();

        String url = baseUrl+"/default";
        String expected = "{'message': 'welcome to the JBCP Calendar Application'}";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        Assert.assertThat(result, is(expected));

    }
 
Example 13
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_new_create_new_EventFormAutoPopulate() throws Exception {

        OAuth2RestTemplate template = template();

        String url = baseUrl+"/events/new?auto";
        String expected = "foobar";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
//        String result = template.postForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        Assert.assertThat(result, is(expected));

    }
 
Example 14
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_createEvent_user1() throws Exception {

        OAuth2RestTemplate template = template("user1");

        String url = baseUrl+"/events/new";
        String expected = "foobar";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        assertThat(result, is(expected));

    }
 
Example 15
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
@Test
public void test_default_user1() throws Exception {

    OAuth2RestTemplate template = template("user1");

    String url = baseUrl+"/default";
    String expected = "{'message': 'welcome to the JBCP Calendar Application'}";

    logger.info(" CALLING: {}", url);
    String result = template.getForObject(url, String.class);
    logger.info(" RESULT: {}", result);

    assertThat(result, is(expected));

}
 
Example 16
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_default_admin1() throws Exception {

        OAuth2RestTemplate template = template("admin1");

        String url = baseUrl+"/default";
        String expected = "{'message': 'welcome to the JBCP Calendar Application'}";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        assertThat(result, is(expected));

    }
 
Example 17
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_rootContext() throws Exception {

        OAuth2RestTemplate template = template();

        String url = baseUrl+"/";
        String expected = "{'message': 'welcome to the JBCP Calendar Application'}";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        Assert.assertThat(result, is(expected));

    }
 
Example 18
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_signup_user1() throws Exception {

        OAuth2RestTemplate template = template("user1");

        String url = baseUrl+"/signup/new";
        String expected = "foobar";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        assertThat(result, is(expected));

    }
 
Example 19
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_signup() throws Exception {

        OAuth2RestTemplate template = template();

        String url = baseUrl+"/signup/new";
        String expected = "foobar";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        Assert.assertThat(result, is(expected));

    }
 
Example 20
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 3 votes vote down vote up
public void test_createEvent() throws Exception {

        OAuth2RestTemplate template = template();

        String url = baseUrl+"/events/new";
        String expected = "foobar";

        logger.info(" CALLING: {}", url);
        String result = template.getForObject(url, String.class);
        logger.info(" RESULT: {}", result);

        Assert.assertThat(result, is(expected));

    }