Java Code Examples for org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails#setGrantType()

The following examples show how to use org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails#setGrantType() . 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: JavaConfig.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Bean
    public OAuth2RestOperations oAuth2RestOperations() {

        ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
        resource.setAccessTokenUri(tokenUri);

        resource.setId(resourceId);
        resource.setClientId(resourceClientId);
        resource.setClientSecret(resourceClientSecret);

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

        resource.setUsername(resourceUserId);
        resource.setPassword(resourceUserPassword);

        OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
//        template.setRequestFactory(requestFactory);
        return template;
    }
 
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 {
    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 4
Source File: JavaConfig.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Bean
    public OAuth2RestOperations oAuth2RestOperations() {

        ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
        resource.setAccessTokenUri(tokenUrl);
        resource.setId(resourceId);
        resource.setClientId(resourceClientId);
        resource.setClientSecret(resourceClientSecret);

        resource.setGrantType("password");

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

        resource.setUsername("[email protected]");
        resource.setPassword("user1");

        OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
//        template.setRequestFactory(requestFactory);
        return template;
    }
 
Example 5
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 6
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 7
Source File: OpenRestTemplate.java    From open-cloud with MIT License 5 votes vote down vote up
/**
 * 构建网关Oauth2 password方式请求
 *
 * @param clientId
 * @param clientSecret
 * @param accessTokenUri
 * @param username
 * @param password
 * @return
 */
public OAuth2RestTemplate buildOAuth2PasswordRequest(String clientId, String clientSecret, String accessTokenUri, String username, String password) {
    ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
    resource.setUsername(username);
    resource.setPassword(password);
    resource.setClientId(clientId);
    resource.setClientSecret(clientSecret);
    resource.setAccessTokenUri(accessTokenUri);
    resource.setAuthenticationScheme(AuthenticationScheme.form);
    resource.setGrantType("password");
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource);
    return restTemplate;
}
 
Example 8
Source File: BasicControllerIT.java    From Mastering-Spring-5.1 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user2");
	resource.setPassword("user2-password");
	resource.setAccessTokenUri(createURL("/oauth/token"));
	resource.setClientId("YourClientID");
	resource.setClientSecret("TopSecretClientPassword");
	resource.setGrantType("password");
	// resource.setAuthenticationScheme(AuthenticationScheme.header);
	// resource.
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example 9
Source File: TodoControllerIT.java    From Mastering-Spring-5.1 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user2");
	resource.setPassword("user2-password");
	resource.setAccessTokenUri(createUrl("/oauth/token"));
	resource.setClientId("YourClientID");
	resource.setClientSecret("TopSecretClientPassword");
	resource.setGrantType("password");
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example 10
Source File: OauthClientApplication.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public OAuth2ProtectedResourceDetails oAuth2ProtectedResourceDetails() {
    final ResourceOwnerPasswordResourceDetails details = new ResourceOwnerPasswordResourceDetails();
    details.setAccessTokenUri("http://localhost:9090/oauth/token");
    details.setClientId("webapp");
    details.setClientSecret("websecret");
    details.setGrantType("password");

    return details;
}
 
Example 11
Source File: BasicControllerIT.java    From Mastering-Spring-5.0 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user-name");
	resource.setPassword("user-password");
	resource.setAccessTokenUri(createURL("/oauth/token"));
	resource.setClientId("clientId");
	resource.setClientSecret("clientSecret");
	resource.setGrantType("password");
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example 12
Source File: TodoControllerIT.java    From Mastering-Spring-5.0 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user-name");
	resource.setPassword("user-password");
	resource.setAccessTokenUri(createUrl("/oauth/token"));
	resource.setClientId("clientId");
	resource.setClientSecret("clientSecret");
	resource.setGrantType("password");
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example 13
Source File: OAuth2ClientTest.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
private OAuth2RestTemplate template(String user){
    ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
    resource.setAccessTokenUri(tokenUrl);
    resource.setId(resourceId);
    resource.setClientId(resourceClientId);
    resource.setClientSecret(resourceClientSecret);

    resource.setGrantType("password");

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

    this.setResourceUser(resource, user);

    return new OAuth2RestTemplate(resource);
}