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

The following examples show how to use org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails#setClientSecret() . 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: DataFlowClientAutoConfigurationAgaintstServerTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
public void usingUserWithViewRoles() throws Exception {

	final ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setUsername("bob");
	resourceDetails.setPassword("bobspassword");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

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

	System.setProperty("accessTokenAsString", accessToken.getValue());

	context = new AnnotationConfigApplicationContext(TestApplication.class);

	final DataFlowOperations dataFlowOperations = context.getBean(DataFlowOperations.class);
	final AboutResource about = dataFlowOperations.aboutOperation().get();

	assertNotNull(about);
	assertEquals("bob", about.getSecurityInfo().getUsername());
	assertEquals(1, about.getSecurityInfo().getRoles().size());
}
 
Example 2
Source File: DataFlowClientAutoConfigurationAgaintstServerTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
public void usingUserWithAllRoles() throws Exception {

	final ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setUsername("user");
	resourceDetails.setPassword("secret10");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

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

	System.setProperty("accessTokenAsString", accessToken.getValue());

	context = new AnnotationConfigApplicationContext(TestApplication.class);

	final DataFlowOperations dataFlowOperations = context.getBean(DataFlowOperations.class);
	final AboutResource about = dataFlowOperations.aboutOperation().get();

	assertNotNull(about);
	assertEquals("user", about.getSecurityInfo().getUsername());
	assertEquals(7, about.getSecurityInfo().getRoles().size());
}
 
Example 3
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 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: Oauth2ClientRestTemplate.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 演示 grant_type=password 时,获取资源的方法
 * 用的场景还不知道,@Deprecated
 *
 * @param client_id
 * @param client_secret    取决于 AuthorizationServer 设置,如果 client 设置了secret,则此项参数为必需,否则可以没有
 * @param access_token_uri
 * @param username
 * @param password
 * @param scope
 * @return
 */
@Deprecated
public OAuth2RestOperations resourceOwnerPasswordRestTemplate(String client_id, String client_secret, String access_token_uri, String username, String password, String... scope) {

    // 防止 url 写错
    if (!access_token_uri.contains("token"))
        throw new RuntimeException("uri is wrong :  access_token_uri = " + access_token_uri);

    // 防止 client_secret 写错
    if (username == null || password == null || username.isEmpty() || password.isEmpty())
        throw new RuntimeException("username or password  is wrong :  username or password is a required parameter");

    ResourceOwnerPasswordResourceDetails details = new ResourceOwnerPasswordResourceDetails();
    details.setId("3");
    details.setClientId(client_id);
    if (client_secret != null && !client_secret.isEmpty())
        details.setClientSecret(client_secret);
    details.setAccessTokenUri(access_token_uri);
    details.setUsername(username);
    details.setPassword(password);
    details.setScope(Arrays.asList(scope));
    return new OAuth2RestTemplate(details, oAuth2ClientContext);


}
 
Example 6
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 7
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 8
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 9
Source File: ClientConfiguration.java    From OAuth-2.0-Cookbook with MIT License 6 votes vote down vote up
@Bean
public OAuth2ProtectedResourceDetails passwordResourceDetails() {
    //@formatter:off
    ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();

    resourceDetails.setId("oauth2server");
    resourceDetails.setTokenName("oauth_token");
    resourceDetails.setClientId("clientapp");
    resourceDetails.setClientSecret("123456");
    resourceDetails.setAccessTokenUri("http://localhost:8080/oauth/token");
    resourceDetails.setScope(Arrays.asList("read_profile"));

    resourceDetails.setClientAuthenticationScheme(AuthenticationScheme.header);
    //@formatter:on

    return resourceDetails;
}
 
Example 10
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 11
Source File: AuthorizationTest.java    From spring-boot-demo with MIT License 5 votes vote down vote up
protected OAuth2RestTemplate oauth2RestTemplate(String username, String password, List<String> scope) {
    ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
    resource.setAccessTokenUri(AUTHORIZATION_SERVER + "/oauth/token");
    resource.setClientId("oauth2");
    resource.setClientSecret("oauth2");
    resource.setId("oauth2");
    resource.setScope(scope);
    resource.setUsername(username);
    resource.setPassword(password);
    return new OAuth2RestTemplate(resource);
}
 
Example 12
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 13
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 14
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);
}
 
Example 15
Source File: OAuthClient.java    From cf-java-client-sap with Apache License 2.0 5 votes vote down vote up
private OAuth2ProtectedResourceDetails getResourceDetails(String username, String password, String clientId, String clientSecret) {
    ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
    resource.setUsername(username);
    resource.setPassword(password);

    resource.setClientId(clientId);
    resource.setClientSecret(clientSecret);
    resource.setId(clientId);
    resource.setClientAuthenticationScheme(AuthenticationScheme.header);
    resource.setAccessTokenUri(authorizationUrl + "/oauth/token");

    return resource;
}
 
Example 16
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 17
Source File: SsoAuthClient.java    From cola with MIT License 5 votes vote down vote up
/**
 * 登录
 *
 * @param username
 * @param password
 * @return
 */
public OAuth2AccessToken login(String username, String password) {
	ResourceOwnerPasswordResourceDetails details = new ResourceOwnerPasswordResourceDetails();
	details.setUsername(username);
	details.setPassword(password);
	details.setAccessTokenUri(this.properties.getAccessTokenUri());
	details.setClientId(this.properties.getClientId());
	details.setClientSecret(this.properties.getClientSecret());
	OAuth2RestTemplate auth2RestTemplate = new OAuth2RestTemplate(details);
	auth2RestTemplate.setAccessTokenProvider(new ResourceOwnerPasswordAccessTokenProvider());
	return auth2RestTemplate.getAccessToken();
}
 
Example 18
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 19
Source File: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccessSecurityInfoUrlWithOAuth2AccessTokenPasswordGrant2Times() throws Exception {

	final ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setUsername("user");
	resourceDetails.setPassword("secret10");
	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)));

	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)));
}
 
Example 20
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;
}